codeforces#333 div2 B. Approximating a Constant Range

Source: Internet
Author: User

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

Two ways of doing this problem,

Sliding window + segment tree query interval max:

#include <bits/stdc++.h>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intInf= (1<< in);structsegtree{structNode {intL,r; intMin,max;    }; Node T[MAXN<<2]; voidPUSH_UP (intRT) {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); }    voidBuildint*a,intLintRintRT) {T[RT].L=l; T[rt].r=R; if(l==R) {T[rt]. Max=T[RT]. min=A[l]; return; }        intM= (l+r) >>1; Build (A,l,m,rt<<1); Build (A,m+1,r,rt<<1|1);    PUSH_UP (RT); }    intQuerymax (intLintRintRT) {        if(L&LT;=T[RT].L&AMP;&AMP;T[RT].R&LT;=R)returnT[rt].        Max; intM= (T[RT].L+T[RT].R) >>1; intres=-INF; if(l<=m) Res=max (Res,querymax (l,r,rt<<1)); if(r>m) Res=max (Res,querymax (l,r,rt<<1|1)); returnRes; }    intQuerymin (intLintRintRT) {        if(L&LT;=T[RT].L&AMP;&AMP;T[RT].R&LT;=R)returnT[rt].        Min; intM= (T[RT].L+T[RT].R) >>1; intres=INF; if(l<=m) Res=min (Res,querymin (l,r,rt<<1)); if(r>m) Res=min (Res,querymin (l,r,rt<<1|1)); returnRes; }}; Segtree Sgt;intA[maxn],n;intMain () {//freopen ("In.txt", "R", stdin);     while(cin>>N) {REP (i,1, N) scanf ("%d",&A[i]); Sgt.build (A,1N1); intPre=1; intans=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

Sliding window + Monotone queue maintenance sliding window maximum:

#include <bits/stdc++.h>#defineREP (I,A,B) for (int i=a;i<=b;i++)#defineMS0 (a) memset (A,0,sizeof (a))using namespaceStd;typedefLong Longll;Const intmaxn=1000100;Const intinf=1<< in;d eque<int>q1,q2;intN,A[MAXN];intMain () {Freopen ("In.txt","R", stdin);  while(cin>>N) {REP (i,1, N) scanf ("%d",&A[i]); intPre=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

First monotone Queue = =

codeforces#333 div2 B. Approximating a Constant Range

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.