最开始没想出怎么贪心,看了网上的说法,才知道是贪心,但是网上的文章大多又没有证明……
/*
* HDU-1051
* mike-w
* 2012-10-16
* GREEDY
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 5555
#define LOL_DEBUG
int w[MAXSIZE][2];
int tag[MAXSIZE];
int T, N;
int comp(const void *e1, const void *e2)
{
int *p1=(int*)e1;
int *p2=(int*)e2;
return (p1[0]-p2[0]) ? (p1[0]-p2[0]) : (p1[1]-p2[1]);
}
int main(void)
{
int i, j, cnt, x1, x2;
scanf("%d", &T);
while(T-->0)
{
scanf("%d", &N);
for(i=0; i<N; i++)
scanf("%d%d", w[i], w[i]+1);
qsort(w, N, sizeof(int[2]), comp);
#ifdef LOL_DEBUG
for(i=0; i<N; i++)
printf("[%2d, %2d]\n", w[i][0], w[i][1]);
#endif
memset(tag, 0, sizeof(tag));
for(i=0, cnt=0; i<N; i++)
if(!tag[i])
{
cnt++;
x1=w[i][0];
x2=w[i][1];
for(j=i; j<N; j++)
if(!tag[j] && x1<=w[j][0] && x2<=w[j][1])
tag[j]=1, x1=w[j][0], x2=w[j][1];
}
printf("%d\n", cnt);
}
return 0;
}