第一届“文翁杯”现场竞技赛 T2 蜀石经(优先队列+模拟)

传送门

【题目分析】

哇这个名字这的。。。。qwq,气球赛才308tql,垫底了啊。

这道题n^2的暴力挺好想的,考试的时候先提交了一个玄学复杂度,但最高应该是n^2的程序,即先全部压进优先队列中,然后每次出队元素更新所有影响的元素再压回去就行了,最后应该是因为没卡所以过掉70pts。

其实正解也很简单,记录一个时间戳以更新答案,每次处理完一个人后将所有等待的压入队列更新答案就行了。

记得队列空了有可能是没有等待的人了,所以记个pos判一下是否处理完所有人。

【代码~】

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+10;

int n,tim,ans,now;
int pos=1;
int st[MAXN],ed[MAXN];
struct Stu{
	int a,t,stu,id;
	friend bool inline operator<(const Stu &a,const Stu &b){
		return a.stu<b.stu;
	}
}stu[MAXN],x;
priority_queue<Stu> q;

int Read(){
	int i=0,f=1;
	char c;
	for(c=getchar();(c>'9'||c<'0')&&c!='-';c=getchar());
	if(c=='-')
	  f=-1,c=getchar();
	for(;c>='0'&&c<='9';c=getchar())
	  i=(i<<3)+(i<<1)+c-'0';
	return i*f;
}

int cmp(const Stu &a,const Stu &b){
	if(a.a!=b.a)
	  return a.a<b.a;
	return a.stu>b.stu;
}

int main(){
	n=Read();
	for(int i=1;i<=n;i++){
		stu[i].a=Read(),stu[i].t=Read();
		stu[i].stu=n-i;
	}
	sort(stu+1,stu+n+1,cmp);
	for(int i=1;i<=n;i++)
	  stu[i].id=i;
	while(pos<=n){
		tim=now;
		while(stu[pos].a<=now){
			if(pos>n)
			  break;
			q.push(stu[pos]);
			st[pos]=stu[pos].a;
			pos++;
		}
		if(q.empty()){
			if(pos>n)
			  break;
			q.push(stu[pos]);
			st[pos]=stu[pos].a;
			tim=stu[pos].a;
			pos++;
		}
		x=q.top();
		q.pop();
		now=tim+x.t;
		ed[x.id]=tim;
	}
	while(!q.empty()){
		tim=now;
		x=q.top();q.pop();
		ed[x.id]=now;
		now=tim+x.t;
	}
	for(int i=1;i<=n;i++)
	  ans=max(ans,ed[i]-st[i]);
	cout<<ans;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值