PTA 7-6 Table Tennis(30分)

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

10
20:52:00 10 0
08:00:00 20 0
08:02:00 30 0
20:51:00 10 0
08:10:00 30 0
08:12:00 10 1
20:40:00 13 0
08:01:30 15 1
20:53:00 10 1
20:54:00 10 0
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:40:00 20:40:00 0
20:51:00 20:51:00 0
20:52:00 20:52:00 0
20:53:00 20:53:00 0
4 3 2

限制条件

限制条件数量
代码长度限制16 KB
时间限制400 ms
内存限制64 MB

C++代码

#include <iostream>

using namespace std;

#define maxp 10000 //客人总数不超过10000
#define maxt 100 //桌子总数不超过100

int n, k, m; //共n个客人,k张桌子,其中m张VIP桌子

struct Player //单个客人
{
    int arrtime; //到达时间
    int statime; //开始时间
    int playtime; //比赛时间
    int vipp; //是否为VIP客人
};
struct Player player[maxp]; //客人总数

struct Table
{
    int time; //被占用时间
    int vipt; //是否为VIP桌
};
struct Table table[maxt];

int server[maxt]; //每张桌子服务了多少客人

int start = 8 * 3600; //开门时间,用秒计算,后面会更新为到达时间
int close = 21 * 3600; //关门时间

int findemptytable(int vipp) //找空桌子
{
    int emptytable = -1; //如果没有空桌子,显示为-1
    for(int i = 0; i < k; i++)
    {
        if(table[i].time == 0) //如果有空桌子
        {
            //如果来的是VIP客人,但该空桌子不是VIP桌
            if(vipp == 1 && table[i].vipt != 1)
            {
                continue;
            }
            emptytable = i;
            break;
        }
    }
    return emptytable;
}

int findvip(int waitfirst, int nowtime) //查看等待队列里有没有VIP客人
//waitfirst等待队列里的第一个人
{
    int v = -1;
    for(int j = waitfirst; j < n && player[j].arrtime <= nowtime; j++)
    {
        if(player[j].vipp)
        {
            v = j;
            break;
        }
    }
    return v;
}

int NoOvertime(int t) //超过两小时按两小时计算
{
    if(t > 7200)
    {
        return 7200;
    }
    else
    {
        return t;
    }
}

//把第5题的堆排序搬过来,改成了结构体数组
void subheap(struct Player a[], int i, int n) //比较父结点和子结点,在子树中生成最大堆
{
    int parent = i;
    int child = 2 * i + 1; //堆从0开始,父结点为i,则左孩子为2i+1
    while(child < n)
    {
        if(child + 1 < n && a[child].arrtime < a[child + 1].arrtime) //如果这个父结点有右孩子,并且右孩子比左孩子大
        {
            child += 1; //child的值是左右孩子中较大孩子的位置
        }
        if(a[child].arrtime > a[parent].arrtime) //如果孩子结点比父结点大,交换二者
        {
            struct Player tem = a[child];
            a[child] = a[parent];
            a[parent] = tem;

            parent = child;
        }

        child = child * 2 + 1; //由上至下做比较
    }
}

void trip(struct Player a[], int num) //第一趟堆排序,将最大值放到根结点的位置 //num为数组元素个数
{
    for(int i = num / 2 - 1; i >= 0; i--) // n/2+1是倒数第二排最后一个位置,也就是右下角那棵子树的根结点
    {
        subheap(a, i, num);
    }
}

void heapsort(struct Player a[], int num) //堆排序
{
    trip(a, num);

    for(int i = num - 1; i > 0; i--)
    {
        struct Player f = a[0];
        a[0] = a[i];
        a[i] = f; //交换根结点和最后一个元素的位置

        subheap(a, 0, i);
    }

}

int main()
{
    cin >> n; //共n个客人

    int t1, t2, t3; //时分秒
    for(int i = 0; i < n; i++)
    {
        //用scanf比较方便,不用专门处理字符串
        //用scanf记得变量前加美元符号!
        scanf("%d:%d:%d %d %d", &t1, &t2, &t3, &player[i].playtime, &player[i].vipp);
        player[i].arrtime = t1 * 3600 + t2 * 60 + t3;
        player[i].statime = close + 1;  //no
        player[i].playtime = NoOvertime(player[i].playtime * 60);
    }

    cin >> k >> m; //k张桌子,其中m张vip桌
    for(int i = 0; i < k; i++) //初始化为0
    {
        table[i].time = 0;
        table[i].vipt = 0;
        server[i] = 0;
    }
    for(int i = 0; i < m; i++) //记录哪几张桌子是VIP桌
    {
        int viptnum;
        cin >> viptnum;
        table[viptnum - 1].vipt = 1; //结构体数组里是从0开始的
    }

    heapsort(player, n); //堆排序

    for(int i = 0; i < n; i++)
    {
        if(player[i].arrtime > start) //既筛除了八点前的客人,后面又更新为当前时间
        {
            for(int j = 0; j < k; j++)
            {
                table[j].time = table[j].time - (player[i].arrtime - start); //所有table经过arrtime-nowtime时长
                if(table[j].time < 0) //不需要等待的客人
                {
                    table[j].time = 0;
                }
            }
            start = player[i].arrtime; //如果当前时刻该顾客还未到,时间调到顾客到达时间
        }

        int fastest = 0; //最快结束的桌子
        for(int j = 1; j < k; j++)
        {
            if(table[j].time < table[fastest].time)
            {
                fastest = j;
            }
        }
        start += table[fastest].time; //从桌子最快结束时间开始玩

        if(start >= close) //开始玩的时间超过了关门时间
        {
            n = i; //后面的客人都超时,输出时只输出不超时的客人
            break;
        }

        int t = table[fastest].time; //直接写在t位置,测试点1678答案错误
        for(int j = 0; j < k; j++)
        {
            table[j].time -= t; //所有桌子被占用时间根据当前时间的变化调整
        }

        //空桌子是vip桌时,查看等待队列里有没有vip客人
        int viptable = findemptytable(1);
        int vipplayer = findvip(i, start);

        int emptytable;
        if(viptable != -1 && vipplayer != -1)
        {
            if(i != vipplayer)
            {
                for(int j = vipplayer; j > i; j--) //通过依次与前面的客人交换位置达到移动到最前面的目的
                {
                    struct Player tem = player[j]; 
                    player[j] = player[j - 1];
                    player[j - 1] = tem;
                }
            }
            emptytable = viptable;
        }
        else
        {
            emptytable = findemptytable(0);
            if(emptytable == -1)
            {
                while(1);
            }
        }

        player[i].statime = start;
        table[emptytable].time = player[i].playtime;
        server[emptytable]++;
    }

    int wait;
    for(int i = 0; i < n; i++)
    {
        if(player[i].statime >= close)
        {
            while(1);
        }
        printf("%02d:%02d:%02d %02d:%02d:%02d", player[i].arrtime / 3600, (player[i].arrtime / 60) % 60, player[i].arrtime % 60, player[i].statime / 3600, (player[i].statime / 60) % 60, player[i].statime % 60);
        wait = player[i].statime - player[i].arrtime;
        wait = wait / 60 + (wait % 60) / 30;
        cout << " " << wait << endl;
    }

    for(int i = 0; i < k - 1; i++) //最后没有空格
    {
        cout << server[i] << " ";
    }
    cout << server[k - 1];

    return 0;
}

运行结果

请添加图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值