1026 Table Tennis (30)★★★★☆

哇 又碰到排队问题,这类问题做起来很费时间,尤其是这道题,复杂程度在PAT A 1014之上,关构建函数就写了 50 行。等做完题目一看,正确率全站最低,只有 0.15,瑟瑟发抖。 github 地址:https://github.com/iofu728/PAT-A-by-iofu728 难度:★★★★☆ 关键词:排队问题,模拟,排序

题目

1026.Table Tennis (30) A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the tables are occupied, they will have to wait in a queue. It is assumed that every pair of players can play for at most 2 hours.

Your job is to count for everyone in queue their waiting time, and for each table the number of players it has served for the day.

One thing that makes this procedure a bit complicated is that the club reserves some tables for their VIP members. When a VIP table is open, the first VIP pair in the queue will have the priviledge to take it. However, if there is no VIP in the queue, the next pair of players can take it. On the other hand, if when it is the turn of a VIP pair, yet no VIP table is available, they can be assigned as any ordinary players.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (<=10000) – the total number of pairs of players. Then N lines follow, each contains 2 times and a VIP tag: HH:MM:SS – the arriving time, P – the playing time in minutes of a pair of players, and tag – which is 1 if they hold a VIP card, or 0 if not. It is guaranteed that the arriving time is between 08:00:00 and 21:00:00 while the club is open. It is assumed that no two customers arrives at the same time. Following the players’ info, there are 2 positive integers: K (<=100) – the number of tables, and M (< K) – the number of VIP tables. The last line contains M table numbers.

Output Specification:

For each test case, first print the arriving time, serving time and the waiting time for each pair of players in the format shown by the sample. Then print in a line the number of players served by each table. Notice that the output must be listed in chronological order of the serving time. The waiting time must be rounded up to an integer minute(s). If one cannot get a table before the closing time, their information must NOT be printed.

Sample Input: 9 20:52:00 10 0 08:00:00 20 0 08:02:00 30 0 20:51:00 10 0 08:10:00 5 0 08:12:00 10 1 20:50:00 10 0 08:01:30 15 1 20:53:00 10 1 3 1 2

大意

做个一个桌球馆,叫号程序。这个桌球馆有 k 张桌子,特别一点的是这家还有 VIP 制度,安排 m 张 VIP 桌子,当 VIP 桌子空的时候,VIP 客户可以在普通客户之前排队。如果 VIP 桌也满了,VIP 等同于普通客户。输出今天营业时间内,所有客户到达时间,被叫到号时间,等待时间。并输出每张桌子服务客户次数。

思路

  1. 乍一看,和之前讲的 1014、1017 是一个类型的题目,也没有多少复杂,就是设一个队列 vector,通过 sort 来确定下一个可用桌子。但不同的是,本题有 VIP 服务,没有 VIP 服务我们只需要对窗口(也就是这里的桌子的 poptime 进行排序),当这里不仅要对桌子排序,还要对客户队伍排序,以便 VIP 客户能在 VIP 桌子可用的时候到达队首。 具体实施的时候,采用了两个 struct 分别对桌子、客户进行记录。
  2. 对于模拟问题,我们要抓住遍历对象,抓主要,避免把问题想复杂。对于排队问题,我们的遍历对象,始终是队伍的出队时间 poptime,只有一个人出队了,其他人才能进队。
  3. VIP 系统如何实现。仔细分析题目,本题的 VIP 策略,只有在 VIP 客户已经在排队,且在 VIP 客户前面还有其他客户在等的时候,有空 VIP 桌子出现的时候,才能插队,所以在遍历桌子 pop 事件的时候。只有当 pop 的桌子是 VIP 桌子的时候才要考虑有没有插队。(当然经过后面的分析可以发现,不仅要满足上述条件,还要满足剩余队列中存在 VIP 用户才有可能被插队)。
  4. 因为有 VIP 系统,有插队情况,就不能按照 vector 顺序遍历。就考虑使用类似一个 queue 的策略,(之所以不使用 queue,是因为在 cpp 中 queue 不能使用 sort 函数),每遍历一个节点,pop 一个(在 vector 中,使用 erase)。
  5. 一开始偷懒,用了好多个 sort,后来最后一个测试点,报超时,无奈把几乎所有的 sort 都删了。呜呜呜,所以有时候 sort 是好用,但也要注意是否必要。
  6. 本题还要注意在营业时间才有效,特别是桌子服务次数的计算的时候也要在营业时间

