题意:区间调度问题
解法:应用贪心算法,贪心的规则:
在可选的节目中,选取结束时间早的节目。
1: #include<stdlib.h>
2: #include<string.h>
3: #include<stdio.h>
4: #define N 101
5: struct time{
6: int s,t;
7: }timer[N];
8: int comp(const void *p,const void *q){
9: struct time a=*(struct time *)p;
10: struct time b=*(struct time *)q;
11: return a.t-b.t;
12: }
13: int main(){
14: int n,i,ans=0,stamp=0;
15: while(scanf("%d",&n)!=EOF&&n){
16: for(i=0;i<n;i++)
17: scanf("%d %d",&timer[i].s,&timer[i].t);
18: qsort(timer,n,sizeof(struct time),comp);//按照结束的时间从小到大排序
19: ans=0,stamp=0;
20: for(i=0;i<n;i++){
21: if(stamp<=timer[i].s){ //stamp:时间戳
22: stamp=timer[i].t;
23: ans++;
24: }
25: }
26: printf("%d ",ans);
27: }
28: }
29: