codeforces#333 div2 B. Approximating a Constant Range

來源:互聯網
上載者:User

標籤:

http://codeforces.com/contest/602/problem/B

這道題的兩種做法,

滑窗+線段樹查詢區間最值:

#include<bits/stdc++.h>#define REP(i,a,b) for(int i=a;i<=b;i++)#define MS0(a) memset(a,0,sizeof(a))using namespace std;typedef long long ll;const int maxn=1000100;const int INF=(1<<29);struct SegTree{    struct Node    {        int l,r;        int Min,Max;    };    Node T[maxn<<2];    void push_up(int rt)    {        T[rt].Max=max(T[rt<<1].Max,T[rt<<1|1].Max);        T[rt].Min=min(T[rt<<1].Min,T[rt<<1|1].Min);    }    void build(int *a,int l,int r,int rt)    {        T[rt].l=l;T[rt].r=r;        if(l==r){            T[rt].Max=T[rt].Min=a[l];            return;        }        int m=(l+r)>>1;        build(a,l,m,rt<<1);        build(a,m+1,r,rt<<1|1);        push_up(rt);    }    int queryMax(int L,int R,int rt)    {        if(L<=T[rt].l&&T[rt].r<=R) return T[rt].Max;        int m=(T[rt].l+T[rt].r)>>1;        int res=-INF;        if(L<=m) res=max(res,queryMax(L,R,rt<<1));        if(R>m) res=max(res,queryMax(L,R,rt<<1|1));        return res;    }    int queryMin(int L,int R,int rt)    {        if(L<=T[rt].l&&T[rt].r<=R) return T[rt].Min;        int m=(T[rt].l+T[rt].r)>>1;        int res=INF;        if(L<=m) res=min(res,queryMin(L,R,rt<<1));        if(R>m) res=min(res,queryMin(L,R,rt<<1|1));        return res;    }};SegTree sgt;int a[maxn],n;int main(){    //freopen("in.txt","r",stdin);    while(cin>>n){        REP(i,1,n) scanf("%d",&a[i]);        sgt.build(a,1,n,1);        int pre=1;        int ans=1;        REP(i,1,n){            while(sgt.queryMax(pre,i,1)-sgt.queryMin(pre,i,1)>1) pre++;            ans=max(ans,i-pre+1);        }        cout<<ans<<endl;    }    return 0;}
View Code

滑窗+單調隊列維護滑窗最值:

#include<bits/stdc++.h>#define REP(i,a,b) for(int i=a;i<=b;i++)#define MS0(a) memset(a,0,sizeof(a))using namespace std;typedef long long ll;const int maxn=1000100;const int INF=1<<29;deque<int> q1,q2;int n,a[maxn];int main(){    freopen("in.txt","r",stdin);    while(cin>>n){        REP(i,1,n) scanf("%d",&a[i]);        int pre=1,ans=1;        while(!q1.empty()) q1.pop_back();        while(!q2.empty()) q2.pop_back();        REP(i,1,n){            while(!q1.empty()&&q1.back()<a[i]) q1.pop_back();            q1.push_back(a[i]);            while(!q2.empty()&&q2.back()>a[i]) q2.pop_back();            q2.push_back(a[i]);            while(q1.front()-q2.front()>1){                if(a[pre]==q1.front()) q1.pop_front();                if(a[pre]==q2.front()) q2.pop_front();                pre++;            }            ans=max(ans,i-pre+1);        }        cout<<ans<<endl;    }    return 0;}
View Code

第一道單調隊列= =

 

codeforces#333 div2 B. Approximating a Constant Range

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.