PAT-A1014 Waiting in Line 题目内容及题解

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 customer​2​​ 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

题目大意

顾客按照规则在银行排队,题目给出银行的窗口数、每个窗口允许的排队数以及顾客完成交易所需时间等条件进行队列模拟,要求对给定编号的顾客输出其完成交易的时间。

规则如下:

  • 有N个窗口,每个窗口有M个位置可供排队,全满后顾客需在黄线后排队等待;
  • 每位顾客在从黄线后进入排队时会选择最短的队列进行排队,如果有相同长度的队列,则选择编号最小的窗口进行排队;
  • 前N名客户将在8:00接受服务;
  • 17点后仍未开始接受服务的顾客将不会得到服务,输出“Sorry”。

解题思路

  1. 对窗口和顾客分别设置结构体,并初始化,对人记录开始服务时间、服务时间及服务结束时间,对窗口记录当前排队队列,下次队列处理时间,队中元素数;
  2. 模拟初次加队(按窗口号依次添加);
  3. 如仍有人排队则根据规则模拟依次将其安排进各队伍中;
  4. 输出所请求客人的服务结束时间(当17点后未开始服务的顾客将不会得到服务,但是17点前接受服务的顾客可以持续接受服务,结束服务时间不受限制);
  5. 服务结束,返回0值。

代码

#include<stdio.h>

#define MAXN 1010
#define last 540//结束时间,不包括 

struct Custom{
    int start;
    int dur;
    int end;
}cus[MAXN];//顾客 

struct Window{
    int queue[10];//队列 
    int front,rear;//队首出队,队尾入队 
    int nexttime;//下次队列处理时间 
    int num;//队中元素 
}win[25];

int M,N,K,Q;
int current;//当前进队顾客 
int T;//时间。 

void Init(){
    int i;
    scanf("%d%d",&N,&M);//N窗口数,M窗口排队数
    for(i=0;i<N;i++){
        win[i].nexttime=0;
        win[i].front=0;
        win[i].rear=0;
        win[i].num=0;
    }
    scanf("%d%d",&K,&Q);//K顾客数,Q询问数 
    for(i=1;i<=K;i++){
        scanf("%d",&cus[i].dur);
        cus[i].start=-1;
        cus[i].end=-1;//表明未服务
    }
    return;
}//初始化 

void Push(int L,int C){
    win[L].queue[win[L].rear]=C;
    win[L].rear=(win[L].rear+1)%M;
    win[L].num++;
    return;
}//加队 

void Op(int L){
    win[L].queue[win[L].front]=0;
    win[L].front=(win[L].front+1)%M;//队头元素指向改变 
    win[L].num--;//队列数减一 
    win[L].nexttime+=cus[win[L].queue[win[L].front]].dur;//队列L的下一次执行过程时间 
    cus[win[L].queue[win[L].front]].start=T;//更新顾客开始时间 
    cus[win[L].queue[win[L].front]].end=win[L].nexttime;//更新顾客结束时间
    if(current<K){
        current++;
        Push(L,current);
    }//若仍有未排队顾客则使其进队
    if(win[L].num==0){
        win[L].nexttime=last;
    }//队空,则将其置于结束位置。 
    
    return;
}

void Init_0(){
    int i,j;
    current=0;
    for(i=0;i<M;i++){//轮次 
        for(j=0;j<N;j++){//队名 
            Push(j,++current);//初始时一次压入
            if(i==0){//第一轮 
                win[j].nexttime+=cus[current].dur;//队列下一次执行时间 
                cus[current].start=T;//顾客服务开始时间 
                cus[current].end=win[j].nexttime;//顾客服务结束时间 
            }
            if(current>=K){
                return; 
            }
        }
    }
}//初始加队 

void print(int num){
    int t;
    if(cus[num].end==-1){
        printf("Sorry\n");
    }else{
        t=cus[num].end;
        printf("%02d:%02d\n",((t/60)+8),(t%60));
    }
}

int main(){
    int i,query;
    Init();
    Init_0();
    for(T=0;T<last;T++){
        for(i=0;i<N;i++){
            if(T>=win[i].nexttime){
                Op(i);//对队列进行操作 
            }
        }
    }
    for(i=0;i<Q;i++){
        scanf("%d",&query);
        print(query);
    } 
    return 0;
} 

运行结果

PAT-A1014 Waiting in Line 题目内容及题解