B. Approximating a Constant Range
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 a1, ...,an. There aren ' t any big jumps between consecutive data points -for each 1 ≤i<n, I T ' s guaranteed that | Ai+ 1-ai| ≤ 1.
A Range[L,R]Of data points is saidAlmost constantIf the difference between the largest and the smallest value in that range are at most1. Formally, letM be the maximum And m < Span lang= "ZH-CN" >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≤ ) -th E number of data points.
The second line contains n integers a1,a2, ...,an ( C16>1 ≤ai≤ ).
Output
Print a single number-the maximum length of a almost constant range of the given sequence.
Sample Test (s)
Input
5
1 2 3) 3 2
Output
4
Input
11
5 4 5 5 6 7 8 8 8 7 6
Output
5
From <Http://codeforces.com/contest/602/problem/B>
Codeforces Round #333 (Div. 2)
"Test Instructions":
N number , the difference of adjacent number does not exceed 1.
The longest interval, so that the extreme difference does not exceed 1.
"Problem-solving ideas":
For x, the legal interval containing x needs to take into account the location of the X-1 x+1 x+2 X-2:
Use array p[i] to record the maximum position of the present I;
If themaximum position of X-1 is greater than x+1, the position of x+1 and X-2 should be considered;
Instead, just consider The location of x 1 and x+2 .
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 #defineINF 0x3f3f3f3f7 #defineLL Long Long8 #defineMAXN 1100009 #defineIn Freopen ("In.txt", "R", stdin);Ten using namespacestd; One A intN; - intP[MAXN]; - the intMainintargcChar Const*argv[]) - { - //in ; - + while(SCANF ("%d", &n)! =EOF) - { + intAns =-1; AMemset (P,0,sizeof(P)); at - for(intI=1; i<=n;i++){ - intX;SCANF ("%d",&x); - - if(p[x-1]>p[x+1]) ans = max (ans, I-max (p[x+1],p[x-2])); - Elseans = max (ans, I-max (p[x+2],p[x-1])); in -P[X] =i; to } + -printf"%d\n", ans); the } * $ return 0;Panax Notoginseng}
Codeforces 602B approximating a Constant Range (idea title)