CF1248E Queue in the Train


題目鏈接

problem

火車上的一列人要去排隊接水。每個人都會在某個特定的時刻口渴。口渴之后他要去排隊接水,如果他前面的座位有人已經在排隊或者正在接水,那么他就不會去排隊。否則他就會去排隊。每個人接水都為一個相同的時間P。問每個人接完水的時間。

solution

其實模擬即可。注意題目的要求。如果一個人口渴的時候已經在排隊的人中最靠前的位置也在他后面,那么他就要去排隊。否則就把他扔到一個按位置從小到大排序的優先隊列里面。然后模擬就行了。

code

#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<ctime>
#include<set>
using namespace std;
typedef long long ll;
const int N = 100010;
ll read() {
	ll x = 0,f = 1;char c = getchar();
	while(c < '0' || c > '9') {
		if(c == '-') f = -1;c = getchar();
	}
	while(c >= '0' && c <= '9') {
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x * f;
}
struct node {
	int pos,w;
}a[N];
bool operator < (const node &A,const node &B) {
	return A.pos > B.pos;
}
priority_queue<node>q;
bool cmp(const node &A,const node &B) {
	return A.w == B.w ? A.pos < B.pos : A.w < B.w;
}
set<int>s;
ll ans[N];
queue<node>tq;
int main() {
	int n = read(),P = read();
	for(int i = 1;i <= n;++i) {
		a[i].pos = i;a[i].w = read();
	}
	
	sort(a + 1,a + n + 1,cmp);
	
	int p = 1;
	ll now = a[1].w;
	s.insert(n + 1);
	while(p <= n || !tq.empty() || !q.empty()) {
		if(!tq.empty()) {
			node k = tq.front();tq.pop();
			now += P;
			ans[k.pos] = now;
			
			while(p <= n && a[p].w < now) {
				if(a[p].pos < *s.begin()) {
					s.insert(a[p].pos);
					tq.push(a[p++]);
				}
				else {
					q.push(a[p++]);
				}
			}
			s.erase(k.pos);
		}
		
		
		if(tq.empty() && q.empty()) 
			now = max(now,(ll)a[p].w);
		
		while(p <= n && a[p].w <= now) q.push(a[p++]);
		int t = *s.begin();
		if(!q.empty() && q.top().pos <= t) {

			tq.push(q.top());
			s.insert(q.top().pos);
			q.pop();
		}
	}
	
	for(int i = 1;i <= n;++i) printf("%I64d ",ans[i]);
	return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM