题目大意:有n个木棍,分别具有长度li和重量wi。对于木棍s1和s2,若l1<=l2且w1<=w2,则s1、s2可构成单调递增序列。求n个木棍中这样序列的个数。
最先的想法是,先排序,然后一遍一遍扫描,直到全部处理完。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 5000+10 struct Wooden
{
int l, w;
bool operator < (const Wooden& x) const
{
if (l != x.l) return l < x.l;
else return w < x.w;
}
};
Wooden wooden[MAXN];
bool processed[MAXN]; int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int T;
scanf("%d", &T);
while (T--)
{
int n;
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%d%d", &wooden[i].l, &wooden[i].w);
sort(wooden, wooden+n);
int time = ;
memset(processed, , sizeof(processed));
int remain = n;
while (remain > )
{
time++;
int prel = -, prew = -;
for (int i = ; i < n; i++)
if (!processed[i] && wooden[i].l >= prel && wooden[i].w >= prew)
{
processed[i] = true;
remain--;
prel = wooden[i].l;
prew = wooden[i].w;
}
}
printf("%d\n", time);
}
return ;
}
看到网上说可以通过求解最长(严格)递减序列的答案,可是怎么都理解不了,留待以后吧...