PAT 1014 Waiting in Line

  1. 1. Waiting in Line
  2. 2. 思路
  3. 3. 踩坑

Waiting in Line

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.
  • 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:

1
2
3
2 2 7 5
1 2 6 4 3 534 2
3 4 5 6 7

Sample Output:

1
2
3
4
5
08:07
08:06
08:10
17:00
Sorry

银行有N个窗口,每个窗口前有黄线,可以站M个人.现在有K个人要来银行办业务.排队时优先选择人少的队伍,人数一样则优先选窗口编号小的.若黄线区全满则等到队伍空出时再排队.银行8点开门,17点后还没有开始服务的顾客无法接受服务,应输出Sorry,否则应输出服务结束的时间.

思路

先考虑两个窗口,黄线站一个人的情况.前两个人直接接受服务,第三个人会排到最先有空位的那个队伍中去.最先有空位的队伍,也就是当前服务的顾客所需剩余服务时间最短的队伍.
考虑这样定义结构

1
2
3
4
5
6
7
8
9
10
11
struct customer{
int start;
int id;
int duration;
int finished;
};
struct window{
int offset;
int remaining;
queue<int> que;
};

offset为当前时间距离8点的偏移量.remaining是当前服务顾客所需的剩余时间.整个银行的操作过程可以描述如下:

  1. 找到一个排队人数最少的队伍
    • 如果不存在这样的队伍,转到2.
    • 如果存在这样的队伍,顾客入队.
  2. 找到一个顾客离开最早的队伍,完成该顾客的业务,使其离开,并把当前顾客入队.

下面四个函数对应

  • 找到排队人数最少的队伍,
  • 顾客入队
  • 找到顾客离开最早的队伍
  • 顾客离开
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    int find_next_window()
    {
    int res = -1;
    int min = inf;
    for(int i=0;i<n;i++)
    {
    int size = windows[i].que.size();
    if(size<min)
    {
    min = size;
    res = i;
    }
    }
    if(min<m) return res;
    else return -1;
    }
    void insert_next_window(int customIndex,int index)
    {
    if(index==-1) return;
    if(windows[index].que.size()==0)
    {
    windows[index].remaining=cus[customIndex].duration;
    cus[customIndex].start=windows[index].offset;
    }
    windows[index].que.push(customIndex);
    }
    int find_next_finished()
    {
    int res = -1;
    int min = inf;
    for(int i=0;i<n;i++)
    {
    if(windows[i].que.size()==0) continue;
    int size = windows[i].remaining;
    if(size<min)
    {
    min = size;
    res = i;
    }
    }
    if(min==inf) return -1;
    return res;
    }
    void finish_next(int index)
    {
    if(index<0) return;
    int remaining = windows[index].remaining;
    for(int i=0;i<n;i++)
    {
    windows[i].offset+=remaining;
    windows[i].remaining-=remaining;
    }
    int front = windows[index].que.front();
    windows[index].que.pop();
    cus[front].finished=windows[index].offset;
    if(!windows[index].que.empty())
    {
    int next = windows[index].que.front();
    windows[index].remaining = cus[next].duration;
    cus[next].start=windows[index].offset;
    }
    }

整个流程如下:

  • 找出最短的队伍,把人塞进入
  • 如果找不到最短的队伍,找到最先完成的队伍,让队首的顾客离开,把人塞进入
  • 所有人都塞到队伍里后,等待所有队伍处理结束.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <vector>
    #include <cstdio>
    using namespace std;
    struct customer{
    int start;
    int id;
    int duration;
    int finished;
    };
    struct window{
    int offset;
    int remaining;
    queue<int> que;
    };
    int n,m,k,q;
    vector<window> windows;
    vector<customer> cus;
    const int inf = 0xFFFFF;
    int find_next_window()
    {
    int res = -1;
    int min = inf;
    for(int i=0;i<n;i++)
    {
    int size = windows[i].que.size();
    if(size<min)
    {
    min = size;
    res = i;
    }
    }
    if(min<m) return res;
    else return -1;
    }
    void insert_next_window(int customIndex,int index)
    {
    if(index==-1) return;
    if(windows[index].que.size()==0)
    {
    windows[index].remaining=cus[customIndex].duration;
    cus[customIndex].start=windows[index].offset;
    }
    windows[index].que.push(customIndex);
    }
    int find_next_finished()
    {
    int res = -1;
    int min = inf;
    for(int i=0;i<n;i++)
    {
    if(windows[i].que.size()==0) continue;
    int size = windows[i].remaining;
    if(size<min)
    {
    min = size;
    res = i;
    }
    }
    if(min==inf) return -1;
    return res;
    }
    void finish_next(int index)
    {
    if(index<0) return;
    int remaining = windows[index].remaining;
    for(int i=0;i<n;i++)
    {
    windows[i].offset+=remaining;
    windows[i].remaining-=remaining;
    }
    int front = windows[index].que.front();
    windows[index].que.pop();
    cus[front].finished=windows[index].offset;
    if(!windows[index].que.empty())
    {
    int next = windows[index].que.front();
    windows[index].remaining = cus[next].duration;
    cus[next].start=windows[index].offset;
    }
    }
    int main()
    {
    cin>>n>>m>>k>>q;
    cus = vector<customer>(k);
    windows = vector<window>(n);
    for(int i=0;i<k;i++)
    {
    cus[i].id=i+1;
    cin>>cus[i].duration;
    }
    int i =0;
    while(i<k)
    {
    int index = find_next_window();
    if(index!=-1)
    {
    insert_next_window(i,index);
    }
    else{
    break;
    }
    i++;
    }
    for(;i<k;i++)
    {
    int j = find_next_finished();
    if(j!=-1)
    {
    finish_next(j);
    }
    int index = find_next_window();
    if(index!=-1)
    {
    insert_next_window(i,index);
    }
    }
    while(true)
    {
    int j = find_next_finished();
    if(j==-1)
    {
    break;
    }
    finish_next(j);
    }
    for(int i=0;i<q;i++)
    {
    int s;
    cin>>s;
    int offset = cus[s-1].finished;
    int h = offset/60+8;
    int m = offset%60;
    if(cus[s-1].start>=540)
    {
    cout<<"Sorry"<<endl;
    }
    else
    {
    printf("%02d:%02d\n",h,m);
    }
    }
    }

踩坑

误解了题意,以为五点后所有的业务都将停止,没有做完业务的顾客将被强制离开.这也和现实里的银行相悖.以后要细心读题.

把17点距离8点的offset算成了480导致一直有case不能AC.我总是能在加减法上出错🙃