天天看點

Codeforces Round #333 (Div. 2) B. Approximating a Constant Range (dp)

B. Approximating a Constant Range

time limit per test 2 seconds

memory limit per test 256 megabytes

input standard input

output standard output

When Xellos was doing a practice course in university, he once had to measure the intensity of an effect that slowly approached equilibrium. A good way to 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 we're at it?

n data points a1, ..., an. There aren't any big jumps between consecutive data points — for each 1 ≤ i < n, it's guaranteed that |ai + 1 - ai| ≤ 1.

[l, r] of data points is said to be almost constant if the difference between the largest and the smallest value in that range is at most 1. Formally, let M be 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

n (2 ≤ n ≤ 100 000) — the number of data points.

n integers a1, a2, ..., an (1 ≤ ai).

Output

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

Examples

input

5
1 2 3 3 2      

output

4      

input

11
5 4 5 5 6 7 8 8 8 7 6      

output

5      

Note

[2, 5]; its length (the number of data points in it) is 4.

4: [1, 4], [6, 9] and [7, 10]; the only almost constant range of the maximum length 5 is [6, 10].

題解:基礎dp,尺取法也可以。

代碼:

#pragma comment(linker, "/STACK:102400000,102400000")
//#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cmath>
#include<queue>
#include<set>
#include<stack>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define mst(a) memset(a, 0, sizeof(a))
#define M_P(x,y) make_pair(x,y)  
#define rep(i,j,k) for (int i = j; i <= k; i++)  
#define per(i,j,k) for (int i = j; i >= k; i--)  
#define lson x << 1, l, mid  
#define rson x << 1 | 1, mid + 1, r  
const int lowbit(int x) { return x&-x; }  
const double eps = 1e-8;  
const int INF = 1e9+7; 
const ll inf =(1LL<<62) ;
const int MOD = 1e9 + 7;  
const ll mod = (1LL<<32);
const int N = 3e5+7; 
const int M=100010;
const int maxn=2e3+7; 
template <class T1, class T2>inline void getmax(T1 &a, T2 b) {if (b>a)a = b;}  
template <class T1, class T2>inline void getmin(T1 &a, T2 b) {if (b<a)a = b;}
int read(){
int v = 0, f = 1;char c =getchar();
while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}
while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();
return v*f;}
int dp[1000010]; 
int main()
{
  int ans=1,n,a,l=0;
  cin>>n;
  for(int i=1;i<=n;++i)
  {
    scanf("%d",&a);
    ++a;
    l=max(l,dp[a-2]);
    l=max(l,dp[a+2]);
    dp[a]=i;
    ans=max(ans,i-l);
  }
  cout<<ans;
  return 0;
}