PAT 1014 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.
  • Customeri will take Ti 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 transacitions 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 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个窗口前的黄线内都排满队时,自第NM+1个顾客起的所有人都需在黄线外等待。
  • 每名顾客在进入黄线区域时都会选择当前最短的队伍。如果有多条队伍长度相同,顾客总会选择编号最小的窗口。
  • 第i名顾客需要花费Ti的时间来完成他的业务。
  • 前N名顾客假设在早上8点整被接待。

现给出每名顾客的业务处理时间,请给出每名顾客完成业务的准确时间。
输入的第一行包含4个正整数N,M,K和Q,分别表示窗口数、每列黄线能容纳的最大人数、顾客数和待查询的顾客数。下一行包含K个正整数,分别表示K名顾客的处理时间。最后一行包含Q个正整数,表示询问他们的处理时间的顾客编号。顾客从1到K编号。
对于Q名顾客中的每一位,按HH:MM的格式输出他业务完成的具体时间。银行在17:00关门,因此如果顾客在17:00后获得服务,则输出Sorry

题解

银行排队,可直接用队列模拟。
建立N个队列代表银行的N个窗口,队列存储顾客的处理时间和编号,然后用一个长度为N的数组来存储N个窗口当前的时间。将前NM个顾客的处理时间和编号分别入队,然后遍历所有队列,找到所有队首中当前时间加处理时间最小的队伍,将其队首出队,并将下一个顾客入队,以此来模拟黄线外顾客的等待过程。当黄线外没有顾客时,将所有队列中的元素全部出队即可。
需要注意的是,如果顾客排队的时间是在17:00之后,该顾客将无法获得服务。而如果顾客在17点前入队但完成服务后时间超过了17点,顾客仍能正常获得服务。

C语言代码如下:

#include
#include
#include

#define INF -2147483648

typedef struct element_type
{
    int min;
    int number;
}element_type;


typedef struct Queue_element{
    element_type val;
    struct Queue_element *next;
}queue_element;

typedef struct Queue{
    queue_element *front;    
    queue_element *rear;
    int num;
}queue_t;

int is_empty(queue_t* queue){
    if(!queue->front || !queue->rear)
        return 1;
    else
        return 0;
}

element_type dequeue(queue_t* queue){
    if(is_empty(queue))
        return (element_type){INF, INF};
    else{
        element_type ret = queue->front->val;
        queue_element* tmp = queue->front;
        queue->front = tmp->next;
        free(tmp);
        queue->num--;
        return ret;
    }
}

void enqueue(queue_t* queue, element_type num){
    queue_element* new_element = (queue_element*) malloc(sizeof(queue_element));
    new_element->next = NULL;
    new_element->val = num;
    queue->num++;
    if(is_empty(queue)){
        queue->front = new_element;
        queue->rear = new_element;
        return;
    }
    queue->rear->next = new_element;
    queue->rear = new_element;
}

queue_t* create_empty_queue(){
    queue_t* ret = (queue_t*) malloc(sizeof(queue_t));
    ret->front = NULL;
    ret->rear = NULL;
    ret->num = 0;
    return ret;
}

int n, m, k, q;

int main(){
    scanf("%d %d %d %d", &n, &m, &k, &q);
    int *customer = malloc(sizeof(int) * k);
    int *window_t = malloc(sizeof(int) * n);    //表示第i个窗口当前的时间
    int *result = malloc(sizeof(int) * k);
    memset(window_t, 0, sizeof(int) * n);
    queue_t** queues = malloc(sizeof(queue_t*) * n);
    for(int i = 0;i < n;++i)
        queues[i] = create_empty_queue();
    for(int i = 0;i < k;++i){
        scanf("%d", customer + i);
        if(queues[i % n]->num < m){ //黄线内未满时依次排好队
            enqueue(queues[i % n], (element_type){customer[i], i});
        }
    }
    int i = n * m;
    while(i < k){   //黄线内已满时,找到最先完成业务的窗口
        int minn = 1000;
        int index;
        for(int j = 0;j < n;++j){
            if(!is_empty(queues[j]) && queues[j]->front->val.min + window_t[j] < minn){
                minn = queues[j]->front->val.min + window_t[j];
                index = j;
            }
        }
        window_t[index] = minn;
        if(window_t[index] >= 540)
            window_t[index] = 3000;
        int number = dequeue(queues[index]).number;
        result[number] = minn;
        enqueue(queues[index], (element_type){customer[i], i});
        i++;
    }
    for(int j = 0;j < n;++j){
        while(!is_empty(queues[j])){
            element_type tmp = dequeue(queues[j]);
            window_t[j] += tmp.min;
            result[tmp.number] = window_t[j];
            if(window_t[j] >= 540)
                window_t[j] = 3000;
        }
    }
    int query;
    for(int j = 0;j < q;++j){
        scanf("%d", &query);
        query--;
        if(result[query] >= 3000){
            printf("Sorry\n");
        }else{
            printf("%02d:%02d\n", 8 + result[query] / 60, result[query] % 60);
        }
    }
    return 0;
}

你可能感兴趣的:(PAT 1014 Waiting in Line)