Tag: is test targe none tin temp comparison number BSP
Title: Http://codeforces.com/contest/602/problem/B
Test Instructions: give a range containing n number, ask to find a maximal continuous sub-range so that the difference between the maximum and minimum value of this sub-interval is not more than 1, and finally output the length of this sub-interval.
Analysis:
Because the number within the interval is only 1, I use FS and FX to indicate whether the interval is an ascending or descending range.
If the ascending interval is met, then the number of the addition interval that meets the condition is compared with the beginning of the interval a[st], if it is large 1 or is equal to en++, until the condition is not satisfied;
So is the fall range ...
If you do not meet the conditions, st++. At the same time also have to re-judge the interval rise or fall, because of inexperience, here card for a long time;
AC Code:
#include <stdio.h>#include<algorithm>using namespacestd;inta[110000];intMain () {intN; scanf ("%d",&N); for(intI=0; I<n; i++) scanf ("%d",&A[i]); intst=0, en=1; intfs=1, fx=1;///the tag fs is the indication of rising, the FX represents a drop intmaxx=-Ten; while(en<=N) {intt=A[st]; if(a[en]-t==1&&fs==1) {FX=0; En++; } Else if(a[en]-t==-1&&fx==1) {FS=0; En++; } Else if(a[en]-t==0) {en++; } Else { inttemp=en-St; Maxx=Max (maxx,temp); St++; intI=1; while(A[st]==a[st+i])///determine whether the next interval is FS or FXi++; if(a[st]-a[st+i]<0) {FS=1; FX=0; } Else{FS=0; FX=1; } } if(en==N) {inttemp=en-St; Maxx=Max (maxx,temp); Break; }} printf ("%d\n", Maxx);}
View Code
#333 Div2 problem B approximating a Constant Range (ruler)