1026 Table Tennis (30 分)-------C语言

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:

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

这题的难点在于逻辑判断上面,思路我是参考的一位大佬的代码:

https://blog.csdn.net/wsxyh1071652438/article/details/82469628

如果等待队列非空
        如果一个桌子空闲了出来
            如果当前的桌子是vip,则优先选择等待队列中的vip
            如果当前的桌子不是vip,但是可能和该桌子同时结束的另一个桌子号码较大,但是该大号桌子是vip,选择这个vip桌子
                如果下一个玩家是vip,则优先选择空闲桌子中是vip且号码较小的
                如果下一个玩家不是vip,则直接选择该桌子。
    如果等待队列是空的,
        如果到达了一个玩家
            如果该玩家是vip
                如果空闲桌子中存在vip桌子,则优先选择空闲桌子中是vip且号码较小的
                如果空闲桌子中无vip桌子,则优先选择空闲桌子号码较小的
            如果该玩家不是vip,则选择空闲桌子中号码较小的。

这位大佬的逻辑思路完全没问题的,很清晰也很全面,但我发现他的代码有几处小的逻辑漏洞,稍微改了改(格式不太好看,大家复制到IDE上看着更舒服点)

//1026 Table Tennis (30 分)
#include<stdio.h>
#include<algorithm>
#include<vector>
#define MaxTime 65535
using namespace std;
struct Customer{
    int StartTime; //开始使用时间 
    int ArriveTime;  //到达时间 
    int UseTime;   //使用需要的时间 
    int tag;
    int serve;    //有没有被服务过,因为vip不遵循顺序使用,所以需要这项 
};
struct Table{
    int cnt;    //该桌子服务过的玩家数量 
    int EndTime;   //桌子空闲的时间 
    int vip;   //是否是vip桌子 
};
bool cmp1(struct Customer a, struct Customer b){
    return a.ArriveTime < b.ArriveTime;
}
bool cmp2(struct Customer a, struct Customer b){
    return a.StartTime < b.StartTime;
}
vector <struct Customer> people;
vector <struct Table> table;
int N, K, M;
void Table_Tennis(void);
int FindVip(int index, int MinEndTime);
void update(int PersonId, int TableId);

int main()
{
    scanf("%d", &N);
    int i, j;
    for(i=0;i<N;i++){
        int hour, minute, second, use, tag;
        scanf("%d:%d:%d%d%d", &hour, &minute, &second, &use, &tag);
        use = (use>120)? 7200 : use*60;
        int arrive = (hour-8)*3600 + minute*60 + second;
        people.push_back({0, arrive, use, tag, 0});
    }
    scanf("%d%d", &K, &M);
    for(i=0;i<K;i++){
        table.push_back({0,0,0});
    }
    for(i=0;i<M;i++){
        int number;
        scanf("%d", &number);
        table[number-1].vip = 1;
    }
    sort(people.begin(), people.end(), cmp1);
    Table_Tennis();
    sort(people.begin(), people.end(), cmp2);
    for(i=0;i<N;i++){
        if(people[i].serve){
            int WaitTime = (int)(1.0*(people[i].StartTime - people[i].ArriveTime)/60 + 0.5);
            printf("%02d:%02d:%02d %02d:%02d:%02d

",people[i].ArriveTime/3600+8,people[i].ArriveTime%3600/60,people[i].ArriveTime%60,\
             people[i].StartTime/3600+8,people[i].StartTime%3600/60,people[i].StartTime%60);
            printf("%d\n", WaitTime);
        }
    }
    for(i=0;i<K;i++){
        printf("%d", table[i].cnt);
        if(i!=K-1) printf(" ");
    }
}

void Table_Tennis(void)
{
    int i, j;
    for(i=0;i<N;){
        int MinEndTime = MaxTime, MinIndex;
        for(j=0;j<K;j++){
            if(table[j].EndTime<MinEndTime){
                MinEndTime = table[j].EndTime;          //找到最先空闲且编号最小的桌子 
                MinIndex = j;
            }
        }
        if(MinEndTime>=46800 || people[i].ArriveTime>=46800) break;
        int PersonId = i, TableId = MinIndex;
        if(people[i].ArriveTime <= MinEndTime){   //如果最先空闲的桌子之前有玩家来了,这就是等待队列 
            if(table[MinIndex].vip){               //最先空闲的桌子是vip桌,那就优先给等待队列中的vip玩家 
                int vipid = FindVip(i, MinEndTime);
                if(vipid!=-1) PersonId = vipid;
            }
            else if(people[i].tag){             //等待的玩家是vip,先要找到和 MinIndex同时结束的vip桌,没有就还是选MinIndex 
                for(j=0;j<K;j++){
                    if(table[j].vip && table[j].EndTime==MinEndTime){   
      /*注意这个地方,一定是等于 MinEndTime,那位大佬的代码逻辑漏洞就在这儿,因为等待的玩家是vip。 如果不是和MinIndex结束的vip桌,他不会不去MinIndex,而去等后面的vip桌*/ 
                        TableId = j;
                        break;
                    } 
                }
            }
        }
        else{             //玩家在最先空闲的桌子后来,即没有等待队列 
            for(j=0;j<K;j++){               //这段代码不能少,虽然少了它测试也能过,但这是测试的漏洞,但逻辑上这个必须有 
                if(table[j].EndTime<=people[i].ArriveTime){    
    //玩家在最先空闲的时间之后来,但玩家来的时候可能有其他比 MinIndex编号更小的桌子空闲了 
          
         TableId = j;
                    break;
                }
            }
            if(people[i].tag){
                for(j=0;j<K;j++){
                    if(table[j].vip && table[j].EndTime<=people[i].ArriveTime){
                        TableId = j;
                        break;
                    }
                }
            }
        }
        update(PersonId, TableId);
        while(i<N && people[i].serve) i++;
    }
}

int FindVip(int index, int MinEndTime)
{
    for(int i=index;i<N && people[i].ArriveTime<=MinEndTime;i++){
        if(people[i].tag && !people[i].serve) return i;
    }
    return -1;
}

void update(int PersonId, int TableId)
{
    people[PersonId].StartTime = (table[TableId].EndTime > people[PersonId].ArriveTime)? table[TableId].EndTime : people[PersonId].ArriveTime;
    people[PersonId].serve = 1;
    table[TableId].cnt++;
    table[TableId].EndTime = people[PersonId].StartTime + people[PersonId].UseTime;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值