1026 Table Tennis (30)(30 分)

emmmm这个题目emmm,随缘吧,照着敲了一遍答案玩、

#include
#include
#include
#include
using namespace std;
const int K = 111;
const int INF = 1e9 + 10;
struct Player {
    int arrivetime, starttime, traintime;
    bool isvip;
}newplayer;
struct Table {
    int endtime, numserve;
    bool isvip;
}table[K];
vectorplayer;
int convertTime(int h, int m, int s)
{
    return h * 3600 + m * 60 + s;
}
bool cmparrtime(Player a, Player b)
{
    return a.arrivetime < b.arrivetime;
}
bool cmpstarttime(Player a, Player b)
{
    return a.starttime < b.starttime;
}
int nextvip(int vipi)
{
    vipi++;
    while (vipi < player.size() && player[vipi].isvip == 0)vipi++;
    return vipi;
}
void allottable(int pid, int tid)
{
    if (player[pid].arrivetime <= table[tid].endtime)
    {
        player[pid].starttime = table[tid].endtime;
    }
    else
    {
        player[pid].starttime = player[pid].arrivetime;
    }
    table[tid].endtime = player[pid].starttime + player[pid].traintime;
    table[tid].numserve++;
}
int main()
{
    int n, k, m, viptable;
    scanf("%d", &n);
    int sttime = convertTime(8, 0, 0);
    int edtime = convertTime(21, 0, 0);
    for (int i = 0; i < n; i++)
    {
        int h, m, s, trainTime, isVip;
        scanf("%d:%d:%d %d %d", &h, &m, &s, &trainTime, &isVip);
        newplayer.arrivetime = convertTime(h, m, s);
        newplayer.starttime = edtime;
        if (newplayer.arrivetime >= edtime)continue;
        newplayer.traintime = trainTime < 120 ? trainTime * 60 : 7200;
        newplayer.isvip = isVip;
        player.push_back(newplayer);
    }
    scanf("%d%d", &k, &m);
    for (int i = 1; i <= k; i++)
    {
        table[i].endtime = sttime;
        table[i].numserve = table[i].isvip = 0;
    }
    for (int i = 0; i < m; i++)
    {
        scanf("%d", &viptable);
        table[viptable].isvip = 1;
    }
    sort(player.begin(), player.end(), cmparrtime);
    int i = 0, vipi = -1;
    vipi = nextvip(vipi);
    while (i < player.size())
    {
        int idx = -1, minEndtime = INF;
        for (int j = 1; j <= k; j++)
        {
            if (table[j].endtime < minEndtime)
                idx = j, minEndtime = table[j].endtime;
        }
        if (table[idx].endtime >= edtime)break;
        if (player[i].isvip&&i < vipi)
        {
            i++;
            continue;
        }
        if (table[idx].isvip)
        {
            if (player[i].isvip == 1)
            {
                allottable(i, idx);
                if (vipi == i)vipi = nextvip(vipi);
                i++;
            }
            else
            {
                if (vipi < player.size() && player[vipi].arrivetime <= table[idx].endtime)
                {
                    allottable(vipi, idx);
                    vipi = nextvip(vipi);
                }
                else
                {
                    allottable(i, idx);
                    i++;
                }
            }
        }
        else
        {
            if (player[i].isvip == 0)
            {
                allottable(i, idx);
                i++;
            }
            else
            {
                int vipidx = -1, minvipendtime = INF;
                for (int j = 1; j <= k; j++)
                {
                    if (table[j].isvip == 1 && table[j].endtime < minvipendtime)
                    {
                        minvipendtime = table[j].endtime;
                        vipidx = j;
                    }
                }
                if (vipidx != -1 && player[i].arrivetime >= table[vipidx].endtime)
                {
                    allottable(i, vipidx);
                    if (vipi == i)vipi = nextvip(vipi);
                    i++;
                }
                else
                {
                    allottable(i, idx);
                    if (vipi == i)vipi = nextvip(vipi);
                    i++;
                }
            }
        }
    }
    sort(player.begin(), player.end(), cmpstarttime);
    for (int i = 0; i < player.size() && player[i].starttime < edtime; i++)
    {
        int t1 = player[i].arrivetime;
        int t2 = player[i].starttime;
        printf("%02d:%02d:%02d ", t1 / 3600, t1 % 3600 / 60, t1 % 60);
        printf("%02d:%02d:%02d ", t2 / 3600, t2 % 3600 / 60, t2 % 60);
        printf("%.0f\n", round((t2 - t1) / 60.0));
    }
    for (int i = 1; i <= k; i++)
    {
        printf("%d", table[i].numserve);
        if (i != k)printf(" ");
    }
    return 0;
}

你可能感兴趣的:(1026 Table Tennis (30)(30 分))