code

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 101;

struct node  //客户
{
  int come, spend, start, wait;
  bool vip;
};

struct tablenode  //桌子
{
  int poptime = 0, num, time = 0;
  bool vip = false;
};

node tempvip;
vector<node> v, q;
bool vis[maxn] = {false};
int vipid, mm, hh, ss;

bool cmp(node a, node b) { return a.come < b.come; }
bool cmpvip(node a, node b) {
  return ((a.vip == b.vip) ? (a.come < b.come) : (a.vip > b.vip));
}
bool cmptable(tablenode a, tablenode b) {
  return ((a.poptime == b.poptime) ? (a.num < b.num) : (a.poptime < b.poptime));
}
bool cmptables(tablenode a, tablenode b) { return a.num < b.num; }
bool findvip() {
  for (int i = 0; i < q.size(); ++i) {
    if (q[i].vip) {
      tempvip = q[i];
      vipid = i;
      return true;
    }
  }
  return false;
}
void changetime(int time) {
  hh = time / 3600 + 8;
  time %= 3600;
  mm = time / 60;
  ss = time % 60;
  return;
}

int main(int argc, char const *argv[]) {
  int n, k, m, sp, vi, cometime;
  cin >> n;
  getchar();
  for (int i = 0; i < n; ++i) {
    node temp;
    scanf("%d:%d:%d %d %d", &hh, &mm, &ss, &sp, &vi);
    cometime = (hh - 8) * 3600 + (mm)*60 + ss;
    temp.come = cometime;
    temp.spend = ((sp >= 120) ? 120 : sp);
    temp.vip = ((vi) ? true : false);
    if (cometime <= 46800) {
      q.push_back(temp);
    }
  }
  scanf("%d %d", &k, &m);
  std::vector<tablenode> table(k);
  for (int i = 0; i < m; ++i) {
    int temp;
    scanf("%d", &temp);
    table[temp - 1].vip = true;
    table[temp - 1].num = temp;
  }
  for (int i = 0; i < k; ++i) {
    if (!table[i].vip) table[i].num = i + 1;
  }
  sort(q.begin(), q.end(), cmp);
  for (int i = 0; i < n; ++i) {
    sort(table.begin(), table.end(), cmptable);
    node temp = q[0];
    if (table[0].vip == false || (!findvip())) {
      q.erase(q.begin());
      if (table[0].poptime <= temp.come) {
        temp.start = temp.come;
        table[0].poptime = temp.come + temp.spend * 60;
        temp.wait = 0;
      } else {
        temp.start = table[0].poptime;
        table[0].poptime += temp.spend * 60;
        temp.wait = temp.start - temp.come;
      }
      if (temp.start < 46800) {
        v.push_back(temp);
        ++table[0].time;
      }
    } else {
      if (table[0].poptime >= tempvip.come) {
        q.erase(q.begin() + vipid);
        tempvip.start = table[0].poptime;
        table[0].poptime += tempvip.spend * 60;
        tempvip.wait = tempvip.start - tempvip.come;
        if (tempvip.start < 46800) {
          ++table[0].time;
          v.push_back(tempvip);
        }
      } else {
        q.erase(q.begin());
        if (table[0].poptime <= temp.come) {
          temp.start = temp.come;
          table[0].poptime = temp.come + temp.spend * 60;
          temp.wait = 0;
        } else {
          temp.start = table[0].poptime;
          table[0].poptime += temp.spend * 60;
          temp.wait = temp.start - temp.come;
        }
        if (temp.start < 46800) {
          ++table[0].time;
          v.push_back(temp);
        }
      }
    }
  }
  for (int i = 0; i < v.size(); ++i) {
    changetime(v[i].come);
    printf("%02d:%02d:%02d ", hh, mm, ss);
    changetime(v[i].start);
    printf("%02d:%02d:%02d %d\n", hh, mm, ss, int(round(v[i].wait / 60.0)));
  }
  sort(table.begin(), table.end(), cmptables);
  for (int i = 0; i < table.size() - 1; ++i) {
    printf("%d ", table[i].time);
  }
  printf("%d\n", table[table.size() - 1].time);
  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
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
You can use this BibTex to reference this blog if you find it useful and want to quote it.