Cod sursa(job #2302303)

Utilizator PredaBossPreda Andrei PredaBoss Data 14 decembrie 2018 09:44:21
Problema A+B Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <bits/stdc++.h>

using namespace std;
int n,d,t,who,where;
map<int,queue<int> >order;
queue<int>plate;
vector<pair<pair<int,int>,int> >sorted;
vector<pair<int,int> >command;
int ans[100005];
bool ap[100005];
bool add(int when)
{
    bool added=false;
    while(where<n && sorted[where].first.first<=when)
    {
        if(ap[sorted[where].second])
        {
            where++;
            continue;
        }
        ap[sorted[where].second]=true;
        added=true;
        plate.push(sorted[where].first.first+sorted[where].first.second+d);
        order[sorted[where].first.second].push(sorted[where].second);
        where++;
    }
    return add;
}
int main()
{
    ifstream fin("ramen.in");
    ofstream fout("ramen.out");
    fin>>n>>d;
    for(int i=1;i<=n;i++)
    {
        fin>>t>>who;
        command.push_back({t,who});
        sorted.push_back({{t-who,who},i-1});
    }
    sort(sorted.begin(),sorted.end());
    int it=0;
    while(it<n || !plate.empty())
    {
        if(ap[it])
        {
            it++;
            continue;
        }
        t=command[it].first;
        who=command[it].second;
        if(plate.empty())
        {
            order[who].push(it);
            plate.push(t+d);
            ap[it]=true;
            it++;
            continue;
        }
        int i=(order.begin())->second.front();
        int pos=(order.begin())->first;
        if(it<n && plate.front()>=t)
        {
            order[who].push(it);
            plate.push(t+d);
            ap[it]=true;
            it++;
            continue;
        }
        if(add(t))
            continue;
        ans[i]=pos+plate.front();
        (order.begin())->second.pop();
        if(order[pos].empty())
            order.erase(pos);
        plate.pop();
    }
    for(int i=0;i<n;i++)
        fout<<ans[i]<<"\n";
    return 0;
}