14.会场安排问题(L4)

时间限制:3000 ms  |  内存限制: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
", sum);
42       if(ats != NULL){
43           free(ats);
44           ats=NULL;
45         }
46     }
47   return 0;
48 }
View Code

 Time: 172ms Space:316

 1  
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 struct act{
 6   int s;
 7   int e;
 8 }actnode[10000];
 9 int compare(const void *a, const void *b){
10   act *acts1 = (act *)a;
11   act *acts2 = (act *)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 
25       memset(&actnode , 0x00, sizeof(actnode));
26       for (i = 0; i < n; ++i) {
27           scanf("%d%d", &actnode[i].s, &actnode[i].e);
28         }
29       qsort(actnode, n, sizeof(actnode[0]), compare);
30       int sum=0;
31       int curTime=-1;
32       for (i = 0; i < n; ++i) {
33           if(curTime < actnode[i].s){
34               ++sum;
35               curTime = actnode[i].e;
36             }
37           else {
38               continue;
39             }
40         }
41       printf("%d
", sum);
42 
43     }
44   return 0;
45 }
46         
View Code

 212ms 392

1 选择结束时间最早的,贪心算法
解体思路

有误:

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stdio.h>
 5 using namespace std;
 6 #define debug(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
 7 
 8 struct noac{
 9     int Ti_s;
10     int Ti_e;
11     bool operator<(const noac& nc) const // DESC by Ti_s
12     {
13         return (Ti_s > nc.Ti_s);
14     }
15     friend void operator<<(ostream &os, const noac &nc)
16     {
17         os << nc.Ti_s << ' ' << nc.Ti_e << endl;
18     }
19 }tms[10001];
20 
21 int main(){
22     int m,n;
23     scanf("%d", &m);
24     while (m--) {
25         scanf("%d", &n);
26         memset(&tms, 10001, 0x00);
27         for (int i = 0; i < n; ++i)
28         {
29             scanf("%d%d", &tms[i].Ti_s, &tms[i].Ti_e);
30         }
31         sort(tms, tms + n);
32 
33         int maxac = 0;
34         for (int i = 0; i < n; ++i)
35         {
36             int idx  = i;
37             int sum = 0;
38             for (int j = i+1; j < n; ++j)
39             {
40                 if(tms[idx].Ti_s >= tms[j].Ti_e)
41                 {
42                     ++sum;
43                     ++idx;
44                 }else
45                 {
46                     continue;
47                 }
48             }
49             maxac = max(maxac, sum);
50         }
51         printf("%d
", maxac);
52     }
53     return 0;
54 }
View Code

比如说:

6
1 5
2 3
10 11
6 7
8 9
4 5

output: 4,而实际输出为5

原文地址:https://www.cnblogs.com/guxuanqing/p/5628766.html