Codeforces Round #594 (Div. 1) C. Queue in the Train 模擬
C. Queue in the Train
There are ?? seats in the train's car and there is exactly one passenger occupying every seat. The seats are numbered from 1 to ?? from left to right. The trip is long, so each passenger will become hungry at some moment of time and will go to take boiled water for his noodles. The person at seat ?? (1≤??≤??) will decide to go for boiled water at minute ????.
Tank with a boiled water is located to the left of the 1-st seat. In case too many passengers will go for boiled water simultaneously, they will form a queue, since there can be only one passenger using the tank at each particular moment of time. Each passenger uses the tank for exactly ?? minutes. We assume that the time it takes passengers to go from their seat to the tank is negligibly small.
Nobody likes to stand in a queue. So when the passenger occupying the ??-th seat wants to go for a boiled water, he will first take a look on all seats from 1 to ???1. In case at least one of those seats is empty, he assumes that those people are standing in a queue right now, so he would be better seating for the time being. However, at the very first moment he observes that all seats with numbers smaller than ?? are busy, he will go to the tank.
There is an unspoken rule, that in case at some moment several people can go to the tank, than only the leftmost of them (that is, seating on the seat with smallest number) will go to the tank, while all others will wait for the next moment.
Your goal is to find for each passenger, when he will receive the boiled water for his noodles.
Input
The first line contains integers ?? and ?? (1≤??≤100000, 1≤??≤109) — the number of people and the amount of time one person uses the tank.
The second line contains ?? integers ??1,??2,…,???? (0≤????≤109) — the moments when the corresponding passenger will go for the boiled water.
Output
Print ?? integers, where ??-th of them is the time moment the passenger on ??-th seat will receive his boiled water.
Example
input
5 314
0 310 942 628 0
output
314 628 1256 942 1570
Note
Consider the example.
At the 0-th minute there were two passengers willing to go for a water, passenger 1 and 5, so the first passenger has gone first, and returned at the 314-th minute. At this moment the passenger 2 was already willing to go for the water, so the passenger 2 has gone next, and so on. In the end, 5-th passenger was last to receive the boiled water.
題意
火車上面一共有n個(gè)人要打水,每個(gè)人會(huì)選擇在t[i]的時(shí)間去打水,每次打水需要花費(fèi)p的代價(jià)。每個(gè)人都不喜歡排隊(duì),如果這個(gè)人發(fā)現(xiàn)他前面有人沒在座位上,他就不會(huì)去打水;如果有多個(gè)人同時(shí)去打水,序號(hào)小的會(huì)優(yōu)先去排隊(duì);排隊(duì)按照先后順序打水。
題解
純模擬題,維護(hù)一個(gè)打水的優(yōu)先隊(duì)列,維護(hù)一個(gè)排隊(duì)的隊(duì)列,維護(hù)一個(gè)等待序列。
然后模擬模擬就好了。 那么問題來了,這道題真的值div1 C的難度嗎
代碼
#include<bits/stdc++.h>
using namespace std;
priority_queue<pair<long long,int> >Q1;
priority_queue<int> Q2;
set<int> EmptyQueue;
queue<int>Q3;
const int maxn = 1e5+7;
int n;
long long p;
long long ans[maxn];
int main(){
scanf("%d%lld",&n,&p);
for(int i=0;i<n;i++){
long long t;
scanf("%lld",&t);
Q1.push(make_pair(-t,-i));
}
long long now = 0;
while(!Q1.empty()||!Q2.empty()||!Q3.empty()){
if(Q3.empty()&&Q2.empty()){
now = max(now,-Q1.top().first);
}
while(!Q1.empty()&& -Q1.top().first<=now+p){
if(EmptyQueue.empty()||(*EmptyQueue.begin()>-Q1.top().second)){
Q3.push(-Q1.top().second);
EmptyQueue.insert(-Q1.top().second);
} else {
Q2.push(Q1.top().second);
}
Q1.pop();
}
if(!Q3.empty()){
ans[Q3.front()]=now+p;
now=now+p;
EmptyQueue.erase(Q3.front());
Q3.pop();
}
if(!Q2.empty()&&Q3.empty()){
Q3.push(-Q2.top());
EmptyQueue.insert(-Q2.top());
Q2.pop();
}
}
for(int i=0;i<n;i++)
cout<<ans[i]<<" ";
cout<<endl;
}

浙公網(wǎng)安備 33010602011771號(hào)