PTA A1014

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.
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 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, 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. Customer 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

單詞

transaction

英 /træn'zækʃ(ə)n; trɑːn-; -'sæk-/ 美 /træn'zækʃən/

n. 交易;事務;辦理;會報,學報

題目分析

這道題我差不多前前後後做了四遍,給了我一些教訓,首先是30分題一定要提前規劃好程序,解決這個問題我們需要哪些數據,說出來有點尷尬,一開始這題我甚至沒有用隊列,另外一定要仔細讀題,是17:00之後來的客人不服務,17:00之前來的客人就算是到半夜也要服務完。另外是Sorry,不是sorry!
這題其實對我來說還是很有難度的,主要是不知道怎麼把時間抽象出來,每一個窗口的每一個人服務時間都不同,用什麼來保存,怎麼保存每個人進入窗口的時間,很多問題。
這道題我最後用了兩種方法A掉,一個是用一個int變量模擬時間,哪一個人進入窗口的時間加上服務時間等於現在時間就出隊,然後再插入新的人,另一個是直接把人一個一個插入窗口,如果窗口都滿了,那麼把所有窗口中第一個服務好的哪個人出隊列。兩種方法的代碼我都在下面貼出,思路參考了很多大佬,尤其是一個用65行C代碼A掉這題的大佬。

具體代碼

從時間角度

#include<stdio.h>
#include<stdlib.h>
#define MAXW 22
#define MAXQ 11
#define MAXP 1005
#define time int

struct Queue
{
    int q[MAXQ];
    int length;
    int front;
    int rear;
};

struct Window
{
    time endtime;
    struct Queue queue;
};

struct Person
{
    time poptime;
    time dealtime;
};

struct Window window[MAXW];
struct Person person[MAXP];
int N, M, K, Q;
time nowtime = 0;

void push_person(struct Queue *queue,int m)
{
    queue->q[queue->rear] = m;
    queue->rear = (queue->rear + 1) % (M + 1);
    queue->length++;
}

int pop_person(struct Queue *queue)
{
    int tmp = queue->q[queue->front];
    queue->front = (queue->front + 1) % (M + 1);
    queue->length--;
    return tmp;
}

int find_min()
{
    int tmp = -1;
    int min = 999;
    for (int i = 0; i < N; i++)
    {
        if (window[i].queue.length == 0)
            return i;
        if (window[i].queue.length != M && window[i].queue.length < min)
        {
            tmp = i;
            min = window[i].queue.length;
        }
    }
    return tmp;
}

int is_full()
{
    for (int i = 0; i < N; i++)
        if (window[i].queue.length != M)
            return 0;
    return 1;
}

int is_empty()
{
    for (int i = 0; i < N; i++)
        if (window[i].queue.length != 0)
            return 0;
    return 1;
}

void count_time()
{
    int p = 1;
    while (p <= K)
    {
        if (!is_empty())
        {
            for (int i = 0; i < N; i++)
            {
                if (person[window[i].queue.q[window[i].queue.front]].poptime + person[window[i].queue.q[window[i].queue.front]].dealtime == nowtime)
                    pop_person(&window[i].queue);
            }
        }
        while (!is_full() && p <= K)
        {
            int min = find_min();
            push_person(&window[min].queue, p);
            person[p].poptime = window[min].endtime;
            window[min].endtime += person[p].dealtime;
            p++;
        }
        nowtime++;
    }
}

int main(void)
{
    scanf("%d %d %d %d", &N, &M, &K, &Q);
    for (int i = 1; i <= K; i++)
        scanf("%d", &person[i].dealtime);
    count_time();
    for (int i = 0; i < Q; i++)
    {
        int q;
        scanf("%d", &q);
        if (person[q].poptime < 540)
            printf("%02d:%02d\n",8+(person[q].poptime+person[q].dealtime)/60, (person[q].poptime + person[q].dealtime) % 60);
        else
            printf("Sorry\n");
    }
    system("pause");
}

從客戶角度

#include<stdio.h>
#include<stdlib.h>
#define MAXW 22
#define MAXQ 11
#define MAXP 1005
#define time int

struct Queue
{
    int q[MAXQ];
    int length;
    int front;
    int rear;
};

struct Window
{
    time firsttime;
    time endtime;
    struct Queue queue;
};

struct Person
{
    time poptime;
    time dealtime;
};

struct Window window[MAXW];
struct Person person[MAXP];
int N, M, K, Q;

void push_person(struct Queue *queue,int m)
{
    queue->q[queue->rear] = m;
    queue->rear = (queue->rear + 1) % (M + 1);
    queue->length++;
}

int pop_person(struct Queue *queue)
{
    int tmp = queue->q[queue->front];
    queue->front = (queue->front + 1) % (M + 1);
    queue->length--;
    return tmp;
}

int find_min()
{
    int push = 0;
    for (int i = 0; i < N; i++)
    {
        if (window[i].firsttime < window[push].firsttime)
            push = i;
    }
    return push;
}

void count_time()
{
    int p = 1;
    while (p <= K)
    {
        int push;
        if (p <= M * N)
            push = (p - 1) % N;
        else
            push = find_min();
        if (window[push].queue.length == M)
        {
            pop_person(&window[push].queue);
            window[push].firsttime += person[window[push].queue.q[window[push].queue.front]].dealtime;
        }
        push_person(&window[push].queue, p);
        if (window[push].queue.length == 1)
            window[push].firsttime += person[window[push].queue.q[window[push].queue.front]].dealtime;
        person[p].poptime = window[push].endtime;
        window[push].endtime += person[p].dealtime;
        p++;
    }
}

int main(void)
{
    scanf("%d %d %d %d", &N, &M, &K, &Q);
    for (int i = 1; i <= K; i++)
        scanf("%d", &person[i].dealtime);
    count_time();
    for (int i = 0; i < Q; i++)
    {
        int q;
        scanf("%d", &q);
        if (person[q].poptime < 540)
            printf("%02d:%02d\n",8+(person[q].poptime+person[q].dealtime)/60, (person[q].poptime + person[q].dealtime) % 60);
        else
            printf("sorry\n");
    }
    system("pause");
}

參考博客

1014 Waiting in Line (30 point(s)) - C語言 PAT 甲級

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章