PAT甲级 1026

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;
}
### 关于 PAT 甲级 1024 题目 PAT (Programming Ability Test) 是一项编程能力测试,其中甲级考试面向有一定编程基础的学生。对于 PAT 甲级 1024 题目,虽然具体题目描述未直接给出,但从相似类型的题目分析来看,这类题目通常涉及较为复杂的算法设计。 #### 数据结构的选择与实现 针对此类问题,常用的数据结构包括但不限于二叉树节点定义: ```cpp struct Node { int val; Node* lchild, *rchild; }; ``` 此数据结构用于表示二叉树中的节点[^1]。通过这种方式构建的二叉树能够支持多种遍历操作,如前序、中序和后序遍历等。 #### 算法思路 当处理涉及到图论的问题时,深度优先搜索(DFS)是一种常见的解题策略。特别是当需要寻找最优路径或访问尽可能多的节点时,结合贪心算法可以在某些情况下提供有效的解决方案[^2]。 #### 输入输出格式说明 根据以往的经验,在解决 PAT 类型的问题时,输入部分往往遵循特定模式。例如,给定 N 行输入来描述每个节点的信息,每行按照如下格式:“Address Data Next”,这有助于理解如何解析输入并建立相应的数据模型[^4]。 #### 数学运算示例 有时也会遇到基本算术表达式的求值问题,比如分数之间的加减乘除运算。下面是一些简单的例子展示不同情况下的计算结果: - \( \frac{2}{3} + (-2) = -\frac{7}{3}\) -2) = -\frac{4}{3}\) - \( \frac{2}{3} ÷ (-2) = -\frac{1}{3}\) 这些运算是基于样例提供的信息得出的结果[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值