PAT1026 (大模拟)

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
和之前做的银行排队服务的问题类似,区别在于这里增加了vip选项。如果有vip的台子空出来,在排队的人中有vip,那么就会将vip台子分给他;否则就分给普通人。
当前排队队首是vip,空出的台子不是vip台子,不会立即分配,而是寻找在时间范围内是否有vip台子可以空出来。
最初想分情况讨论,写的很复杂,时间还会超时。对于这种问题,可以这样来解决的:
依次找最先空出来的台子,在刚进来的人中,根据是否是vip,空出的台子是否为vip进行分配。
代码如下:
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
struct pla{
int arrive,start,p,tag;
};
struct tab
{
    int end=8*3600;
    int vip,num;
};
vector<pla>player;
vector<tab>table;
const int inf=0x3f3f3f3f;
bool cmp1(pla a,pla b)
{
    return a.arrive<b.arrive;
}
bool cmp2(pla a,pla b)
{
    return a.start<b.start;
}
int findvip(int id)
{
    id++;
    while(id<player.size()&&player[id].tag!=1)
        id++;
    return id;
}
void allocate(int person,int index)
{

    if(player[person].arrive<=table[index].end)
        player[person].start=table[index].end;
    else
        player[person].start=player[person].arrive;
    //cout<<person<<" "<<index<<player[person].start<<" "<<player[person].arrive<<endl;
    table[index].end=player[person].start+player[person].p;
    table[index].num++;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int h,m,s,p,tag;
        scanf("%d:%d:%d %d %d",&h,&m,&s,&p,&tag);
        pla temp;
        temp.arrive=h*3600+m*60+s;
        temp.start=21*3600;
        if(temp.arrive>=21*3600) //是否不符合条件
            continue;
        temp.p=p<=120?p*60:7200;  //测试用例有超过2小时的情况
        temp.tag=tag;
        player.push_back(temp);
    }
    int k,m;
    scanf("%d%d",&k,&m);
    table.resize(k+1);
    for(int i=1;i<=m;i++)
    {
        int tmp;
        scanf("%d",&tmp);
        table[tmp].vip=1;
    }
    sort(player.begin(),player.end(),cmp1);
    int i=0,vipid=-1;
    vipid=findvip(vipid); //寻找vip成员
    while(i<player.size())
    {
        int index=-1,minendtime=inf;
        for(int j=1;j<=k;j++) //寻找最早结束的桌子
        {
            if(table[j].end<minendtime)
            {
                minendtime=table[j].end;
                index=j;
            }
        }
        if(table[index].end>=21*3600)
            break;
        if(player[i].tag==true&&i<vipid) //说明此vip已经分配过了
        {
            i++;
            continue;
        }
        if(table[index].vip==1)
        {
            if(player[i].tag==1)
            {
                allocate(i,index);
                if(vipid==i)
                    vipid=findvip(vipid);
                i++;
            }
            else
            {
                if(vipid<player.size()&&player[vipid].arrive<=table[index].end)
                {
                    allocate(vipid,index);
                    vipid=findvip(vipid);
                }
                else
                {
                    allocate(i,index);
                    i++;
                }
            }
        }
        else
        {
            if(player[i].tag!=1)
            {
                allocate(i,index);
                i++;
            }
            else
            {
                int vipindex=-1,vipendtime=inf;
                for(int j=1;j<=k;j++)
                {
                    if(table[j].vip==1&&table[j].end<vipendtime)
                    {
                        vipindex=j;
                        vipendtime=table[j].end;
                    }
                }
                if(vipindex!=-1&&player[i].arrive>=table[vipindex].end)
                {
                    allocate(i,vipindex);
                    if(i==vipid)
                        vipid=findvip(vipid);
                    i++;
                }
                else
                {
                    allocate(i,index);
                    if(i==vipid)
                        vipid=findvip(vipid);
                    i++;
                }

            }
        }
    }
    sort(player.begin(),player.end(),cmp2);
    for(int i=0;i<player.size()&&player[i].start<21*3600;i++)
    {
        printf("%02d:%02d:%02d ",player[i].arrive/3600,player[i].arrive%3600/60,player[i].arrive%60);
        printf("%02d:%02d:%02d ",player[i].start/3600,player[i].start%3600/60,player[i].start%60);
        printf("%.0f
",round((player[i].start-player[i].arrive)/60.0));
    }
    for(int i=1;i<table.size();i++)
    {
        if(i!=1)
            printf(" ");
        printf("%d",table[i].num);
    }
}


 
原文地址:https://www.cnblogs.com/flightless/p/8542032.html