PAT (Advanced Level) Practice 1014 Waiting in Line (30 point(s))

题目: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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值