【PAT】【Advanced Level】1026. Table Tennis (30)

来源:互联网 发布:mac os官方下载 编辑:程序博客网 时间:2024/05/20 03:40

1026. Table Tennis (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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:
920:52:00 10 008:00:00 20 008:02:00 30 020:51:00 10 008:10:00 5 008:12:00 10 120:50:00 10 008:01:30 15 120:53:00 10 13 12
Sample Output:
08:00:00 08:00:00 008:01:30 08:01:30 008:02:00 08:02:00 008:12:00 08:16:30 508:10:00 08:20:00 1020:50:00 20:50:00 020:51:00 20:51:00 020:52:00 20:52:00 03 3 2
原题链接:

https://www.patest.cn/contests/pat-a-practise/1026

https://www.nowcoder.com/questionTerminal/f499b30897e34f3ea33ad767daa6716a

思路:

按时间顺序排序。

从营业开始处理

对于每一个时间点

先判断VIP桌是否空闲,有空闲则插入VIP

在扫描全部桌子(包括VIP)

坑点:

PAT数据较弱

牛客网数据较强

时间间隔四舍五入

VIP可能乱序输入

时间不超过两个小时

CODE:

#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#define N 10010#define K 101using namespace std;typedef struct S{  string arri;  int ti;  int per;  int v;  int comp;  int sta;  int tz;};typedef struct T{    int tnum;    int cid;    int rest;};S cust[N];T tab[K];T vtab[K];int ntab[K];bool cmp1(S a,S b){    return a.ti<b.ti;}bool cmp2(S a,S b){    if (a.sta==b.sta) return (a.sta-a.ti)<(b.sta-b.ti);    else return a.sta<b.sta;}bool cmp3(T a,T b){    return a.tnum<b.tnum;}int main(){    memset(ntab,0,sizeof(ntab));    int n;    cin>>n;    for (int i=0;i<n;i++)    {        string a;        int b,c;        cin>>a>>b>>c;        int tim;        tim=atoi(a.substr(0,2).c_str())*3600+atoi(a.substr(3,2).c_str())*60+atoi(a.substr(6,2).c_str());        S t;        t.arri=a;        t.ti=tim;        t.per=b*60;        t.v=c;        t.comp=0;        t.sta=-1;        cust[i]=t;    }    int k,m;    cin>>k>>m;    bool fl[K];    memset(fl,0,sizeof(fl));    for (int i=0;i<m;i++)    {        T ta;        ta.cid=-1;        cin>>ta.tnum;        ta.tnum--;        vtab[i]=ta;        fl[ta.tnum]=1;    }    sort(vtab,vtab+m,cmp3);    int nn=0;    for (int i=1;i<=k;i++)    {        tab[nn].cid=-1;        tab[nn++].tnum=i;    }    sort(cust,cust+n,cmp1);    int nx=0;    int nv=-1;    for(int i=0;i<n;i++)        if(cust[i].v==1)    {        nv=i;        break;    }    for (int i=8*3600;i<21*3600;i++)    {        for (int j=0;j<m;j++)        {            if (tab[vtab[j].tnum].cid==-1)            {                if (nv==-1) continue;                if (cust[nv].ti>i) continue;                if (nv==nx) nx++;                tab[vtab[j].tnum].cid=nv;                tab[vtab[j].tnum].rest=cust[nv].per;                cust[nv].sta=i;                cust[nv].comp=1;                cust[nv].tz=vtab[j].tnum;                ntab[vtab[j].tnum]++;                int tv=nv;                nv=-1;                for(int l=tv+1;l<n;l++)                    if(cust[l].v==1)                {                    nv=l;                    break;                }            }        }        for (int j=0;j<k;j++)        {            if (tab[j].cid==-1)            {                if (nx>=n) continue;                if (cust[nx].ti>i) continue;                if (nv==nx)                {                    nv=-1;                    for(int l=nx+1;l<n;l++)                        if(cust[l].v==1)                    {                        nv=l;                        break;                    }                }                tab[j].cid=nx;                tab[j].rest=cust[nx].per;                cust[nx].sta=i;                cust[nx].comp=1;                cust[nx].tz=j;                ntab[j]++;                nx++;                while (cust[nx].comp==1)                {                    nx++;                    if (nx==n) break;                }            }            tab[j].rest--;            if (tab[j].rest==0 || (cust[tab[j].cid].per-tab[j].rest>=120*60))            {                tab[j].cid=-1;            }        }    }    sort(cust,cust+n,cmp2);    for (int i=0;i<n;i++)    {        if (cust[i].sta<0) continue;        string _time="";        int shi=cust[i].sta/3600;        int fen=cust[i].sta%3600/60;        int miao=cust[i].sta%60;        _time+=('0'+shi/10);        _time+=('0'+shi%10);        _time+=':';        _time+=('0'+fen/10);        _time+=('0'+fen%10);        _time+=':';        _time+=('0'+miao/10);        _time+=('0'+miao%10);        int las=((cust[i].sta-cust[i].ti)/60);        if ((cust[i].sta-cust[i].ti)%60>=30) las++;        cout<<cust[i].arri<<" "<<_time<<" "<<las<<endl;    }    for (int i=0;i<k-1;i++)        cout<<ntab[i]<<" "; cout<<ntab[k-1];    return 0;}



原创粉丝点击