PAT甲级-1014. Waiting in Line (30)(模拟)

1014. Waiting in Line (30)

时间限制
400 ms
内存限制
65536 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 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 customers 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 Input
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7
Sample Output
08:07
08:06
08:10
17:00
Sorry


题目意思:

银行早晨8点上班,17点结束接待窗口以外的客户。

给出银行窗口数目N、每个窗口前黄线内允许等待的人数M、K个客户处理各自业务需

要的时间、查询的Q个客户编号。

输出客户办理完业务的时间,如果不能办理就输出Sorry。


解题思路:

1、所有能进黄线的客户人数是N*M,其他客户在外面等着;

2、当有客户处理完毕,黄线内有空时,黄线外等待的客户挑选窗口的顺序是:等待人

数最少、时间最早、编号最小;

3、已经在黄线内的客户不能到别的窗口前排队;

4、客户只要是在17点之前坐到窗口就可以办理业务,而不是业务结束时间在17点之

前,也就是说WindowStartTime>=17:00时,输出Sorry。


#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 1010
struct Node
{
    int no,sh,sm,eh,em;//客户编号、花费时间、结束时间
    bool vis;//是否处理完毕
} t[MAXN];//客户
struct Win
{
    int no,wh=8,wm=0,num=0;//窗口编号、营业时间、当前客户数
    queue<Node>que;//每个窗口的队伍
} w[20];//窗口
bool cmp(Win a,Win b)//注意窗口排序关键字
{
    if(a.num!=b.num) return a.num<b.num;
    else if(a.wh!=b.wh) return a.wh<b.wh;
    else if(a.wm!=b.wm) return a.wm<b.wm;
    return a.no<b.no;
}
bool flagEmpty(int n)//所有窗口业务是否都处理完毕
{
    for(int i=0; i<n; ++i)
        if(!w[i].que.empty()) return false;
    return true;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("D:/x/read.txt","r",stdin);
    //freopen("D:/x/out.txt","w",stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    int n,m,k,Q;//窗口、黄线、客户、查询
    cin>>n>>m>>k>>Q;
    for(int i=0; i<k; ++i)
    {
        int x;
        cin>>x;
        t[i].sh=x/60;
        t[i].sm=x%60;
        t[i].vis=true;
        t[i].no=i;//客户编号
    }
    for(int i=0; i<n; ++i) w[i].no=i;
    int cnt=0,wait=k;//已受理和等待的服务人数
    while(1)
    {
        if(cnt==k&&flagEmpty(n)) break;//黄线后没有客户等待且所有窗口业务都处理完毕
        sort(w,w+n,cmp);//按窗口处理结束时间排序
        for(int j=0; j<m; ++j) //黄线内
            for(int i=0; i<n; ++i) //让黄线后的客户去窗口排队
                if(w[i].que.size()<m&&wait>0)
                {
                    w[i].que.push(t[cnt]);
                    ++cnt,--wait,++w[i].num;
                    if(cnt==k) goto A;//黄线后没有客户等待
                }
A:
        for(int i=0; i<n; ++i) //处理各个窗口的客户业务
        {
            if(w[i].que.empty()) continue;
            Node temp=w[i].que.front();
            w[i].que.pop();
            int no=temp.no;//获取客户编号
            --w[i].num;
            if(w[i].wh>=17||w[i].wh<8) t[no].vis=false;//不在服务时间内
            else
            {
                t[no].eh=w[i].wh+temp.sh+(w[i].wm+temp.sm)/60;
                t[no].em=(w[i].wm+temp.sm)%60;
                w[i].wh=t[no].eh;
                w[i].wm=t[no].em;
            }
        }
    }
    while(Q--)//查询
    {
        int q;
        cin>>q;
        --q;
        if(!t[q].vis) puts("Sorry");
        else printf("%02d:%02d\n",t[q].eh,t[q].em);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值