[PAT] [繁琐排队模拟] 1026 Table Tennis (30 分)

题目链接: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 privilege 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:

10
20:52:00 10 0
08:00:00 20 0
08:02:00 30 0
20:51:00 10 0
08:10:00 30 0
08:12:00 10 1
20:40:00 13 0
08:01:30 15 1
20:53:00 10 1
20:54:00 10 0
3 1
2

Sample Output:

08:00:00 08:00:00 0
08:01:30 08:01:30 0
08:02:00 08:02:00 0
08:12:00 08:16:30 5
08:10:00 08:20:00 10
20:40:00 20:40:00 0
20:51:00 20:51:00 0
20:52:00 20:52:00 0
20:53:00 20:53:00 0
4 3 2

 // 解题思路:

// 又是排队模拟题,跟操作系统里的调度模型很相似,模拟得头痛也非常花时间, 写完和查bug将近用了三小时

其它博客的测试点分析:https://blog.csdn.net/jmlikun/article/details/49821845

#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
// 解题思路:
// 又是排队模拟题,跟操作系统里的调度模型很相似,模拟得头痛也非常花时间, 写完和查bug将近用了三小时
// 将player封装在结构体中
struct player
{
    int arr, leave, ser, wait, ptime, num;
    bool pri;
    bool operator<(const player &r) const
    {
        if(leave != r.leave)
        return leave > r.leave;
        else return num > r.num;
    }
};
// 两种比较比较方式,分别用于读入和ans的vector
bool cmp1(const player &l, const player &r)
{
    return l.arr < r.arr;
}
bool cmp2(const player &l, const player &r)
{
    return l.ser < r.ser;
}
// 服务优先队列,leave时间早得在前
priority_queue<player> serve;
// VIP和com桌队列
priority_queue<int, vector<int>, greater<int> > ready_com;
priority_queue<int, vector<int>, greater<int> > ready_vip;
// VIP 和 com player队列
queue<player> vip;
queue<player> com;
// v用于读入数据且按arr时间顺序排序,ans为serve时间顺序输出
vector<player> v, ans;
int cnt[105];
bool a[105];
int main()
{
    // 读入数据
    int n; cin >> n;
    int time, h, m, s, pt, pri; char c;
    for(int i = 1; i <= n; i++){
        cin >> h >> c >> m >> c >> s >> pt >> pri;
        if(!pt) continue;
        if(pt > 120) pt = 120;
        pt *= 60;
        time = h * 3600 + m * 60 + s;
        v.push_back((player){time, 0, 0, 0, pt, 0, pri});

    }
    // 标记桌子
    int tab, vtab; cin >> tab >> vtab;
    for(int t, i = 1; i <= vtab; i++){
        scanf("%d", &t);
        a[t] = true;
    }
    // 排序
    sort(v.begin(), v.end(), cmp1);
    // serve队列初始化,即填满table
    for(int i = 1; i <= tab; i++){
        serve.push((player){-1, 28800, -1, 0, 0, i, 0});
    }
    // 模拟时钟,i为指针,从0点到21点,注意不能等于21点
    player t;
    for(int k = 0, i = 0; i < 75600; i++)
    {
        if(k < v.size() && i == v[k].arr){
            if(v[k].pri){
                vip.push(v[k]);
            }
            else com.push(v[k]);
            k++;
        }
        // 到了离开时间则出列,且push重新的空闲桌子编号进ready队列
        while(!serve.empty() && i == serve.top().leave){
            if(a[serve.top().num])
            ready_vip.push(serve.top().num);
            else
            ready_com.push(serve.top().num);
            serve.pop();
        }
        // 按规则进行匹配
        while((!ready_vip.empty() || !ready_com.empty()) && (!vip.empty() || !com.empty()))
        {
            int tnum = -1;
            if(!ready_vip.empty() && !vip.empty()){
                t = vip.front();
                tnum = ready_vip.top();
                vip.pop();
                ready_vip.pop();
            }
            else if(!vip.empty() && !com.empty()){
                if(vip.front().arr < com.front().arr){
                    t = vip.front();
                    vip.pop();
                }
                else{
                    t = com.front();
                    com.pop();
                }
            }
            else if(!vip.empty()){
                t = vip.front();
                    vip.pop();
            }
            else{
                t = com.front();
                com.pop();
            }
            if(tnum == -1){
                int x = 1e5, y = 1e5;
                if(!ready_vip.empty()) x = ready_vip.top();
                if(!ready_com.empty()) y = ready_com.top();
                if(x < y){
                    tnum = x;
                    ready_vip.pop();
                }
                else{
                    tnum = y;
                    ready_com.pop();
                }
            }
            // 计算得到其它信息,放入服务队列
            t.num = tnum;
            t.ser = i;
            t.leave = i + t.ptime;
            t.wait = round((double)(t.ser - t.arr) / 60);           
            cnt[tnum]++;
            serve.push(t); ans.push_back(t);
        }
    }
    // 按要求排序输出
    sort(ans.begin(), ans.end(), cmp2);
    for(int i = 0; i < ans.size(); i++)
    {
        int arr = ans[i].arr;
        int ser = ans[i].ser;
        printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", arr / 3600, arr / 60 % 60, arr % 60, ser / 3600, ser / 60 % 60, ser % 60, ans[i].wait);
    }
    for(int i = 1; i < tab; i++)
    {
        printf("%d ", cnt[i]);
    }
    printf("%d\n", cnt[tab]);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值