PAT (Advanced Level) 1014 Waiting in Line

题解

  排队模拟。需要注意的是,如果一个人在17:00及以后还没有开始服务,这种情况才输出“Sorry”。一个人在17:00之前已经开始了服务,就算结束服务在17:00之后,这样也是没问题的。

代码

#include<bits/stdc++.h>
using namespace std;
const int inf = 0x7fffffff;
struct node
{
    int num,finish_time;
    queue<int> people;
}windows[25];
bool cmp_quesize(node x,node y)
{
    if(x.people.size()!=y.people.size())
        return x.people.size()<y.people.size();
    else
        return x.num<y.num;
}
bool cmp_finish(node x,node y)
{
    if(x.finish_time!=y.finish_time)
        return x.finish_time<y.finish_time;
    else
        return x.num<y.num;
}
int N,M,K,Q,service_time[1005],servicing,ans[1005];
void POP();
void PUSH(int p);
void print(int p);
int main()
{
    int i,q;
    scanf("%d%d%d%d",&N,&M,&K,&Q);
    for(i=1;i<=K;i++) scanf("%d",&service_time[i]);
    for(i=1;i<=N;i++) windows[i].num=i,windows[i].finish_time=inf;
    for(i=1;i<=K;i++)
    {
        if(servicing==N*M)
            POP();
        PUSH(i);
    }
    while(servicing) POP();
    for(i=0;i<Q;i++)
    {
        scanf("%d",&q);
        print(q);
    }
    system("pause");
    return 0;
}
void print(int p)
{
    int sum=8*60+ans[p];
    if(ans[p]-service_time[p]>=540)  printf("Sorry
");
    else    printf("%02d:%02d
",sum/60,sum%60);
}
void PUSH(int p)
{
    servicing++;
    sort(windows+1,windows+N+1,cmp_quesize);
    windows[1].people.push(p);
    if(windows[1].finish_time==inf) windows[1].finish_time=service_time[p];
}
void POP()
{
    int one,two=0;
    servicing--;

    sort(windows+1,windows+N+1,cmp_finish);

    one=windows[1].people.front();
    windows[1].people.pop();
    ans[one]=windows[1].finish_time;
    if(!windows[1].people.empty())
    {
        two=windows[1].people.front();
        windows[1].finish_time+=service_time[two];
    }
    else    windows[1].finish_time=inf;
}