http://pat.zju.edu.cn/contests/pat-practise/1014

来源:互联网 发布:js中绑定含参数函数 编辑:程序博客网 时间:2024/04/30 09:33
1014. Waiting in Line (30)
时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (NM+1)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, customer1 is served at window1 while customer2 is served at window2. Customer3 will wait in front of window1 and customer4 will wait in front of window2. Customer5 will wait behind the yellow line.

At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now. Customer2 will leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 at 08:10.

Input

Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (<=20, number of windows), M (<=10, the maximum capacity of each line inside the yellow line), K (<=1000, number of customers), and Q (<=1000, 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

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 51 2 6 4 3 534 23 4 5 6 7
Sample Output
08:0708:0608:1017:00Sorry
首先,值传递,1处注意修改,然后就是题目要求不能在5点之前服务的输出sorry,不是在5.00之前不能完成的。这个想当然了。

相当不爽啊!

[cpp] view plaincopy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<memory.h>  
  4. #include<algorithm>  
  5. #include<cstring>  
  6. #include<queue>  
  7. #include<cmath>  
  8. #include<vector>  
  9. #include<cstdlib>  
  10. #include<iomanip>  
  11. #include<string>  
  12. using namespace std;  
  13. #define MAX 0x6fffffff  
  14. struct Node{  
  15.     int need;  
  16.     int remain;  
  17.     int th;  
  18.     int donetime;  
  19. };  
  20. vector<Node> a[21];  
  21. Node nodes[1001];  
  22. int main(){  
  23.   
  24.     //freopen("in.txt", "r", stdin);  
  25.     int n,m,k,q;  
  26.     cin>>n>>m>>k>>q;  
  27.     for(int i=1;i<=k;++i){  
  28.         scanf("%d", &nodes[i].need);  
  29.         nodes[i].th = i;  
  30.         nodes[i].remain = nodes[i].need;  
  31.     }  
  32.   
  33.     int num = 1;  
  34.     for(int i=1;i<=m;++i)  
  35.         for(int j=1;j<=n;++j){  
  36.             a[j].push_back(nodes[num++]);  
  37.         }  
  38.   
  39.   
  40.     for(int t=0;1;){  
  41.         int go = MAX;  
  42.         bool flag = true;  
  43.         for(int j=1;j<=n;++j){  
  44.             if(a[j].size()>0 && go>a[j][0].remain){  
  45.                 go = a[j][0].remain;  
  46.                 flag = false;  
  47.             }  
  48.         }  
  49.         if(flag)  
  50.             break;  
  51.           
  52.         t += go;  
  53.         for(int j=1;j<=n;++j){  
  54.             if(a[j].size()>0){  
  55.                 a[j][0].remain -= go;  
  56.                 if(a[j][0].remain==0){    
  57.                     nodes[a[j][0].th].donetime = t;//一开始直接修改队列里的,没修改到nodes数组里的  
  58.                     a[j].erase(a[j].begin());     
  59.                     if(num<=k){  
  60.                         a[j].push_back(nodes[num++]);  
  61.                     }  
  62.                     while(a[j].size()>0 && t>=540)//删除开始时间不在17.00之前的  
  63.                         a[j].erase(a[j].begin());  
  64.                 }  
  65.             }  
  66.         }     
  67.     }  
  68.       
  69.     int query;  
  70.     for(int i=0;i<q;++i){  
  71.         scanf("%d", &query);  
  72.         if(nodes[query].donetime!=0){  
  73.             cout<<setfill('0')<<setw(2)<<8+nodes[query].donetime/60<<":"<<setw(2)<<setfill('0')<<nodes[query].donetime%60<<endl;  
  74.         }else  
  75.             cout<<"Sorry"<<endl;  
  76.     }  
  77.       
  78.       
  79.     //fclose(stdin);  
  80.     return 0;  
  81. }  
  82.           
原创粉丝点击