PAT甲級-1014. Waiting in Line (30)(模擬)
1014. Waiting in Line (30)
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 customers 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 Input2 2 7 5 1 2 6 4 3 534 2 3 4 5 6 7Sample Output
08:07 08:06 08:10 17:00 Sorry
題目意思:
銀行早晨8點上班,17點結束接待視窗以外的客戶。
給出銀行視窗數目N、每個視窗前黃線內允許等待的人數M、K個客戶處理各自業務需
要的時間、查詢的Q個客戶編號。
輸出客戶辦理完業務的時間,如果不能辦理就輸出Sorry。
解題思路:
1、所有能進黃線的客戶人數是N*M,其他客戶在外面等著;
2、當有客戶處理完畢,黃線內有空時,黃線外等待的客戶挑選視窗的順序是:等待人
數最少、時間最早、編號最小;
3、已經在黃線內的客戶不能到別的視窗前排隊;
4、客戶只要是在17點之前坐到視窗就可以辦理業務,而不是業務結束時間在17點之
前,也就是說WindowStartTime>=17:00時,輸出Sorry。
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 1010
struct Node
{
int no,sh,sm,eh,em;//客戶編號、花費時間、結束時間
bool vis;//是否處理完畢
} t[MAXN];//客戶
struct Win
{
int no,wh=8,wm=0,num=0;//視窗編號、營業時間、當前客戶數
queue<Node>que;//每個視窗的隊伍
} w[20];//視窗
bool cmp(Win a,Win b)//注意視窗排序關鍵字
{
if(a.num!=b.num) return a.num<b.num;
else if(a.wh!=b.wh) return a.wh<b.wh;
else if(a.wm!=b.wm) return a.wm<b.wm;
return a.no<b.no;
}
bool flagEmpty(int n)//所有視窗業務是否都處理完畢
{
for(int i=0; i<n; ++i)
if(!w[i].que.empty()) return false;
return true;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("D:/x/read.txt","r",stdin);
//freopen("D:/x/out.txt","w",stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
int n,m,k,Q;//視窗、黃線、客戶、查詢
cin>>n>>m>>k>>Q;
for(int i=0; i<k; ++i)
{
int x;
cin>>x;
t[i].sh=x/60;
t[i].sm=x%60;
t[i].vis=true;
t[i].no=i;//客戶編號
}
for(int i=0; i<n; ++i) w[i].no=i;
int cnt=0,wait=k;//已受理和等待的服務人數
while(1)
{
if(cnt==k&&flagEmpty(n)) break;//黃線後沒有客戶等待且所有視窗業務都處理完畢
sort(w,w+n,cmp);//按視窗處理結束時間排序
for(int j=0; j<m; ++j) //黃線內
for(int i=0; i<n; ++i) //讓黃線後的客戶去視窗排隊
if(w[i].que.size()<m&&wait>0)
{
w[i].que.push(t[cnt]);
++cnt,--wait,++w[i].num;
if(cnt==k) goto A;//黃線後沒有客戶等待
}
A:
for(int i=0; i<n; ++i) //處理各個視窗的客戶業務
{
if(w[i].que.empty()) continue;
Node temp=w[i].que.front();
w[i].que.pop();
int no=temp.no;//獲取客戶編號
--w[i].num;
if(w[i].wh>=17||w[i].wh<8) t[no].vis=false;//不在服務時間內
else
{
t[no].eh=w[i].wh+temp.sh+(w[i].wm+temp.sm)/60;
t[no].em=(w[i].wm+temp.sm)%60;
w[i].wh=t[no].eh;
w[i].wm=t[no].em;
}
}
}
while(Q--)//查詢
{
int q;
cin>>q;
--q;
if(!t[q].vis) puts("Sorry");
else printf("%02d:%02d\n",t[q].eh,t[q].em);
}
return 0;
}
相關文章
- PAT甲級-1140. Look-and-say Sequence (20)(模擬)
- 【PAT甲級A1038】Recover the Smallest Number (30分)(c++)C++
- PAT甲級1032 Sharing
- PAT甲級1030 Travel Plan
- 浙大PAT甲級考試
- PAT甲級1023 Have Fun with Number
- (非原創)PAT甲級1123 Is It a Complete AVL Tree (30分)|C++實現C++
- PAT 甲級 1152 Google Recruitment (20分)GoUI
- 20年春季甲級pat考試
- PAT甲級-1015. Reversible Primes (20)
- PAT甲級1126~1130|C++實現C++
- 【PAT A1051】【棧/模擬】
- PAT-B 1085 PAT單位排行【模擬】
- PAT甲級真題1069 數字黑洞(巧妙解法)
- PAT甲級考試題庫題目分類
- 【PAT甲級A1084】Broken Keyboard (20分)(c++)C++
- PTA甲級 1076 Forwards on Weibo (30分)Forward
- 2024 秋季PAT認證甲級(題解A1-A4)
- PAT甲級1122 Hamiltonian Cycle (25分)|C++實現C++
- PAT甲級1154 Vertex Coloring (25分)|C++實現C++
- 2021.9.12週六PAT甲級考試覆盤與總結
- PAT甲級1110 Complete Binary Tree (25分)|C++實現C++
- 19年春季第二題 PAT甲級 1157 Anniversary(25 分)
- PAT-B 1027 列印沙漏 【模擬】
- PAT-B 1058 選擇題 【模擬】
- PAT-B 1061 判斷題【模擬】
- PAT-B 1067 試密碼【模擬】密碼
- PAT-B 1071 小賭怡情【模擬】
- 菜鳥記錄:c語言實現PAT甲級1010--RadixC語言
- 【PAT甲級A1065】A+B and C (64bit) (20分)(c++)C++
- 2020年7月第2題 PAT甲級真題 The Judger (25分)
- PAT-B 1054 求平均值 【模擬】
- PAT-B 1072 開學寄語【模擬】
- PAT-B 1081 檢查密碼【模擬】密碼
- PAT-B 1084 外觀數列 【模擬】
- PAT-B 1018 錘子剪刀布 【模擬】
- PAT(甲級)2020年秋季考試 7-1 Panda and PP Milk (20分)
- PAT-B 1019 數字黑洞【陣列+模擬】陣列