本文主要是介绍【PAT1014】 Waiting in Line (30) queue模拟排队,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 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 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
银行分多窗口进行排队进行业务办理,求客户的结束时间。
分析:
可以纯粹使用queue进行过程模拟。
代码:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
using namespace std;//此代码使用前,需删除下面两行+后面的system("PAUSE")
ifstream fin("in.txt");
#define cin finconst int closeTime = 17;
const int closeTimeInt = 17*60;struct Window{int hour; //窗口的每次服务的开始时间int minute;int winEnd; //每次服务完后的时间整型queue<int> que; //窗口队列Window(){hour=8;minute=0;}
}win[20];queue<int> waiting; //黄线外等待队列
int cusTime[1001]={0}; //每个客户的业务办理时间struct FinishTime{ //每个客户的结束时间int hour;int minute;
}finishTime[1001];int main()
{int n,m,k,q;cin>>n>>m>>k>>q;int i;for(i=1;i<k+1;i++){cin>>cusTime[i];}int insideNum,waitNum;if(n*m >= k){insideNum = k;waitNum = 0;}else{insideNum = n*m;waitNum = k-insideNum;}for(i=0;i<insideNum;i++){ //根据先后把人安排到黄线内的对应窗口win[i%n].que.push(i+1);}int j,earlyFlag,cust;int endTime = 0x7fffffff;if(waitNum){for(i=insideNum;i<k;i++){ //把剩下的人放入等待队列waiting.push(i+1);}for(j=0;j<n;j++){ //预处理每个窗口的第一个客户,找出最早结束的一个cust = win[j].que.front();finishTime[cust].hour = win[j].hour + (win[j].minute+cusTime[cust])/60;finishTime[cust].minute = (win[j].minute+cusTime[cust])%60;win[j].hour = finishTime[cust].hour;win[j].minute = finishTime[cust].minute;win[j].winEnd = finishTime[cust].hour*100 + finishTime[cust].minute;if(win[j].winEnd < endTime){endTime = win[j].winEnd;earlyFlag = j;}}do{win[earlyFlag].que.pop();//循环执行{最早结束的一个出列;等待队列补上;找当前每窗口第一个客户中最早结束的一个;}直到等待队列为空;此时所有人都在窗口队列里了。win[earlyFlag].que.push(waiting.front());waiting.pop();cust = win[earlyFlag].que.front();finishTime[cust].hour = win[earlyFlag].hour + (win[earlyFlag].minute+cusTime[cust])/60;finishTime[cust].minute = (win[earlyFlag].minute+cusTime[cust])%60;win[earlyFlag].hour = finishTime[cust].hour;win[earlyFlag].minute = finishTime[cust].minute;win[earlyFlag].winEnd = finishTime[cust].hour*100 + finishTime[cust].minute;endTime = 0x7fffffff;for(j=0;j<n;j++){if(win[j].winEnd < endTime){endTime = win[j].winEnd;earlyFlag = j;}}}while(!waiting.empty());}for(i=0;i<n;i++){if(waitNum)win[i].que.pop(); //如果之前有等的,那窗口的第一个客户时间已经计算过,直接出列即可while(!win[i].que.empty()){ //对每个窗口队列的人依次进行时间计算cust = win[i].que.front();finishTime[cust].hour = win[i].hour + (win[i].minute+cusTime[cust])/60;finishTime[cust].minute = (win[i].minute+cusTime[cust])%60;win[i].hour = finishTime[cust].hour;win[i].minute = finishTime[cust].minute;win[i].que.pop();}}int queryID;for(i=0;i<q;i++){cin>>queryID;if(finishTime[queryID].hour >= closeTime){if(finishTime[queryID].hour*60 + finishTime[queryID].minute - cusTime[queryID] >= closeTimeInt){ //算开始时间是否在关门点后cout<<"Sorry"<<endl;continue;}}cout<<setw(2)<<setfill('0')<<finishTime[queryID].hour<<":"<<setw(2)<<setfill('0')<<finishTime[queryID].minute<<endl;}system( "PAUSE");return 0;
}
代码看起来有些臃肿,但竟然一次就AC了。
纯粹的模拟算法实现,看起来很容易理解。
更简练的实现可以参见 http://blog.csdn.net/huajh7/article/details/7918144
这篇关于【PAT1014】 Waiting in Line (30) queue模拟排队的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!