PAT甲级-1026 Table Tennis (30分)【思路绝对清晰!!】

点击链接PAT甲级-AC全解汇总

题目:
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

题意:
乒乓球场有K个桌子,其中有M个vip桌子,8-21点营业,只接待21点之前有桌子的player,输出用户等待的时间以及每个桌子接待的用户数量。

思路:
我看很多人写的好复杂,各种情况列了半天,其实这道题思路是可以很清晰的。

存储格式:
桌子数组:记录服务完成的时间,初始化都为8*3600;
VIP和普通两个队列,按照到达时间排序;队头即为最早到达;

执行过程:
找出最早空出来的桌子,判断:

  • 如果是VIP桌,且VIP的队头已经到达,则VIP优先;否则和两个队头作比较
  • 如果是普通桌,直接两个队头作比较;

这里有个坑: 当很多桌子空出来的时候,理应选择数字最小的,但是!!!题目说了,如果有VIP桌空的时候,VIP进来优先选择VIP桌,而不选择数字小的普通桌。
所以,有2种情况是找最小的VIP桌,

  1. VIP桌的时间<VIP队头 且 VIP队头小于normal队头
  2. VIP桌的时间<VIP队头 且 normal队空

特别注意:

  1. 占用时间超过2h按照2h算;
  2. 等待时间按分钟四舍五入,并非向上取整,所以 (+30)%60 ;
  3. 是21点之前用上桌子,而不是21点之前到达;
  4. VIP找最小VIP桌,哪怕是刚空出来的VIP桌(case7),若没有空VIP桌才选最小普通桌;

我的代码:

#include<bits/stdc++.h>
using namespace std;

class Player{
public:
    Player(){};
    Player(int a,int b,bool c):arrive_time(a),using_time(b),vip(c){}
    int arrive_time;
    int using_time;
    int serve_time;
    int wait_time=0;
    bool vip;
    Player& operator =(Player p){
        this->arrive_time=p.arrive_time;
        this->using_time=p.using_time;
        this->vip=p.vip;
        return *this;
    }
};

bool my_cmp(Player a,Player b){
    return a.arrive_time<b.arrive_time;
}

int main()
{
    int table_time[110]={0},table_served[110]={0};
    bool table_vip[110]={0};
    vector<Player>all_players,res_players;
    queue<Player> q_normal,q_vip;

    int N,K,M;
    cin>>N;
    for(int i=0;i<N;i++)
    {
        int hh,mm,ss,use,vip;
        scanf("%d:%d:%d %d %d",&hh,&mm,&ss,&use,&vip);
        int time=hh*3600+mm*60+ss;
//        if(time>=21*3600)continue;//这里过滤会出错,不知道为什么
        Player t(time,use>120?120*60:use*60,vip);//最多是用2h
        all_players.push_back(t);
    }
    sort(all_players.begin(),all_players.end(),my_cmp);
    for(auto it:all_players)//普通队列和vip队列,按到达时间排序
    {
        if(it.vip)q_vip.push(it);
        else q_normal.push(it);
    }
    cin>>K>>M;
    for(int i=1;i<=K;i++)table_time[i]=8*3600;//初始化乒乓桌
    for(int i=0;i<M;i++)//标记vip桌
    {
        int t;
        cin>>t;
        table_vip[t]=true;
    }

    //看不懂代码的可以看我的思路
    while(q_vip.size()||q_normal.size())
    {
        int min_all_id=0,min_all_time=1000000;
        int min_vip_id=0,min_vip_time=1000000;
        for(int i=1;i<=K;i++)
        {
            if(table_time[i]<min_all_time)
            {
                min_all_time=table_time[i];
                min_all_id=i;
            }
            if(table_time[i]<min_vip_time&&table_vip[i])
            {
                min_vip_time=table_time[i];
                min_vip_id=i;
            }
        }

        //VIP进来优先选择VIP的空桌
        int min_id;
        if((q_vip.front().arrive_time<q_normal.front().arrive_time&&
            q_vip.front().arrive_time>=table_time[min_vip_id]&&q_vip.size())||
           (q_vip.front().arrive_time>=table_time[min_vip_id]&&q_normal.empty()))
           min_id=min_vip_id;
        else min_id=min_all_id;
        int min_time=table_time[min_id];

        Player next;
        if(q_vip.empty())
        {
            next=q_normal.front();
            q_normal.pop();
        }
        else if(q_normal.empty()||
           (table_vip[min_id]&&q_vip.front().arrive_time<=min_time))//vip插队
        {
            next=q_vip.front();
            q_vip.pop();
        }
        else
        {
            if(q_vip.front().arrive_time<q_normal.front().arrive_time)
            {
                next=q_vip.front();
                q_vip.pop();
            }
            else
            {
                next=q_normal.front();
                q_normal.pop();
            }
        }

        if(next.arrive_time<min_time)//需要等
        {
            next.serve_time=min_time;
            next.wait_time=(min_time-next.arrive_time+30)/60;
        }
        else//不需要等
        {
            next.serve_time=next.arrive_time;
        }

        if(next.serve_time>=21*3600)continue;
        table_time[min_id]=next.serve_time+next.using_time;
        res_players.push_back(next);
        table_served[min_id]++;
    }

    for(auto it:res_players)
    {
        int time=it.arrive_time;
        printf("%02d:%02d:%02d ",time/3600,(time/60)%60,time%60);
        time=it.serve_time;
        printf("%02d:%02d:%02d %d\n",time/3600,(time/60)%60,time%60,it.wait_time);
    }
    for(int i=1;i<=K;i++)
    {
        cout<<table_served[i];
        if(i<K)cout<<" ";
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值