题目URL:http://acm.hdu.edu.cn/showproblem.php?pid=1103
Code with C++:
1 #include <iostream>
2 #include <queue>
3 #include <algorithm>
4 using namespace std;
5 // 优先队列,用于放置最晚离开时间
6 priority_queue<int, vector<int>, greater<int> > team12;
7 priority_queue<int, vector<int>, greater<int> > team34;
8 priority_queue<int, vector<int>, greater<int> > team56;
9 // 三类桌子的座位数
10 int A, B, C;
11 // 初始化队列
12 void Init_Queue()
13 {
14 int i;
15 for(i = 0; i < A; i++)
16 team12.push(0);
17 for(i = 0; i < B; i++)
18 team34.push(0);
19 for(i = 0; i < C; i++)
20 team56.push(0);
21 }
22 int main()
23 {
24 int sum_n, Hour, Min, Num, h;
25 char ch;
26 while(scanf("%d %d %d", &A, &B, &C))
27 {
28 if(A == 0 && B == 0 && C == 0) break;
29 Init_Queue();
30 sum_n = 0;
31 getchar(); // 去除回车
32 while(ch = getchar())
33 {
34 if(ch == '#') break;
35 scanf("%d:%d %d", &Hour, &Min, &Num);
36 Hour += 10*(ch-'0');
37 Hour = Hour*60+Min;
38
39 if(Num > 4)
40 {
41 h = team56.top();
42 if(h <= Hour+30)
43 {
44 team56.pop();
45 if(h <= Hour) // 桌子原来就是空的
46 {
47 h = Hour+30;
48 }
49 else
50 {
51 h += 30;
52 }
53 team56.push(h);
54 sum_n += Num;
55 }
56 }
57 else if(Num > 2)
58 {
59 h = team34.top();
60 if(h <= Hour+30)
61 {
62 team34.pop();
63 if(h <= Hour)
64 {
65 h = Hour+30;
66 }
67 else
68 {
69 h += 30;
70 }
71 team34.push(h);
72 sum_n += Num;
73 }
74 }
75 else
76 {
77 h = team12.top();
78 if(h <= Hour+30)
79 {
80 team12.pop();
81 if(h <= Hour)
82 {
83 h = Hour+30;
84 }
85 else
86 {
87 h += 30;
88 }
89 team12.push(h);
90 sum_n += Num;
91 }
92 }
93 getchar();
94 }
95 printf("%d\n", sum_n);
96 while(!team12.empty()){
97 team12.pop();
98 }
99 while(!team34.empty()){
100 team34.pop();
101 }
102 while(!team56.empty()){
103 team56.pop();
104 }
105 }
106 //system("pause");
107 return 0;
108 }