题目:PAT A 1014
题目大意:
银行有N
个窗口,每个窗口的队列可以排M
人,有K
个人依次来银行服务,并且给出每个人服务所需的时间。给出q
系询问。问询问人的服务结束时间
解题思路
- 模拟
- 定义
Windows
表示窗口,Customer
表示客户
struct Window{
int poptime;//表示当前服务的人弹出队列的时间
int endtime;//到某个人时,队列的结束时间,动态变化的
int queue[11];//最多可以排队M人,不超过10个
int front,rear,length;//队列的头指针,尾指针,排队人数
};
struct Customer{
int pushtime;//入队时间=某个队列的结束时间
int time;//服务所需时间
};
- 在模拟的过程中当队列为满时(
i<=m*n
),可以继续入队。否则,就需要弹出当前的队头元素,并开始服务下一个。所以,poptime
需要加上。
#include<bits/stdc++.h>
using namespace std;
struct Window{
int poptime;
int endtime;
int queue[11];
int front,rear,length;
};
struct Customer{
int pushtime;
int time;
};
int main(){
int n,m,k,q;
scanf("%d%d%d%d",&n,&m,&k,&q);
Window windows[21]={0};
Customer customer[1001]={0};
int push,query;
for(int i=1;i<=k;++i){
scanf("%d",&customer[i].time);
if(i<=m*n){
push=push%n;
push++;
}else{
push=1;
for(int j=1;j<=n;++j){
if(windows[j].poptime<windows[push].poptime) push=j;
}
}
//当队列为空时
if(windows[push].length==0) windows[push].poptime=customer[i].time;
//当队列满时,需要弹出
if(windows[push].length==m){
windows[push].front++,windows[push].front%=11;
windows[push].length--;
windows[push].poptime+=customer[windows[push].queue[windows[push].front]].time;//处理下一位
}
customer[i].pushtime=windows[push].endtime;
windows[push].endtime+=customer[i].time;
windows[push].queue[windows[push].rear]=i;
windows[push].rear++,windows[push].rear%=11;
windows[push].length++;
}
for(int i=0;i<q;++i){
scanf("%d",&query);
int tmp=customer[query].pushtime+customer[query].time;
if(customer[query].pushtime>=(17-8)*60) printf("Sorry\n");
else printf("%02d:%02d\n",tmp/60+8,tmp%60);
}
system("pause");
return 0;
}