Approximating a Constant Range

Source: Internet
Author: User

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

Related Article

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.