1026 Table Tennis (PAT甲级)

终于完成了PAT甲级171道题!可以说95%以上都是独立思考解出来的吧,还是挺开心的。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

这道题折磨了我很久,最后测试点3还是过不了,后来网上搜了是晚上9点以后的客人不服务。

v代表vip客人,nv代表普通客人。

我的解法可能比较复杂,第52行和第54行v.push_back(guest(INF, 0));是为了避免后面需要判断v[a]和nv[b]是否越界。

我的解法的总体结构是:

if(vip客人先到){
    if(台子最早空出来的时间早于vip客人到达时间){
        先看vip台子是否有早于vip客人到达时间的;
        没有的话检查所有台子,使用第一个已经空出来的台子。
    } else{
        使用最早空出来的台子;
    }
} else{
    if(台子最早空出来的时间早于普通客人到达时间){
        使用第一张已经空出来的台子;
    } else{
        if(最早空出来的台子是vip台子并且空出来的时间晚于第一个vip客人到达时间){
            vip客人使用最早空出来的台子;
        } else{
            普通客人使用最早空出来的台子;
        }
    }
}

具体代码如下:

#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
const int INF = 24 * 3600;

struct table{
    int open = 8 * 3600;
    int nbr = 0;
};

struct guest{
    int arrive;
    int play;
    guest(int _arrive, int _play): arrive(_arrive), play(_play){}
};

struct node{
    int arrive;
    int play;
    int wait;
    node(int _arrive, int _play, int _wait): arrive(_arrive), play(_play), wait(_wait){}
};

int N, M, K, hh, mm, ss, play, tag, t, a, b, minn, pivot;
std::vector<table> vec;
std::vector<guest> v, nv;
std::set<int> vip;
bool flag;
std::vector<node> ans;

bool cmp(const guest &m, const guest &n){
    return m.arrive < n.arrive;
}

int main(){
    scanf("%d", &N);
    for(int i = 0; i < N; ++i){
        scanf("%d:%d:%d %d %d", &hh, &mm, &ss, &play, &tag);
        if(hh >= 21){
            continue;
        }
        if(tag == 1){
            v.push_back(guest(hh * 3600 + mm * 60 + ss, 60 * std::min(play, 120)));
        } else{
            nv.push_back(guest(hh * 3600 + mm * 60 + ss, 60 * std::min(play, 120)));
        }
    }
    N = v.size() + nv.size();
    sort(v.begin(), v.end(), cmp);
    v.push_back(guest(INF, 0));
    sort(nv.begin(), nv.end(), cmp);
    nv.push_back(guest(INF, 0));
    scanf("%d %d", &K, &M);
    vec.resize(K);
    for(int i = 0; i < M; ++i){
        scanf("%d", &t);
        vip.insert(t - 1);
    }
    a = 0;
    b = 0;
    for(int i = 0; i < N; ++i){
        minn = INF;
        for(int j = 0; j < K; ++j){
            if(vec[j].open < minn){
                minn = vec[j].open;
                pivot = j;
            }
        }
        if(minn >= 21 * 3600){
            break;
        }
        if(v[a].arrive <= nv[b].arrive){
            if(minn <= v[a].arrive){
                ans.push_back(node(v[a].arrive, v[a].arrive, 0));
                flag = false;
                for(auto c : vip){
                    if(vec[c].open <= v[a].arrive){
                        vec[c].open = v[a].arrive + v[a].play;
                        vec[c].nbr++;
                        a++;
                        flag = true;
                        break;
                    }
                }
                if(flag){
                    continue;
                }
                for(int j = 0; j < K; ++j){
                    if(vec[j].open <= v[a].arrive){
                        vec[j].open = v[a].arrive + v[a].play;
                        vec[j].nbr++;
                        a++;
                        break;
                    }
                }
            } else{
                ans.push_back(node(v[a].arrive, vec[pivot].open, (vec[pivot].open - v[a].arrive + 30) / 60));
                vec[pivot].open += v[a].play;
                vec[pivot].nbr++;
                a++;
            }
        } else{
            if(minn <= nv[b].arrive){
                ans.push_back(node(nv[b].arrive, nv[b].arrive, 0));
                for(int j = 0; j < K; ++j){
                    if(vec[j].open <= nv[b].arrive){
                        vec[j].open = nv[b].arrive + nv[b].play;
                        vec[j].nbr++;
                        b++;
                        break;
                    }
                }
            } else{
                if(vip.find(pivot) != vip.end() && vec[pivot].open >= v[a].arrive){
                    ans.push_back(node(v[a].arrive, vec[pivot].open, (vec[pivot].open - v[a].arrive + 30) / 60));
                    vec[pivot].open += v[a].play;
                    vec[pivot].nbr++;
                    a++;
                } else{
                    ans.push_back(node(nv[b].arrive, vec[pivot].open, (vec[pivot].open - nv[b].arrive + 30) / 60));
                    vec[pivot].open += nv[b].play;
                    vec[pivot].nbr++;
                    b++;
                }
            }
        }
    }
    for(int i = 0; i < ans.size(); ++i){
        printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", ans[i].arrive / 3600, ans[i].arrive % 3600 / 60, ans[i].arrive % 60,
            ans[i].play / 3600, ans[i].play % 3600 / 60, ans[i].play % 60, ans[i].wait);
    }
    for(int i = 0; i < K; ++i){
        printf("%d%s", vec[i].nbr, i == K - 1 ? "\n" : " ");
    }
    return 0;
}

题目如下:

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值