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\n");
    else    printf("%02d:%02d\n",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;
}

原文地址:https://www.cnblogs.com/VividBinGo/p/12237301.html

时间: 2024-07-30 16:20:36

PAT (Advanced Level) 1014 Waiting in Line的相关文章

PAT (Advanced Level) 1014. Waiting in Line (30)

简单模拟题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> using namespace std; struct X { int st; int len; int en; }p[1500]; queue<int>Q[2

1014. Waiting in Line (30)——PAT (Advanced Level) Practise

题目信息: 1014. Waiting in Line (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 rule

PAT 甲级 1014 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

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a lon

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c