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<stdio.h>
#include<stdlib.h>
#include<string.h>

#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;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,738评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,377评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,774评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,032评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,015评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,239评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,724评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,374评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,508评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,410评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,457评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,132评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,733评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,804评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,022评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,515评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,116评论 2 341

推荐阅读更多精彩内容