题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=14
思路:贪心
1.按照结束时间排序
2.遍历维护数量
#include<stdio.h>
#include<algorithm>
const int MAX=10001;
using namespace std;
struct time
{
int start;
int end;
};
bool order_by_end(struct time a,struct time b)
{
if(a.end!=b.end)
return a.end<b.end;
else
return a.start<b.start;
}
main()
{
int i,j,times,m,end,count;
struct time a[MAX];
scanf("%d",×);
while(times--){
count=1;
scanf("%d",&m);
for(i=0;i<m;i++)
scanf("%d%d",&a[i].start,&a[i].end);
sort(a,a+m,order_by_end);
end=a[0].end;
for(i=1;i<m;i++)
if(a[i].start>end){
end=a[i].end;
count++;
}
printf("%d\n",count);
}
}