CSUST 4003-你真的会泡面吗?(优先队列模拟)

题目链接:http://acm.csust.edu.cn/problem/4003
博客园食用链接:https://www.cnblogs.com/lonely-wind-/p/13417505.html

Description

高铁上有 n n 个人第 i i 个人编号为 i i 他在 s i s_i 时刻想去泡泡面,他泡面所需时间为 t i t_i

假设他们从座位到饮水机的时间可以忽略不计。不过只有一台饮水机,所以他们要排队泡面。

特别的,泡面可以分很多次泡完。比如你泡面需要 3 m i n 3min ,可以先泡 1 m i n 1min ,那么接下来你所需泡面的时间为 2 m i n 2min

泡面机服务策略:

首先先来的人可以优先泡面;对于同时到达的人,优先服务剩余所需泡面最短的人;对于剩余时间相同的人,则优先服务编号较小的人。

意思就是如果某时刻来了某个人 x x 泡面所需时间比正在使用饮水机的人 y y 剩余泡面所需时间要短,那么 x x 可以暂时请走 y y ,自己先服务。

n n 个人完成泡面时间为 e i e_i 。请输出 n n 个人的 e i s i t i e_i-s_i-t_i 之和,答案对1000000007取模
1 n 1 e 5 , 1 s i , t i 1 e 9 1\leq n\leq 1e5,1\leq s_i,t_i\leq 1e9

Input
第一行一个整数 n n

接下来nn行每行两个整数,表示 s i , t i s_i,t_i

Output
一行一个整数表示答案。

Sample Input 1
3
0 5
1 2
2 1
Sample Output 1
4

Sample Input 2
4
0 5
1 2
0 2
1 2
Sample Output 2
10

Hint
对于样例1:
e 1 = 8 , e 2 = 3 , e 3 = 4 , i = 1 n e i s i t i = 8 0 5 + 3 1 2 + 4 2 1 = 4 e1=8,e2=3,e3=4,\sum_{i=1}^{n}e_i-s_i-t_i=8-0-5+3-1-2+4-2-1=4

emmm,不清楚这道题的本质,比赛的时候一直贪心QAQ。。。

后来在想了挺久之后才发现好像就是个模拟。。。我们先按照来的时间和泡的时间进行一下双关键字排序。我们服务的既然是最短时间的人,那么我们可以用一个优先队列来维护当前每个人的剩余时间。那么可以模拟如下:

对于第一个人,它可以泡到第二个人来,或者直接泡完。如果它可以直接泡完的话,那么我们就直接将答案加上结束时间,否则的话我们就将第一个人的时间减去已经泡了的时间再丢到队列里面去。如此循环下去直到所有的人都跑完了。那么现在队列里面还有多少个人可以直接一个个取出来加就好了。其模拟代码如下:

int cnt=1;
ll ed=0;
priority_queue<ll,vector<ll>,greater<ll> >q;
q.push(person[1].ti);
ed=person[1].st;
ll ans=0;
while (!q.empty() || cnt<n) {
	if (q.empty()) {q.push(person[++cnt].ti); ed=person[cnt].ti;}
	ll u=q.top();
	q.pop();
	if (cnt<n) {
		ll st=person[cnt+1].st;
		if (st<ed+u) {
			q.push(ed+u-st);
			q.push(person[++cnt].ti);
			ed=st;
		} 
		else { ans=(ans+u)%mod; ed+=u;}
	} 
	else { ans=(ans+u)%mod; ed+=u;}
}

最后我们只需要将答案减去所有人的开始时间和泡的时间就好了。

以下是AC代码:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int mac=1e5+10;
const ll mod=1e9+7;

struct node
{
	ll st,ti;
	bool operator<(const node&a)const{
		if (st!=a.st) return st<a.st;
		if (ti!=a.ti) return ti<a.ti;
	}
}person[mac];

int main()
{
	int n;
	scanf ("%d",&n);
	for (int i=1; i<=n; i++){
		int st,ti;
		scanf ("%d%d",&st,&ti);
		person[i]=node{st,ti};
	}
	sort(person+1,person+1+n);
	int cnt=1;
	ll ed=0;
	priority_queue<ll,vector<ll>,greater<ll> >q;
	q.push(person[1].ti);
	ed=person[1].st;
	ll ans=0;
	while (!q.empty() || cnt<n){
		if (q.empty()) {q.push(person[++cnt].ti); ed=person[cnt].st;}
		ll u=q.top();
		q.pop();
		if (cnt<n) {
			ll st=person[cnt+1].st;
			if (st<ed+u){
				q.push(ed+u-st);
				q.push(person[++cnt].ti);
				ed=st;
			}
			else {ans=(ans+ed+u)%mod; ed+=u;}
		}
		else {ans=(ans+ed+u)%mod; ed+=u;}
	}
	ll sum=0;
	for (int i=1; i<=n; i++)
		sum=(sum+person[i].st+person[i].ti)%mod;
	ans=(ans-sum+mod)%mod;
	printf("%lld\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43906000/article/details/107739654