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:
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.
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.
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.
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
08:07 08:06 08:10 17:00 Sorry
题意:假设有一个银行有N个窗口用于服务。有一根黄线将等待区分成两部分。
顾客在线内等待的规则如下:
1、黄线到窗口的空间足够容纳M个顾客。所以当这N个窗口的队伍都排满时,
所有在(N*M+1)之后(包含N*M+1)的顾客将会在黄线后等待
2、每个顾客将会选择最短的队伍排队,如果有两支及以上队伍人数一样少,
则选择数字小的队伍
3、顾客i会花费Ti的时间用于交易
4、最开始的N个顾客假设为在8:00开始交易
现给出每个顾客的交易时间,你需要求出某个顾客完成此次交易所需的时间
分析:一道模拟题
代码如下:
1 #include <iostream>
2 #include <queue>
3 using namespace std;
4 struct node
5 {
6 int begin_time,done_time;
7 };
8 int N,M,K,Q;
9 int customer[1005];
10 queue<int> window[20];
11 int window_time[20];
12 node customer_time[1005];
13 bool AllWindowEmpty()
14 {
15 for(int i=0;i<N;i++)
16 {
17 if(!window[i].empty()) return false;
18 }
19 return true;
20 }
21 int main()
22 {
23 cin>>N>>M>>K>>Q;
24 for(int i=1;i<=K;i++)
25 {
26 scanf("%d",&customer[i]);
27 }
28 for(int i=0;i<20;i++)
29 {
30 window_time[i]=480;
31 }
32 int now=1;
33 while(now<=K)
34 {
35 for(int i=0;i<N;i++)
36 {
37 if(now>K) break;
38 window[i].push(now);
39 now++;
40 }
41 int cnt=0;
42 for(int i=0;i<N;i++)
43 {
44 if(window[i].size()==M) cnt++;
45 }
46 if(cnt==N) break;
47 }
48 while(now<=K||!AllWindowEmpty())
49 {
50 int earlytime=1050;
51 int finish_customer;
52 int finish_window;
53 for(int i=0;i<N;i++)
54 {
55 if(!window[i].empty()&&(window_time[i]+customer[window[i].front()])<earlytime)
56 {
57 earlytime=window_time[i]+customer[window[i].front()];
58 finish_window=i;
59 }
60 }
61 finish_customer=window[finish_window].front();
62 window[finish_window].pop();
63 if(now<=K)
64 {
65 window[finish_window].push(now);
66 now++;
67 }
68 customer_time[finish_customer].done_time=window_time[finish_window]+customer[finish_customer];
69 customer_time[finish_customer].begin_time=window_time[finish_window];
70 window_time[finish_window]=customer_time[finish_customer].done_time;
71
72 }
73 bool flag=false;
74 for(int i=0;i<Q;i++)
75 {
76 int quere;
77 scanf("%d",&quere);
78 int begin_time=customer_time[quere].begin_time;
79 int done_time=customer_time[quere].done_time;
80 if(flag==false) flag=true;
81 else printf("\n");
82 if(begin_time>=1020)
83 printf("Sorry");
84 else
85 printf("%02d:%02d",done_time/60,done_time%60);
86 }
87 return 0;
88 }
原文:https://www.cnblogs.com/jiongyy0570/p/10448132.html