蛋疼度度熊

百度之星初賽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;
}
發佈了51 篇原創文章 · 獲贊 7 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章