1017 Queueing at Bank 25 ☆☆☆

这是一个很有趣的题目,也有一些难度,很早以前就想讲了。和PAT A 1026,PAT A 1014是一个类型的题目,我称之排队问题,我在另外一篇“排队问题分析”中会详细讲一下这类问题的思路。 github 地址:https://github.com/iofu728/PAT-A-by-iofu728 难度:☆☆☆ 关键词:排队,模拟,sort

题目

1017.Queueing at Bank (25) Suppose a bank has K windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. All the customers have to wait in line behind the yellow line, until it is his/her turn to be served and there is a window available. It is assumed that no window can be occupied by a single customer for more than 1 hour.

Now given the arriving time T and the processing time P of each customer, you are supposed to tell the average waiting time of all the customers.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=10000) – the total number of customers, and K (<=100) – the number of windows. Then N lines follow, each contains 2 times: HH:MM:SS – the arriving time, and P – the processing time in minutes of a customer. Here HH is in the range [00, 23], MM and SS are both in [00, 59]. It is assumed that no two customers arrives at the same time.

Notice that the bank opens from 08:00 to 17:00. Anyone arrives early will have to wait in line till 08:00, and anyone comes too late (at or after 17:00:01) will not be served nor counted into the average.

Output Specification:

For each test case, print in one line the average waiting time of all the customers, in minutes and accurate up to 1 decimal place.

Sample Input: 7 3 07:55:00 16 17:00:01 2 07:59:59 15 08:01:00 60 08:00:00 30 08:00:02 2 08:03:00 10

Sample Output: 8.2

大意

和 1014 差不多,银行有 k 个窗口,有 n 个人依次到达银行,择短选队列等待。问平均等待时间为多少?

思路

  1. 选择利用一个 vector 的变量存储窗口最早能用时间,当然也可以按照 1014 那样设一个 node{poptime,endtime},用 poptime 和 endtime 控制进队和出队,但是本题队列长度只有 1,不需要这样复杂操作。
  2. 通过利用 node{come,spend}来记录需要接待的顾客,come--表示达到时间,以 8 点为基础,以秒为单位;spend--表示需要花费时间。
  3. 通过两个 sort 来确定顾客到达时间和进队情况。
  4. 对于到达时队列已经为空的情况,窗口数组的计算需要另外讨论。

code

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int waittime = 0;
struct node {
  int come, spend;
};
struct win {
  int poptime, endtime;
};
int n, k;
bool cmp(node a, node b) { return a.come < b.come; }
int main() {
  scanf("%d %d\n", &n, &k);
  vector<int> window(k);
  fill(window.begin(), window.end(), 0);
  vector<node> v;
  for (int i = 0; i < n; ++i) {
    node temp;
    int hour, minute, second;
    scanf("%d:%d:%d %d", &hour, &minute, &second, &temp.spend);
    temp.come = ((hour - 8) * 60 + minute) * 60 + second;
    if (temp.come <= 32400) v.push_back(temp);
  }
  sort(v.begin(), v.end(), cmp);
  //    for(int i=0;i<v.size();++i){
  //        cout<<v[i].come<<" "<<v[i].spend<<endl;
  //    }
  for (int i = 0; i < v.size(); i++) {
    sort(window.begin(), window.end());
    if (window[0] > v[i].come) {
      waittime += (window[0] - v[i].come);
      window[0] += v[i].spend * 60;
    } else {
      window[0] = v[i].come + v[i].spend * 60;
    }

    //      for(int i=0;i<3;++i){
    //          cout<<window[i]<<" ";
    //      }
    //      cout<<waittime<<endl;
  }

  if (v.size() == 0)
    cout << "0.0\n";
  else {
    printf("%.1f\n", (waittime / (60.0 * v.size())));
  }
  return 0;
}

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
You can use this BibTex to reference this blog if you find it useful and want to quote it.