Codeforces #594 div1 C/div2 E – Queue in the Train

Topic links: https://codeforces.com/contest/1239/problem/C

Meaning of the questions: There are n passengers on the train, according to numbers from 1 to n, i number of people will want to go to fetch water in ti minutes. Water tank can only use for one passenger, per passenger will use p minutes. When a passenger wants to fetch water, he would look at the numbers in front of him is not all the passengers in their seats, if someone is not on the seat, he will continue to sit and wait, or else he would go to fetch water line. A time when there are few passengers want to fetch water at the same time, the minimum number of passengers went to fetch water, others will continue to sit and wait, the calculation of water time each passenger kick.

Practice: simulation.In accordance with the meaning of the questions can be simulated, see the specific implementation of the code. First, we need a priority queue judge who should fetch water on the seat, then we need a priority queue wait seated person is determined, then we need to simulate a queue of people queuing, and finally we need a data structure of the recording empty seats. In accordance with the meaning of the questions can be simulated, see the specific implementation code.

Reference Code:

#include <iostream>
#include <queue>
#include <set>

using namespace std;
struct passenger
{
    int pos, startime;
} psger[100005];

struct cmp_waiting
{
    bool operator()(const passenger &x, const passenger &y)
    {
        return x.pos > y.pos;
    }
};

struct cmp_sitting
{
    bool operator()(const passenger &x, const passenger &y)
    {
        if (x.startime == y.startime)
            return x.pos > y.pos;
        else
            return x.startime > y.startime;
    }
};

priority_queue<passenger, vector<passenger>, cmp_sitting> sitting;
priority_queue<passenger, vector<passenger>, cmp_waiting> waiting;
queue<passenger> queuing;
set<int> emptyseat;
long long ret[100005];

int main()
{
    int n;
    long long p, now;
    cin >> n >> p;
    for (int i = 1; i <= n; ++i)
    {
        cin >> psger[i].startime;
        psger[i].pos = i;
        sitting.push(psger[i]);
    }
    now = sitting.top().startime;
    emptyseat.insert(n + 1);
    while (!sitting.empty() || !queuing.empty() || !waiting.empty())
    {
        if (!queuing.empty())
        {
            passenger fir = queuing.front();
            queuing.pop();
            now += p;
            ret[fir.pos] = now;
            while (!sitting.empty() && sitting.top().startime <= now)
            {
                passenger sec = sitting.top();
                if (sec.pos < *emptyseat.begin())
                {
                    emptyseat.insert(sec.pos);
                    queuing.push(sec);
                }
                else
                    waiting.push(sec);
                sitting.pop();
            }
            emptyseat.erase(fir.pos);
        }
        if (queuing.empty() && waiting.empty())
            now = max(now, (long long) sitting.top().startime);
        while (!sitting.empty() && sitting.top().startime <= now)
        {
            waiting.push(sitting.top());
            sitting.pop();
        }
        int id = *emptyseat.begin();
        if (!waiting.empty() && waiting.top().pos < id)
        {
            queuing.push(waiting.top());
            emptyseat.insert(waiting.top().pos);
            waiting.pop();
        }
    }
    for (int i = 1; i <= n; ++i)
        cout << ret[i] << " ";
    return 0;
}

Guess you like

Origin www.cnblogs.com/mapleaves/p/11808907.html