cf602b approximating a Constant Range

Source: Internet
Author: User

B. Approximating a Constant rangetime limit per test2 secondsMemory limit per testMegabytesInputStandard InputOutputStandard Output

When Xellos is doing a practice course in university, he once had to measure the intensity of a effect that slowly appro ached equilibrium. A good the determine the equilibrium intensity would be choosing a sufficiently large number of consecutive data points That seems as constant as possible and taking their average. Of course, with the usual sizes of data, it's nothing challenging-but why not make a similar programming contest problem While the we ' re at it?

You ' re given a sequence Of  n  data points  a 1, ..., a n .  There aren ' t any big jumps between consecutive data points -for Each 1≤ I < n , it ' s guaranteed that | A i + 1- a i |≤1.

A range [l, R] of data points is said to being almost constant if the difference between the larges T and the smallest value in that range are at most 1. Formally, let m is the maximum and m the minimum value of ai for lir; the range [l, R] is almost constant if m - M ≤1.

Find the length of the longest almost constant range.

Input

The first line of the input contains a single integer n (2≤ n ≤100)-the number of data p Oints.

The second line contains n integers a1, a2, ..., an (1≤ ai ≤100).

Output

Print a single number-the maximum length of a almost constant range of the given sequence.

Examplesinput
5
1 2 3) 3 2
Output
4
Input
11
5 4 5 5 6 7 8 8 8 7 6
Output
5
Note

In the first sample, the longest almost constant range are [2, 5]; its length (the number of data points in it) is 4.

In the second sample, there is three almost constant ranges of length 4: [1, 4], [6, 9] and [7, ten]; the Only almost constant range of the maximum length 5 is [6].

I'm a little surprised. B is the DP, although it is a simple DP

Test instructions is said to give a sequence, to ensure that the adjacent two difference between the value of not more than 1, to find a eldest son string length, required to meet the maximum value of the substring minus the minimum value less than 2

It means that there are only two numbers adjacent to each other in the string.

F[i][1] represents the oldest string that starts with the number of I and contains only a[i] and a[i]+1 two numbers

F[I][2] represents the oldest string that starts with the number of I and contains only a[i] and a[i]-1 two numbers

And then

A[I]==A[I+1] f[i][1]=f[i+1][1] f[i][2]=f[i+1][2]

A[i]==a[i+1]+1 is F[i][1]=1 f[i][2]=f[i+1][1]+1

A[i]==a[i+1]-1 is F[i][2]=1 f[i][1]=f[i+1][2]+1

1#include <cstdio>2#include <iostream>3#include <cstring>4#include <cstdlib>5#include <algorithm>6#include <cmath>7#include <queue>8#include <deque>9#include <Set>Ten#include <map> One#include <ctime> A #defineLL Long Long - #defineINF 0X7FFFFFF - #definePA pair<int,int> the #definePi 3.1415926535897932384626433832795028841971 - using namespacestd; - inline LL read () - { +LL x=0, f=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} +      while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} A     returnx*F; at } -Inlinevoidwrite (LL a) - { -     if(a<0) {printf ("-"); a=-A;} -     if(a>=Ten) Write (ATen); -Putchar (a%Ten+'0'); in } -InlinevoidWriteln (LL a) {write (a);p rintf ("\ n");} to intN,ans; + inta[100010]; - ints1[100010]; the ints2[100010]; * intMain () $ {Panax Notoginsengn=read (); -      for(intI=1; i<=n;i++) a[i]=read (); thes1[n]=s2[n]=ans=1; +      for(inti=n-1; i>=1; i--) A     { the         if(a[i]==a[i+1]) s1[i]=s1[i+1]+1, s2[i]=s2[i+1]+1; +         if(a[i]>a[i+1]) s1[i]=1, s2[i]=s1[i+1]+1; -         if(a[i]<a[i+1]) s2[i]=1, s1[i]=s2[i+1]+1; $ans=Max (Ans,max (s1[i],s2[i)); $     } -printf"%d\n", ans); -}
cf602b

cf602b approximating a Constant Range

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.