1026 Table Tennis (30 point(s))

Ideas:

Use two queues, vip queue and normal queue,

Find the window with the earliest completion time. If it is earlier than the arrival time of the earliest person in the two queues, update the window completion time to the minimum of the two, and then return to the beginning to find the window with the earliest completion time;

If the VIP queue is selected, the window will be searched again, and the VIP window will be selected first;

If the normal queue is selected, use this window;

note:

  1. VIP players, priority allocation of VIP tables
  2. The waiting time is greater than or equal to 30 seconds, advance one minute, and discard if less than 30 seconds;
  3. The duration of the game cannot exceed 120 minutes

1026 Table Tennis (30 point(s))

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.

Example:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<map>
#include<vector>
using namespace std;

int Stamp(string s)
{
    int HH, MM, SS;
    sscanf(s.c_str(), "%d:%d:%d", &HH, &MM, &SS);
    return HH*3600+MM*60+SS;
}

string Time(int stamp)
{
    string s;
    char str[12];
    int HH = stamp / 3600, MM = stamp % 3600 / 60, SS = stamp % 3600 % 60;
    sprintf(str, "%02d:%02d:%02d", HH, MM, SS);
    return string(str);
}

struct table {
    int  count;
    int  finish;
    int  vip;
    bool operator<(const table &rhs) const {
        return finish < rhs.finish;
    }
};

int main()
{
    int N;
    cin >> N;
    map<string, int> Member;
    map<string, int> Vip;
    for(int i = 0; i < N; i++) {
        string arrive;
        int serve, vip;
        cin >> arrive >> serve >> vip;
        if(serve > 120) serve = 120;
        if(vip == 1) Vip[arrive] = serve;
        else Member[arrive] = serve;
    }
    int K, M;
    cin >> K >> M;
    vector<table> Win(K);
    for(int i = 0; i < M; i++) {
        int v;
        cin >> v;
        Win[v-1].vip = 1;
    }
    for(int i = 0; i < N;) {
        auto it = min_element(Win.begin(), Win.end());
        if(it->finish >= Stamp("21:00:00")) break;
        map<string, int> *p;
        if(Vip.empty()) {
            int normStamp = Stamp(Member.begin()->first);
            if(it->finish < normStamp) {
                it->finish = normStamp;
                continue;
            }
            p = &Member;
        } else if(Member.empty()) {
            int vipStamp = Stamp(Vip.begin()->first);
            if(it->finish < vipStamp) {
                it->finish = vipStamp;
                continue;
            }
            p = &Vip;
        } else {
            int vipStamp = Stamp(Vip.begin()->first);
            int normStamp = Stamp(Member.begin()->first);
            if(it->finish < min(vipStamp, normStamp)) {
                it->finish = min(vipStamp, normStamp);
                continue;
            }
            if(vipStamp <= normStamp) p = &Vip;
            else if(it->vip == 1 && vipStamp <= it->finish) p = &Vip;
            else p = &Member;
        }
        string time = p->begin()->first;
        int stamp = Stamp(time);
        if(p == &Vip)
            for(auto t = Win.begin(); t != Win.end(); t++)
                if(t->vip && t->finish <= stamp) { it = t; break; }
        int wait = it->finish - stamp;
        if(wait % 60 >= 30) wait = wait + 60;
        wait = wait / 60;
        cout << time << ' ' << Time(it->finish) << ' ' << wait << endl;
        it->finish += (*p)[time] * 60;
        (*p).erase(time);
        i++, it->count++;
    }
    for(int i = 0; i < K; i++) cout << (i==0 ? "" : " ") << Win[i].count;
}

 

Guess you like

Origin blog.csdn.net/u012571715/article/details/114095113