PAT甲級-1014. Waiting in Line (30)(模擬)

kewlgrl發表於2018-03-10

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;
}

相關文章