首页 > 代码库 > TIANKENG’s restaurant

TIANKENG’s restaurant

TIANKENG’s restaurant


Time Limit: 2000/1000 MS (Java/Others)    

Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 0    

Accepted Submission(s): 0


Problem Description
TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come to enjoy their meal, and there are Xi persons in the ith group in sum. Assuming that each customer can own only one chair. Now we know the arriving time STi and departure time EDi of each group. Could you help TIANKENG calculate the minimum chairs he needs to prepare so that every customer can take a seat when arriving the restaurant?
 
Input
The first line contains a positive integer T(T<=100), standing for T test cases in all.

Each cases has a positive integer n(1<=n<=10000), which means n groups of customer. Then following n lines, each line there is a positive integer Xi(1<=Xi<=100), referring to the sum of the number of the ith group people, and the arriving time STi and departure time Edi(the time format is hh:mm, 0<=hh<24, 0<=mm<60), Given that the arriving time must be earlier than the departure time.

Pay attention that when a group of people arrive at the restaurant as soon as a group of people leaves from the restaurant, then the arriving group can be arranged to take their seats if the seats are enough.
 
Output
For each test case, output the minimum number of chair that TIANKENG needs to prepare.
 
Sample Input
2
2
6 08:00 09:00
5 08:59 09:59
2
6 08:00 09:00
5 09:00 10:00
 
Sample Output
11
6
 
解题:排序后模拟+优先队列
 
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #define LL long long13 #define INF 0x3f3f3f3f14 using namespace std;15 struct node {16     int time,index;17     friend bool operator< (node x,node b) {18         return x.time > b.time;19     }20 };21 priority_queue<node>q;22 struct P {23     int num,ary,lev;24 };25 P p[10020];26 bool cmp(const P &x,const P &y) {27     return (x.ary < y.ary || x.ary == y.ary && x.lev < y.lev);28 }29 int main() {30     int t,i,j,n,k;31     int h1,h2,m1,m2;32     scanf("%d",&t);33     while(t--) {34         while(!q.empty()) q.pop();35         scanf("%d",&n);36         for(i = 0; i < n; i++) {37             scanf("%d %d:%d %d:%d",&k,&h1,&m1,&h2,&m2);38             p[i].ary = h1*60+m1;39             p[i].lev = h2*60+m2;40             p[i].num = k;41         }42         sort(p,p+n,cmp);43         int tm,ans = 0,ba = 0;//剩余的桌子44         q.push((node) {p[0].lev,0});45         ans = p[0].num;46         node tp;47         for(i = 1; i < n; i++) {48             tm = p[i].ary;49             if(p[i].ary == p[i].lev) continue;50             tp = q.top();51             q.push((node){p[i].lev,i});52             while(!q.empty() && tp.time <= tm){53                 ba += p[tp.index].num;54                 q.pop();55                 tp = q.top();56             }//回收操作57             if(p[i].num > ba) {58                 ans += p[i].num-ba;59                 ba = 0;60             }else ba -= p[i].num;//分配61         }62         printf("%d\n",ans);63     }64     return 0;65 }
View Code