1026 Table Tennis 思路分享

【更多PAT题解】

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 privilege 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

分析:

  • 牛客网的样例 需要判断服务时间相同时 会员排在前面 PAT没有这个测试点
  • 具体一些坑点 其他博主都提到了 如果一直卡在测试点过不去 可以去牛客网提交 ,会提供未通过的样例

具体思路

  1. 如果是VIP会员

    • 判断是否有空闲的VIP桌子,如果有空闲的VIP桌子,分配给VIP会员
    • 如果没有空闲的VIP桌子,找一张最早空闲,且编号最小的桌子,分配给VIP会员
  2. 如果是普通会员

    找一张最早空闲,且编号最小的桌子

    1. 如果这张桌子是VIP桌子

      • 判断队伍中第一个没有被服务的VIP会员是否已经在等待,如果在等待,将桌子分配给VIP会员
      • 如果没有被服务过的VIP会员在等待,把桌子分配给普通会员
    2. 如果这张桌子是普通桌子

      分配给当前的普通会员

感谢柳婼大神的代码,提供了很大的帮助,不过柳婼大神的代码,有很多的逻辑错误

CODE:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

#define OPEN_TIME 28800
#define CLOSE_TIME 75600
using namespace std;
struct customer {
    int arr_time = OPEN_TIME;   // 到达时间
    int ser_time;               // 服务时间
    float wait_time = 0;        // 等待时间
    int play_time = 0;          // 训练时间
    int vip = 0;                // 是否vip
    bool served = false;        // 是否被服务过
};
struct table {
    int now_time = OPEN_TIME;   // 球桌空闲时间
    int count = 0;              // 服务人数
    bool vip;                   // 是否为VIP桌
};
vector<customer> customers;
vector<table> tables;

bool cmp1(customer a, customer b) { return a.arr_time < b.arr_time; }
bool cmp2(customer a, customer b) { return a.ser_time < b.ser_time; }

int findNextVip(int vipId) {    // 查询队伍中第一个没有被服务的VIP
    vipId++;
    while (vipId < customers.size() && !customers[vipId].vip) vipId++;
    return vipId >= customers.size() ? 0 : vipId;   // 防止越界
}

void alloc(int id, int table_id) {     // 分配桌子
    if (tables[table_id].now_time <= customers[id].arr_time)
        customers[id].ser_time = customers[id].arr_time;
    else
        customers[id].ser_time = tables[table_id].now_time;
    tables[table_id].now_time = customers[id].ser_time + customers[id].play_time;
    tables[table_id].count++;
    customers[id].wait_time = round((customers[id].ser_time - customers[id].arr_time) / 60.0);
    customers[id].served = true;
}

int main() {
    int num_pairs, num_tables, num_vip;
    scanf("%d", &num_pairs);
    for (int i = 0; i < num_pairs; ++i) {
        customer temp;
        int h, m, s;
        scanf("%d:%d:%d %d %d", &h, &m, &s, &temp.play_time, &temp.vip);
        temp.arr_time = h * 3600 + m * 60 + s;                                          // 统一转换成秒
        temp.play_time = temp.play_time <= 120 ? temp.play_time * 60 : 120 * 60;        // 最多训练2个小时
        customers.push_back(temp);
    }
    scanf("%d %d", &num_tables, &num_vip);
    tables.resize(num_tables + 1);
    for (int i = 0; i < num_vip; ++i) {     // 读入VIP桌子
        int vip_table;
        scanf("%d", &vip_table);
        tables[vip_table].vip = true;
    }
    sort(customers.begin(), customers.end(), cmp1);
    int i = 0;
    int vip_id = findNextVip(-1);
    while (i < num_pairs) {
        if (customers[i].served) {  // 被服务过
            i++;
            continue;
        }
        int min_table_id = -1;  // 空闲且编号最小的桌子
        int min = 99999999;
        for (int j = 1; j < num_tables + 1; ++j) {
            if (tables[j].now_time < min) {     // 最小的桌子
                min = tables[j].now_time;
                min_table_id = j;
            }
            if (tables[j].now_time <= customers[i].arr_time) {      // 存在空闲的桌子
                min_table_id = j;
                break;
            }
        }
        // 如果最早空闲的桌子都超过21点 排队结束
        if (tables[min_table_id].now_time >= CLOSE_TIME || customers[i].arr_time >= CLOSE_TIME)
            break;
        if (customers[i].vip == 1) {    // 用户是vip
            // 查找是否有空闲的vip桌子
            int table_id = -1;
            for (int j = 1; j < num_tables + 1; ++j) {
                if (tables[j].vip && tables[j].now_time <= customers[i].arr_time) {
                    table_id = j;
                    break;
                }
            }
            if (i == vip_id) vip_id = findNextVip(vip_id);  // 更新下一个VIP
            if (table_id == -1) {  // 没有空闲的VIP桌子 分配普通桌子
                alloc(i++, min_table_id);
            } else // 有空闲的VIP桌子 分配vip桌
                alloc(i++, table_id);
        } else { // 不是VIP
            if (tables[min_table_id].vip) { // 如果是vip桌子
                // 有VIP用户等待 优先分配
                if (!customers[vip_id].served && customers[vip_id].arr_time <= tables[min_table_id].now_time) {
                    alloc(vip_id, min_table_id);
                    vip_id = findNextVip(vip_id);
                    continue;
                }
            }   // 分配给该用户
            alloc(i++, min_table_id);
        }
    }
    sort(customers.begin(), customers.end(), cmp2);
    for (int j = 0; j < customers.size(); ++j) {
        if (customers[j].served) {
            printf("%02d:%02d:%02d ", customers[j].arr_time / 3600, (customers[j].arr_time % 3600) / 60,
                   (customers[j].arr_time % 3600) % 60);
            printf("%02d:%02d:%02d %.0f\n", customers[j].ser_time / 3600, (customers[j].ser_time % 3600) / 60,
                   (customers[j].ser_time % 3600) % 60, customers[j].wait_time);
        }
    }
    for (int j = 1; j < num_tables + 1; ++j) {
        if (j != 1) printf(" ");
        printf("%d", tables[j].count);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值