#333 Div2 problem B approximating a Constant Range (ruler)

Source: Internet
Author: User

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)

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.