蛋疼度度熊

百度之星初赛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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值