Description
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 l ≤ i ≤ r; 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.
Sample Input
Input
5
1 2 3) 3 2
Output
4
Input
11
5 4 5 5 6 7 8 8 8 7 6
Output
5
Hint
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].
Ideas:
At first various want LIS, but CF again proves that the first two topics are all ideas (although the idea is not simpler than the algorithm = =~)
The first is to find one of the sub-segments in a whole number of paragraphs, and in the case of its m-m<=1, make it len Max
So here are two key points to think about:
(1) Transitions between different states: set a M and m for each state, and determine if the previous state can continue to be maintained for each of the points in the count++
(2) realize that the starting point of the new State is not necessarily the end point of the old state: there may be a point where the original state does not last, but it does not mean that this is the starting point of the new state, and that the beginning of the new state may appear in the process of maintaining the old state. This is the main point of the future as long as according to different circumstances to find the beginning is OK.
#include <iostream>using namespacestd;intM,m,n;intnum[100007];BOOLOkintt) { if(m==M) {if(t==m)return true; Else if(t = = m1|| t = = m+1) return true; Else return false; } Else { if(t==m| | t==m)return true; Else return false; }}intMain () { while(cin>>N) {cin>>num[1]; M= num[1]; M= num[1]; intCount =1; intAns =0; for(inti =2; I <= n;i++) {cin>>Num[i]; if(OK (Num[i])) {if(m==m && num[i] = = m1) M=Num[i]; Else if(m==m && num[i] = = m+1) M=Num[i]; Count++; } Else { if(M = =m) {m= m =Num[i]; Count=1; } Else { intPOS; if(Num[i] = = num[i-1]+1) {M=Num[i]; M= num[i-1]; for(intj = I1; J >=1; j--) { if(Num[j]! = num[i-1]) Break; POS=J; } Count= i-pos+1;//The count at this point should be = coordinates starting at the current coordinate-s segment } Else if(Num[i] = = num[i-1]-1) {M= num[i-1]; M=Num[i]; for(intj = I1; J >=1; j--) { if(Num[j]! = num[i-1]) Break; POS=J; } Count= i-pos+1;//Ibid . } Else{M= m =Num[i]; Count=1; }}} ans=Max (Ans,count); } cout<<ans<<Endl; } return 0;}
Cf-approximating a Constant Range