PTA-1014——Waiting in Line
Posted orangecyh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PTA-1014——Waiting in Line相关的知识,希望对你有一定的参考价值。
题目:
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:
- The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (st one will have to wait in a line behind the yellow line.
- Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
- Customer?i?? will take T?i?? minutes to have his/her transaction processed.
- The first N customers are assumed to be served at 8:00am.
Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.
For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer?1?? is served at window?1?? while customer?2?? is served at window?2??. Customer?3?? will wait in front of window?1?? and customer?4?? will wait in front of window?2??. Customer?5?? will wait behind the yellow line.
At 08:01, customer?1?? is done and customer?5?? enters the line in front of window?1?? since that line seems shorter now. Customer?2?? will leave at 08:02, customer?4?? at 08:06, customer?3?? at 08:07, and finally customer?5?? at 08:10.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (≤, number of windows), M (≤, the maximum capacity of each line inside the yellow line), K (≤, number of customers), and Q (≤, number of customer queries).
The next line contains K positive integers, which are the processing time of the K customers.
The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.
Output Specification:
For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM
where HH
is in [08, 17] and MM
is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served before 17:00, you must output Sorry
instead.
Sample Input:
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
Sample Output:
08:07
08:06
08:10
17:00
Sorry
分析:
队列。
一个注意点:服务开始时间>=17:00才不服务,而不是结束时间.
代码:
1 #include<iostream> 2 #include<queue> 3 #include<cstring> 4 using namespace std; 5 int n,m,k,q; 6 int need[1001]; //每个客户需要的时间 7 int finish[1001]; //每个客户的结束时间 8 queue<int> win[21]; //窗口的队伍 9 int now[1001]; //每个窗口的时间 10 int main(){ 11 cin>>n>>m>>k>>q; 12 for(int i=1;i<=k;i++){ 13 cin>>need[i]; 14 } 15 memset(now,0,sizeof(now)); 16 for(int i=1;i<=k;i++){ //所有的客户都安排进窗口 17 if(i<=n){ 18 win[i].push(i); 19 now[i]=need[i]; //这个窗口第一个客户,当前时间设置为第一个客户结束的时间 20 }else if(i<=n*m){ //黄线内有空位 21 win[(i-1)%n+1].push(i); 22 }else{ //黄线外依次替换 23 int earlist=1000000; //下一个最早结束的时间 24 int point=0; //结束的客户所在的窗口位置 25 for(int j=1;j<=n;j++){ //找出最早结束的窗口 26 if(now[j]<earlist){ 27 earlist=now[j]; 28 point=j; 29 } 30 } 31 int customer=win[point].front(); //结束的客户编号 32 win[point].pop(); //删除服务结束的客户 33 win[point].push(i); //后面的客户进入黄线内 34 finish[customer]=now[point]; //记录客户服务结束的时间 35 now[point]+=need[win[point].front()]; //时间挪到该窗口下一个客户结束的时间 36 } 37 } 38 for(int i=1;i<=n;i++){ //最后剩下的黄线内的人进行清空 39 while(win[i].size()>0){ 40 int customer=win[i].front(); 41 win[i].pop(); 42 finish[customer]=now[i]; 43 now[i]+=need[win[i].front()]; 44 } 45 } 46 for(int i=0;i<q;i++){ 47 int query; 48 cin>>query; 49 if(finish[query]-need[query]>=540){ //若开始办理的时间大于等于540分钟,即超过了17:00 50 cout<<"Sorry"<<endl; 51 }else{ //否则输出时间 52 int a=8+finish[query]/60; 53 int b=finish[query]%60; 54 if(a<10){ 55 cout<<"0"<<a<<":"; 56 }else{ 57 cout<<a<<":"; 58 } 59 if(b<10){ 60 cout<<"0"<<b<<endl; 61 }else{ 62 cout<<b<<endl; 63 } 64 } 65 } 66 return 0; 67 }
以上是关于PTA-1014——Waiting in Line的主要内容,如果未能解决你的问题,请参考以下文章
1014. Waiting in Line (30)(模拟题)