思路:先排序l,对于相等的情况排序w,然后求最大上升子序列。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _node {
int l;
int w;
bool visit;
}Node;
Node a[5010];
int n, m;
int cmp(const void *b, const void *c)
{
if ((*(Node *)b).l == (*(Node *)c).l)
return (*(Node *)b).w - (*(Node *)c).w;
else
return (*(Node *)b).l - (*(Node *)c).l;
}
int greedy()
{
int result = 0, max = 0;
for (int i = 0; i < m; ++i) {
if (!a[i].visit) {
result++;
int weight = a[i].w;
a[i].visit = true;
for (int j = i + 1; j < m; ++j) {
if (!a[j].visit && weight <= a[j].w) {
a[j].visit = true;
weight = a[j].w;
}
}
}
}
return result;
}
int main()
{
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; ++i) {
scanf("%d", &m);
for (int j = 0; j < m; ++j) {
scanf("%d%d", &a[j].l, &a[j].w);
a[j].visit = false;
}
qsort(a, m, sizeof(Node), cmp);
printf("%d\n", greedy());
}
}
}