传送门
【题目分析】
哇这个名字这的。。。。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;
}