pku 3306 Busy Airport 模拟+堆
题意就不说了,就是模拟一个机场的起降时间如果当前时刻不能起飞,则推迟10分钟再次尝试
这题里面还包含一个历法子问题,甚是麻烦。。于是就用了各种C++面向对象技术把条理理清楚
历法的详细解释在题目最底端,不是我们用的公历
直接贴代码了
1
# include
<
cstdio
>
2 # include < queue >
3 # include < cstdlib >
4 # include < vector >
5 # include < cstring >
6 using namespace std;
7 struct Time
8 {
9 int s,m,h,ms;
10 Time( char * str)
11 {
12 h = atoi(strtok(str, " : " ));
13 m = atoi(strtok(NULL, " : " ));
14 s = atoi(strtok(NULL, " . " ));
15 ms = atoi(strtok(NULL, " . " ));
16 }
17 Time(){}
18 void print()
19 {
20 printf( " %s%d:%s%d:%s%d. " ,h < 10 ? " 0 " : "" ,h,m < 10 ? " 0 " : "" ,m,s < 10 ? " 0 " : "" ,s);
21 if (ms < 10 ) printf( " 00 " );
22 else if (ms < 100 ) printf( " 0 " );
23 printf( " %d " ,ms);
24 }
25 bool operator > ( const Time & a) const
26 {
27 if (h != a.h) return h > a.h;
28 else if (m != a.m) return m > a.m;
29 else if (s != a.s) return s > a.s;
30 else return ms > a.ms;
31 }
32 bool operator != ( const Time & a) const
33 {
34 return s != a.s || m != a.m || h != a.h || ms != a.ms;
35 }
36 Time operator + ( const Time & a)
37 {
38 Time tmp =* this ;
39 tmp.ms += a.ms;
40 tmp.s += a.s;
41 tmp.h += a.h;
42 tmp.m += a.m;
43 tmp.s += tmp.ms / 1000 ;
44 tmp.ms %= 1000 ;
45 tmp.m += tmp.s / 60 ;
46 tmp.s %= 60 ;
47 tmp.h += tmp.m / 60 ;
48 tmp.m %= 60 ;
49 return tmp;
50 }
51 };
52 struct Date
53 {
54 Time t;
55 int y,m,d;
56 Date(){}
57 Date( char * str, const Time & tt)
58 {
59 t = tt;
60 d = atoi(strtok(str, " / " )) - 1 ;
61 m = atoi(strtok(NULL, " / " )) - 1 ;
62 y = atoi(strtok(NULL, " / " ));
63 }
64 void print()
65 {
66 printf( " %s%d/%s%d/%d " ,d + 1 < 10 ? " 0 " : "" ,d + 1 ,m + 1 < 10 ? " 0 " : "" ,m + 1 ,y);
67 t.print();
68 }
69 bool operator > ( const Date & a) const
70 {
71 if (y != a.y) return y > a.y;
72 else if (m != a.m) return m > a.m;
73 else if (d != a.d) return d > a.d;
74 else return t > a.t;
75 }
76 bool operator != ( const Date & a) const
77 {
78 return y != a.y || m != a.m || d != a.d || t != a.t;
79 }
80 bool leap()
81 {
82 switch (y % 33 )
83 {
84 case 1 : return 1 ;
85 case 5 : return 1 ;
86 case 9 : return 1 ;
87 case 13 : return 1 ;
88 case 17 : return 1 ;
89 case 22 : return 1 ;
90 case 30 : return 1 ;
91 default : return 0 ;
92 };
93 }
94 int maxday()
95 {
96 if (m < 6 ) return 31 ;
97 else if (m < 11 ) return 30 ;
98 else return leap() ? 30 : 29 ;
99 }
100 Date add( const Time & a)
101 {
102 Date tmp =* this ;
103 tmp.t = tmp.t + a;
104 tmp.d += tmp.t.h / 24 ;
105 tmp.t.h %= 24 ;
106 tmp.m += tmp.d / maxday();
107 tmp.d %= maxday();
108 tmp.y += tmp.m / 12 ;
109 tmp.m %= 12 ;
110 return tmp;
111 }
112 };
113 struct entry
114 {
115 bool type; // 0 起飞 1 降落
116 int id;
117 Date t1,t2;
118 entry(){}
119 bool operator < ( const entry & a) const
120 {
121 if (t1 != a.t1) return t1 > a.t1;
122 else if (type != a.type) return type < a.type;
123 else return id > a.id;
124 }
125 void print()
126 {
127 printf( " %d " ,id);
128 t1.print();
129 }
130 };
131
132 priority_queue < entry > timetable;
133 int main()
134 {
135 int testcase;
136 Time ten;
137 ten.h = ten.s = ten.ms = 0 ;
138 ten.m = 10 ;
139 scanf( " %d " , & testcase);
140 for ( int test = 1 ;test <= testcase;test ++ )
141 {
142 int num,left;
143 printf( " Report for Test-Case #%d:\n " ,test);
144 scanf( " %d%d " , & num, & left);
145 while (num -- )
146 {
147 char date[ 255 ],time[ 255 ];
148 entry tmp;
149 tmp.type = 0 ;
150 scanf( " %d %s %s " , & tmp.id,date,time);
151 tmp.t1 = Date(date,Time(time));
152 scanf( " %s " ,time);
153 tmp.t2 = tmp.t1.add(Time(time));
154 timetable.push(tmp);
155 }
156 while ( ! timetable.empty())
157 {
158 entry top = timetable.top();
159 timetable.pop();
160 if (top.type == 1 )
161 {
162 top.print();
163 printf( " LANDED\n " );
164 left ++ ;
165 }
166 else
167 {
168 if (left > 0 )
169 {
170 left -- ;
171 top.print();
172 printf( " ACCEPTED\n " );
173 top.t1 = top.t2;
174 top.type = 1 ;
175 timetable.push(top);
176 }
177 else
178 {
179 top.print();
180 printf( " POSTPONED\n " );
181 top.t1 = top.t1.add(ten);
182 top.t2 = top.t2.add(ten);
183 timetable.push(top);
184 }
185 }
186 }
187 printf( " \n " );
188 }
189 return 0 ;
190 }
191
2 # include < queue >
3 # include < cstdlib >
4 # include < vector >
5 # include < cstring >
6 using namespace std;
7 struct Time
8 {
9 int s,m,h,ms;
10 Time( char * str)
11 {
12 h = atoi(strtok(str, " : " ));
13 m = atoi(strtok(NULL, " : " ));
14 s = atoi(strtok(NULL, " . " ));
15 ms = atoi(strtok(NULL, " . " ));
16 }
17 Time(){}
18 void print()
19 {
20 printf( " %s%d:%s%d:%s%d. " ,h < 10 ? " 0 " : "" ,h,m < 10 ? " 0 " : "" ,m,s < 10 ? " 0 " : "" ,s);
21 if (ms < 10 ) printf( " 00 " );
22 else if (ms < 100 ) printf( " 0 " );
23 printf( " %d " ,ms);
24 }
25 bool operator > ( const Time & a) const
26 {
27 if (h != a.h) return h > a.h;
28 else if (m != a.m) return m > a.m;
29 else if (s != a.s) return s > a.s;
30 else return ms > a.ms;
31 }
32 bool operator != ( const Time & a) const
33 {
34 return s != a.s || m != a.m || h != a.h || ms != a.ms;
35 }
36 Time operator + ( const Time & a)
37 {
38 Time tmp =* this ;
39 tmp.ms += a.ms;
40 tmp.s += a.s;
41 tmp.h += a.h;
42 tmp.m += a.m;
43 tmp.s += tmp.ms / 1000 ;
44 tmp.ms %= 1000 ;
45 tmp.m += tmp.s / 60 ;
46 tmp.s %= 60 ;
47 tmp.h += tmp.m / 60 ;
48 tmp.m %= 60 ;
49 return tmp;
50 }
51 };
52 struct Date
53 {
54 Time t;
55 int y,m,d;
56 Date(){}
57 Date( char * str, const Time & tt)
58 {
59 t = tt;
60 d = atoi(strtok(str, " / " )) - 1 ;
61 m = atoi(strtok(NULL, " / " )) - 1 ;
62 y = atoi(strtok(NULL, " / " ));
63 }
64 void print()
65 {
66 printf( " %s%d/%s%d/%d " ,d + 1 < 10 ? " 0 " : "" ,d + 1 ,m + 1 < 10 ? " 0 " : "" ,m + 1 ,y);
67 t.print();
68 }
69 bool operator > ( const Date & a) const
70 {
71 if (y != a.y) return y > a.y;
72 else if (m != a.m) return m > a.m;
73 else if (d != a.d) return d > a.d;
74 else return t > a.t;
75 }
76 bool operator != ( const Date & a) const
77 {
78 return y != a.y || m != a.m || d != a.d || t != a.t;
79 }
80 bool leap()
81 {
82 switch (y % 33 )
83 {
84 case 1 : return 1 ;
85 case 5 : return 1 ;
86 case 9 : return 1 ;
87 case 13 : return 1 ;
88 case 17 : return 1 ;
89 case 22 : return 1 ;
90 case 30 : return 1 ;
91 default : return 0 ;
92 };
93 }
94 int maxday()
95 {
96 if (m < 6 ) return 31 ;
97 else if (m < 11 ) return 30 ;
98 else return leap() ? 30 : 29 ;
99 }
100 Date add( const Time & a)
101 {
102 Date tmp =* this ;
103 tmp.t = tmp.t + a;
104 tmp.d += tmp.t.h / 24 ;
105 tmp.t.h %= 24 ;
106 tmp.m += tmp.d / maxday();
107 tmp.d %= maxday();
108 tmp.y += tmp.m / 12 ;
109 tmp.m %= 12 ;
110 return tmp;
111 }
112 };
113 struct entry
114 {
115 bool type; // 0 起飞 1 降落
116 int id;
117 Date t1,t2;
118 entry(){}
119 bool operator < ( const entry & a) const
120 {
121 if (t1 != a.t1) return t1 > a.t1;
122 else if (type != a.type) return type < a.type;
123 else return id > a.id;
124 }
125 void print()
126 {
127 printf( " %d " ,id);
128 t1.print();
129 }
130 };
131
132 priority_queue < entry > timetable;
133 int main()
134 {
135 int testcase;
136 Time ten;
137 ten.h = ten.s = ten.ms = 0 ;
138 ten.m = 10 ;
139 scanf( " %d " , & testcase);
140 for ( int test = 1 ;test <= testcase;test ++ )
141 {
142 int num,left;
143 printf( " Report for Test-Case #%d:\n " ,test);
144 scanf( " %d%d " , & num, & left);
145 while (num -- )
146 {
147 char date[ 255 ],time[ 255 ];
148 entry tmp;
149 tmp.type = 0 ;
150 scanf( " %d %s %s " , & tmp.id,date,time);
151 tmp.t1 = Date(date,Time(time));
152 scanf( " %s " ,time);
153 tmp.t2 = tmp.t1.add(Time(time));
154 timetable.push(tmp);
155 }
156 while ( ! timetable.empty())
157 {
158 entry top = timetable.top();
159 timetable.pop();
160 if (top.type == 1 )
161 {
162 top.print();
163 printf( " LANDED\n " );
164 left ++ ;
165 }
166 else
167 {
168 if (left > 0 )
169 {
170 left -- ;
171 top.print();
172 printf( " ACCEPTED\n " );
173 top.t1 = top.t2;
174 top.type = 1 ;
175 timetable.push(top);
176 }
177 else
178 {
179 top.print();
180 printf( " POSTPONED\n " );
181 top.t1 = top.t1.add(ten);
182 top.t2 = top.t2.add(ten);
183 timetable.push(top);
184 }
185 }
186 }
187 printf( " \n " );
188 }
189 return 0 ;
190 }
191