Original title address: Http://codeforces.com/contest/602/problem/B
Test instructions
Given a sequence that satisfies abs (A[i+1]-a[i]) <=1
Required to find the longest sub-sequence [l,r] satisfies the difference between Max and Min in a sequence less than or equal to 1
Exercises
Required to find the longest sequence of test instructions requirements, what are the characteristics of the corresponding sequence?
If the starting point of the sequence is l,r, then there should be abs (A[l-1]-a[r]) ==2
So we enumerate the endpoints, the corresponding starting point can be maintained and queried with a P array O (1)
At the same time, it is required that the a[r]-1 and a[r]+1 in this sequence cannot occur simultaneously, and can be judged by the P array.
The last position p[x]:x appears
The subscript that appeared on the previous x.
Sometimes, because X is too large, it needs to be discretized or it can be implemented with map instead of an array.
#include <bits/stdc++.h>using namespacestd;Const intmaxn=1e5;intp[maxn+Ten];intarr[maxn+5];intMainvoid) {#ifdef EX1 freopen ("In.txt","R", stdin); #endif intN; scanf ("%d",&N); for(intI=1; i<=n;++i) {scanf ("%d",&Arr[i]); } intans=0; for(intI=1; i<=n;++i) {intx=Arr[i]; P[x+2]=i; intK=max (p[x+4],p[x]); if(p[x+3]>k && p[x+1]>k) {ans=max (Ans,i-min (p[x+3],p[x+1])); } Else{ans=max (ans,i-k); }} printf ("%d\n" , ans); }
Approximating a Constant Range