PAT甲级 1026

51 篇文章 0 订阅
7 篇文章 0 订阅

Table Tennis

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


  1. 考点

    1. 模拟和逻辑
  2. 题解

    1. 分配时,第一个人到达时有table空余。
      1. 若该人是vip,且有vip table,分配vip table。
      2. 若该人不是vip,则直接分配最小的table。
    2. 分配时,第一个table可用时,已有人在等待。
      1. 若是vip table,则从最先等待的vip里面选择。
      2. 若不是vip table,则选择最先到达的人。
  3. 坑点

    1. round up指在超过或等于30时。
    2. 会有数据在8点和21点外,本题只涉及到21点外的。
    3. 题目2hours的限制,要将大于2hours的改成2hours。
#include<iostream>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<limits.h>
#include<set>
using namespace std;
const int END_TIME=21*60*60;
const int START_TIME=8*60*60;
set<pair<int,int> >noVipPlayer,vipPlayer;

struct node{
    int time,serveNum;
    bool vip;
    node(){};
    node(int time,int serveNum,bool vip){
        this->time=time,this->serveNum=serveNum,this->vip=vip;
    };
}table[100+1];
void outTime(int time){
    printf("%02d:%02d:%02d ",time/3600,time%3600/60,time%60);
}
void assginAndOut(int tableIdx,set<pair<int,int> >&setName){
    outTime(setName.begin()->first);
    outTime(max(table[tableIdx].time,setName.begin()->first));
    printf("%d\n",(max(setName.begin()->first,table[tableIdx].time)-setName.begin()->first+30)/60);
    ++table[tableIdx].serveNum;
    table[tableIdx].time=max(table[tableIdx].time,setName.begin()->first)+setName.begin()->second;
    setName.erase(setName.begin());
}

int main(){
    //freopen("./in","r",stdin);
    int N;
    scanf("%d",&N);
    int i,hh,mm,ss,play,vip;
    for(i=0;i<N;i++){
        scanf("%d:%d:%d %d %d",&hh,&mm,&ss,&play,&vip);
        if(vip){
            vipPlayer.insert(make_pair(hh*60*60+mm*60+ss,min(120*60,play*60)));
        }else{
            noVipPlayer.insert(make_pair(hh*60*60+mm*60+ss,min(120*60,play*60)));
        }
    }
    int K,M,vipTable;
    scanf("%d %d",&K,&M);
    for(i=0;i<K;i++){
        table[0]=node(START_TIME,0,0);
    }
    for(i=0;i<M;i++){
        scanf("%d",&vipTable);
        table[vipTable]=node(START_TIME,0,1);
    }
    int vipTime,noVipTime,minTime,tableIdx,vipTableIdx,minTimeTableIdx,idx;
    bool minIsVipTable;
    int cnt;
    while(!(vipPlayer.empty()) || !(noVipPlayer.empty())){
        vipTime=vipPlayer.empty()?INT_MAX:vipPlayer.begin()->first;
        noVipTime=noVipPlayer.empty()?INT_MAX:noVipPlayer.begin()->first;
        minTime=min(vipTime,noVipTime);
        minIsVipTable=table[1].vip;
        tableIdx=0;
        minTimeTableIdx=1;
        vipTableIdx=0;
        cnt=0;
        for(i=1;i<=K;i++){
            if(table[i].time<=minTime){
                ++cnt;
                if(!tableIdx) tableIdx=i;
                if(table[i].vip && !vipTableIdx){
                    vipTableIdx=i;
                }
            }
            if(table[i].time<table[minTimeTableIdx].time){
                minTimeTableIdx=i;
                minIsVipTable=table[i].vip;
            }
        }

        if(minTime>=END_TIME || table[minTimeTableIdx].time>=END_TIME)break;

        if(cnt){//第一个人到达时有table空余
            if(vipTime<noVipTime && vipTableIdx){//该人是vip且有vip table,分配vip table
                idx=vipTableIdx;
            }else{//分配最小的table
                idx=tableIdx;   
            }
            if(vipTime<noVipTime) assginAndOut(idx,vipPlayer);
            else assginAndOut(idx,noVipPlayer);
        }else{//第一个table可用时,已有人在等待
            if(minIsVipTable && vipTime<=table[idx].time){//有vip table且有Vip在等待,分配vip table
                assginAndOut(minTimeTableIdx,vipPlayer);
            }else{//分配给最先到的人
                if(vipTime<noVipTime) assginAndOut(minTimeTableIdx,vipPlayer);
                else assginAndOut(minTimeTableIdx,noVipPlayer);
            }
        }
    }
    printf("%d",table[1].serveNum);
    for(i=2;i<=K;++i){
        printf(" %d",table[i].serveNum);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值