suseoj 1210: 会场安排问题 (贪心)

1210: 会场安排问题

时间限制: 1 Sec  内存限制: 128 MB
提交: 1  解决: 1
[ 提交][ 状态][ 讨论版][命题人: liyuansong]

题目描述

假设要在足够多的会场里安排一批活动,并希望使用尽可能少的会场。设计一个有效的贪心算法进行安排(这个问题实际上是著名的图着色问题。若将每一个活动作为图的一个顶点,不相容活动间用边相连。使相邻顶点着有不同颜色的最小着色数,相应于要找的最小会场数。)

对于给定的k个待安排的活动,计算使用最少会场的时间表。

输入

第1行有1个正整数k,表示有k个待安排的活动。下来的k行中,每行有2个正整数,分别表示k个待安排的活动的开始时间和结束时间。间以0点开始的分钟计。

输出

将计算出的最少会场数输出。

样例输入

5
1 23
12 28
25 35
27 80
36 50

样例输出

3

C/C++代码实现(AC):
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <stack>
 7 #include <map>
 8 #include <queue>
 9 #include <climits>
10 
11 using namespace std;
12 const int MAX = 1e6 + 10;
13 int a, b, ans = 1, temp, n;
14 struct node
15 {
16     int x, y;
17 } P[MAX];
18 
19 bool cmp(node a, node b)
20 {
21     return a.y < b.y;
22 }
23 
24 int main()
25 {
26     scanf("%d", &n);
27     for (int i = 0; i < n; ++ i)
28     {
29         scanf("%d%d", &a,&b);
30         P[i].x = a,
31              P[i].y = b;
32     }
33     sort(P, P + n, cmp);
34     temp = P[0].y;
35     for (int i = 0; i < n; ++ i)
36     {
37         if (P[i].x > temp)
38         {
39             temp = P[i].y;
40             ++ ans;
41         }
42     }
43     printf("%d\n", ans);
44     return 0;
45 }

猜你喜欢

转载自www.cnblogs.com/GetcharZp/p/9162423.html
今日推荐