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;
}

累计访问量: | 昨日访问量: | 昨日爬虫数:
Gunjianpan © 2017 - 2020 Power by VuePress & iofu728/blog