百度之星初赛B赛T6
先把这些线段以左端点升序排列,然后把重叠的,重合的,还有一些什么特殊的情况都处理出来,把连续的几段处理成一段(dalao说可以不处理)
然后开一个队列,从前往后压入队列,如果出现了断开的部分,就用m比较,如果小于m,就把m减去这一段的长度,然后压入队列,如果大于m,就弹出队首元素,增加m,直到这一段的长度小于m进行更新,在这期间,要不断的进行更新ans的值,
要注意的是一个测试点有许多数据,所以while输入
然后我再while那里一直TLE………
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
struct H{
int l;
int r;
}day[1001000],thn[1001000],now;
int comp(H a,H b)
{
return a.l<b.l;
}
queue <int> q;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
{
int l,r;
scanf("%d%d",&day[i].l,&day[i].r);
}
sort(day+1,day+n+1,comp);
now.l=day[1].l;
now.r=day[1].r;
int tot=0;
for(int i=1;i<n;i++)
{
if(now.r>=day[i+1].l)
{
now.r=max(now.r,day[i+1].r);
continue;
}
else
{
thn[++tot].l=now.l;
thn[tot].r=now.r;
now.l=day[i+1].l;
now.r=day[i+1].r;
}
}
int maxn=0,ans=0;
q.push(thn[1].r-thn[1].l+1);
maxn=thn[1].r-thn[1].l+1;
for(int i=2;i<tot;i++)
{
if(thn[i+1].l-thn[i].r+1<=m)
{
m-=thn[i+1].l-thn[i].r+1;
maxn+=(m+thn[i+1].r-thn[i+1].l-1);
q.push(thn[i+1].l-thn[i].r+1);
q.push(thn[i+1].r-thn[i+1].l+1);
}
else
{
while(thn[i+1].l-thn[i].r+1>m)
{
ans=max(ans,maxn);
q.pop();
m+=q.front();
q.pop();
}
m-=thn[i+1].l-thn[i].r+1;
maxn+=(m+thn[i+1].r-thn[i+1].l-1);
q.push(thn[i+1].l-thn[i].r+1);
q.push(thn[i+1].r-thn[i+1].l+1);
}
}
cout<<ans<<endl;
}
return 0;
}