1026 Table Tennis (30 分)(C++)

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

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:50:00 20:50:00 0
20:51:00 20:51:00 0
20:52:00 20:52:00 0
3 3 2

本题是我见过的PAT甲级里面坑点最多的题emmmmm

简要说一下思路吧(思路不算难,主要是坚持写下去):

首先对人的处理:我没有用队列存储(这里可以用优先队列,很方便),我直接用vector数组存储,再用sort排序,个人没怎么用过优先队列,看你自己。在存储的时候,我另外用vipplayers数组将所有vip用户存储起来,然后用双指针的方式指示下一个待服务的。另外每个人一次标记,用一个isserve数组记录对应编号的人是否已经服务到了

对桌子的处理:我用的类似1014的方式,没有用结构体,直接用数组记录每个桌子下一个空闲的时间

如何服务:首先与类似1014的方式一样,遍历所有桌子,找到结束时间最早的那个桌子,但是与1014不一样的是,如果遇到一个桌子的结束时间早于此时即将需要提供服务的玩家的到达时间,立即停止寻找。现在这个桌子是否为该玩家提供服务,还需要判断一下,如果这个桌子是vip桌,而玩家不是vip,那么就要从vipplayer数组中第一个等待服务的,如果该vip玩家在桌子空闲前到达,那么这个桌子给vip,本次服务给该vip玩家;其次如果这个桌子不是vip,而玩家是vip,还要对桌子进行遍历,假设存在vip桌子空着,则换vip桌子。

注意点:

①playtime超过2小时要压缩为2小时

②虽然本题所有人的到达时间没有超过21点,但是第三组数据有恰好21点到的,此人不能得到服务

③vip用户,如果此时有vip桌子空着,用编号最小的vip桌子

④等待时间四舍五入

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct 
{
    int id, arrtime, flag, playtime;
}player;
int n, k, m, cnt = 0;
int main(){
    scanf("%d", &n);
    std::vector<player> players(n), vipplayers;
    for(int i = 0; i < n; ++ i){
        int h, m, s;
        scanf("%d:%d:%d %d %d",&h,&m,&s,&players[i].playtime,&players[i].flag);
        players[i].arrtime = 3600 * (h - 8) + 60 * m + s;
        players[i].id = i;
        players[i].playtime *= 60;
        players[i].playtime = min(7200, players[i].playtime);
        if(players[i].flag == 1)
            vipplayers.push_back(players[i]);
    }
    sort(vipplayers.begin(), vipplayers.end(),[](player a, player b){return a.arrtime < b.arrtime;});
    sort(players.begin(), players.end(),[](player a, player b){return a.arrtime < b.arrtime;});
    scanf("%d %d", &m, &k);
    std::vector<int> viptable(m + 1, 0), lasttime(m + 1, 0), tablecnt(m + 1, 0), isserve(n ,0);
    for(int i = 0; i < k; ++ i){
        int temp;
        scanf("%d", &temp);
        viptable[temp] = 1;
    }
    int curNorm = 0, curVip = 0;
    for(int i = 0; i < n; ++ i){
        while(isserve[players[curNorm].id] == 1)
            ++ curNorm;
        int minn = lasttime[1], tableid = 1;
        player serve = players[curNorm];
        for(int j = 2; j <= m; ++ j){
            if(minn <= players[curNorm].arrtime)
                break;
            if(lasttime[j] < minn){
                minn = lasttime[j];
                tableid = j;
            }
        }   
        if(minn >= 46800 || serve.arrtime >= 46800)
            break;
        while(curVip < vipplayers.size() && isserve[vipplayers[curVip].id] == 1)
            ++ curVip;
        if(viptable[tableid] == 1 && players[curNorm].flag == 0 && curVip < vipplayers.size() && lasttime[tableid] >= vipplayers[curVip].arrtime)
            serve = vipplayers[curVip];
        if(serve.flag == 1 && viptable[tableid] == 0){
            for(int j = tableid + 1; j <= m; ++ j)
                if(viptable[j] == 1 && lasttime[j] <= serve.arrtime){
                    tableid = j;
                    break;
                }
        }
        printf("%02d:%02d:%02d",serve.arrtime / 3600 + 8,(serve.arrtime % 3600) / 60,serve.arrtime % 60);
        if(serve.arrtime > lasttime[tableid])
            lasttime[tableid] = serve.arrtime;
        printf(" %02d:%02d:%02d ",lasttime[tableid]/3600+8,(lasttime[tableid]%3600)/60,lasttime[tableid]%60);
        int waittime = max(lasttime[tableid] - serve.arrtime, 0);
        waittime = int(1.0 * waittime / 60 + 0.5);
        printf("%d\n", waittime);
        ++ tablecnt[tableid];
        lasttime[tableid] += serve.playtime;
        isserve[serve.id] = 1;
    }
    printf("%d",tablecnt[1]);
    for(int i = 2; i <= m; i++)
        printf(" %d",tablecnt[i]);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值