时间限制:65535 KB
难度:4
 
描述
学校的小礼堂每天都会有许多活动,有时间这些活动的计划时间会发生冲突,需要选择出一些活动进行举办。小刘的工作就是安排学校小礼堂的活动,每个时间最多安排一个活动。现在小刘有一些活动计划的时间表,他想尽可能的安排更多的活动,请问他该如何安排。
 
输入
第一行是一个整型数m(m<100)表示共有m组测试数据。
每组测试数据的第一行是一个整数n(1<n<10000)表示该测试数据共有n个活动。
随后的n行,每行有两个正整数Bi,Ei(0<=Bi,Ei<10000),分别表示第i个活动的起始与结束时间(Bi<=Ei)
输出
对于每一组输入,输出最多能够安排的活动数量。
每组的输出占一行
样例输入
2
2
1 10
10 11
3
1 10
10 11
11 20
样例输出
1
2
提示
注意:如果上一个活动在t时间结束,下一个活动最早应该在t+1时间开始

 

 1  
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 typedef struct act{
 6   int s;
 7   int e;
 8 }STACT;
 9 int compare(const void *a, const void *b){
10   STACT *acts1 = (STACT *)a;
11   STACT *acts2 = (STACT *)b;
12   return (acts1->e - acts2->e);
13 }
14 int main()
15 {
16   //m:num of test,n:num of activities
17   int m,n;
18   int i;
19   scanf("%d", &m);
20   getchar();
21   while (m--) {
22       scanf("%d", &n);
23       getchar();
24       STACT *ats=(STACT *)malloc(sizeof(STACT)*n);
25       memset(ats, 0x00, sizeof(STACT)*n);
26       for (i = 0; i < n; ++i) {
27           scanf("%d%d", &ats[i].s, &ats[i].e);
28         }
29       qsort(ats, n, sizeof(ats[0]), compare);
30       int sum=0;
31       int curTime=-1;
32       for (i = 0; i < n; ++i) {
33           if(curTime < ats[i].s){
34               ++sum;
35               curTime = ats[i].e;
36             }
37           else {
38               continue;
39             }
40         }
41       printf("%d\n", sum);
42       if(ats != NULL){
43           free(ats);
44           ats=NULL;
45         }
46     }
47   return 0;
48 }
View Code

相关文章: