【PAT】甲级-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

        大型模拟题,vip客户会优先选择编号最小的vip桌,没有vip桌才会去选择普通桌,普通客户会直接选择最小的桌子,无所谓是否vip桌。模拟日常生活的使用情况,对于一个抵达的客户,先去找他抵达时是否有可直接用的桌子(vip客户先找编号最小的vip桌,没有再找编号最小的普通桌,普通客户直接找编号最小的桌子),如果能找到则直接把当前客户安排到这个桌子,如果找不到,则去找最早能用的桌子,此时需要额外判断,如果最早能用的桌子为普通桌则直接安排当前客户使用,如果是vip桌,则要判断在这个vip桌可用时是否有vip客户在等待,有则安排vip客户,没有则安排当前客户。这里的部分代码和逻辑参考了柳婼大神。另外还有一个坑点是每个客户最多玩2小时,超过两小时全部视为2小时。

#include<bits/stdc++.h>
/*当一个客户抵达时,看是否有直接可用的桌子(vip为最小标号vip桌,没有vip桌才是最小标号的普通桌,普通客户就是找最小标号的可用桌),如果有则直接进行安排。
如果没有则去找最早结束的桌子,如果最早结束的桌子是普通桌直接安排,如果是vip桌则要看是否有vip客户正在等待,有则安排vip客户,没有就安排当前客户*/
using namespace std;
struct custom{
    int arrtime;
    int servetime;
    int needtime;
    bool isvip;
    bool isserved;
};
struct tablenode{
    int canserve;
    int count;
    bool isvip;
};
vector<custom> player;
vector<tablenode> table;
bool cmp1(custom a,custom b){
    return a.arrtime<b.arrtime;
}
bool cmp2(custom a,custom b){
    return a.servetime<b.servetime;
}
int findcanuse(int p){
    if(!player[p].isvip){
        for(int i=1;i<table.size();i++)
            if(table[i].canserve<=player[p].arrtime)
                return i;
    }else{
        for(int i=1;i<table.size();i++)
            if(table[i].canserve<=player[p].arrtime&&table[i].isvip)
                return i;
        for(int i=1;i<table.size();i++)
            if(table[i].canserve<=player[p].arrtime&&!table[i].isvip)
                return i;
    }
    return -1;
}
int findvip(){
    for(int i=0;i<player.size();i++){
        if(player[i].isvip&&!player[i].isserved)
            return i;
    }
    return -1;
}
int findtable(){
    int tmp=-1,mintime=99999999;
    for(int i=1;i<table.size();i++)
        if(table[i].canserve<mintime){
            mintime=table[i].canserve;
            tmp=i;
        }
    return tmp;
}
void alloc(int p,int t){
    if(player[p].arrtime<=table[t].canserve)
        player[p].servetime=table[t].canserve;
    else
        player[p].servetime=player[p].arrtime;
    if(player[p].servetime<75600)
        table[t].count++;
    table[t].canserve=player[p].servetime+player[p].needtime;
    player[p].isserved=true;
}
int main(){
    int n,k,m,vip;
    cin>>n;
    custom tmpcustom;
    for(int i=0;i<n;i++){
        int h,m,s,time,flag;
        scanf("%d:%d:%d %d %d",&h,&m,&s,&time,&flag);
        tmpcustom.arrtime=h*3600+m*60+s;
        if(tmpcustom.arrtime>=75600) continue;
        tmpcustom.needtime=(time>=120?7200:time*60);
        tmpcustom.isvip=(flag==1?true:false);
        tmpcustom.isserved=false;
        player.push_back(tmpcustom);
    }
    cin>>k>>m;
    table.resize(k+1);
    for(int i=0;i<m;i++){
        int tmp;
        cin>>tmp;
        table[tmp].isvip=true;
    }
    sort(player.begin(),player.end(),cmp1);
    int nowplayer=0;
    while(nowplayer<player.size()){
        if(player[nowplayer].isserved){
            nowplayer++;
            continue;
        }
        int nowtable=findcanuse(nowplayer);
        if(nowtable!=-1){
            alloc(nowplayer,nowtable);
            nowplayer++;
        }else{
            nowtable=findtable();
            if(!table[nowtable].isvip){
                alloc(nowplayer,nowtable);
                nowplayer++;
            }else{
                int nowvip=findvip();
                if(nowvip!=-1&&player[nowvip].arrtime<=table[nowtable].canserve){
                    alloc(nowvip,nowtable);
                }else{
                    alloc(nowplayer,nowtable);
                    nowplayer++;
                }
            }
        }
    }
    sort(player.begin(),player.end(),cmp2);
    for(int i=0;i<player.size()&&player[i].servetime<75600;i++){
        printf("%02d:%02d:%02d ",player[i].arrtime/3600,player[i].arrtime%3600/60,player[i].arrtime%60);
        printf("%02d:%02d:%02d ",player[i].servetime/3600,player[i].servetime%3600/60,player[i].servetime%60);
        printf("%.0f\n", round((player[i].servetime- player[i].arrtime)/60.0));
    }
    cout<<table[1].count;
    for(int i=2;i<table.size();i++)
        cout<<" "<<table[i].count;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值