【PAT】A1014 Waiting in Line (30分)


作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB

A1014 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 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 custmers 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, customer​1​​ is served at window​1​​ while customer2​​ is served at window​2​​ . Customer​3​​ will wait in front of window​1​​ and customer​4​​ will wait in front of window​2​​ . Customer​5​​ will wait behind the yellow line.

At 08:01, customer​1​​ is done and customer​5​​ enters the line in front of window​1​​ since that line seems shorter now. Customer​2​​ will leave at 08:02, customer​4​​ at 08:06, customer​3​​ at 08:07, and finally customer​5​​ at 08:10.

Input Specification:

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 Specification:

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

Code

#include <bits/stdc++.h>
using namespace std;
int wtime[1010],remaintime[1010],fintime[1010];
queue<int> qi[25];
int main(){
    int n,m,k,q,query,index=1;
    cin>>n>>m>>k>>q;
    for(int i=1;i<=k;i++){
        cin>>wtime[i];
        remaintime[i]=wtime[i];
        fintime[i]=1e9;
    }
    int num=min(n*m,k);
    for(int i=1;i<=num;i++){
        int x=i%n;
        if(x==0)    x=n;
        qi[x].push(i);
    }
    if(k>=n*m+1)    for(int i=n*m+1;i<=k;i++)   qi[n+1].push(i);    // 黄线后的队伍
    int T=0;		// T用于表示当前的时间(s)
    while(1){
        int mint=1e9;
        int allempty=1;
        for(int i=1;i<=n;i++){
            if(!qi[i].empty()){		// 如果第i号窗口队伍非空
                mint=min(mint,remaintime[qi[i].front()]);	// 找到所有队伍中最先办理好业务的人还需的时间
                allempty=0;
            }
        }
        if(allempty==1) break;		// 如果全部窗口前队伍都为空,说明处理完毕
        T+=mint;
        for(int i=1;i<=n;i++){
            if(!qi[i].empty()){
                remaintime[qi[i].front()]-=mint;			// 同时办理的其他窗口的人还需的时间减去mint
                if(remaintime[qi[i].front()]==0){			// 若发现减去mint后业务办完
                    fintime[qi[i].front()]=T;				// 标记结束时间
                    qi[i].pop();							// 出队
                    if(!qi[n+1].empty()){					// 若黄线外还有人
                        int temp=qi[n+1].front();
                        qi[n+1].pop();
                        qi[i].push(temp);
                    }
                }
            }
        }
        if(T>=540){		// 若此时已经过了17:00,把没办完的办完,其他人就不处理了
            for(int i=1;i<=n;i++){
                if(!qi[i].empty()){
                    int temp=qi[i].front();
                    if(remaintime[temp]!=wtime[temp])	// 已经开始办了
                        fintime[temp]=T+remaintime[temp];
                }
            }
            break;
        }
    }
    for(int i=0;i<q;i++){
        cin>>query;
        if(fintime[query]==1e9) printf("Sorry\n");
        else    printf("%02d:%02d\n",fintime[query]/60+8,fintime[query]%60);
    }
	return 0;
}

Analysis

-模拟银行排队。银行来了K个客户,银行有N个窗口,每个窗口前有M个位置排队,客户选择最短的队伍排,根据每个客户的序号和服务时间来确定最后客户离开银行的时间。

-先把前N*M个客户排好位置,剩余的人排在黄线外。

-注意:给客户的服务,如果在17点前就开始了,则给他服务完。换句话说,就是不接收17点之后才开始的服务,17点前的服务做完再下班。

发布了159 篇原创文章 · 获赞 17 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/ztmajor/article/details/103840309