dp/Monotone Queue optimization
Puzzle: http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html
Reference:
First, we need to make a few things clear
1. Assuming that we now know that the sequence (I,J) is compliant, then (I,J+1) is also legal if the j+1 element is not greater than the maximum value (I,J) and is not smaller than the minimum value.
2. If the (I,J) is not legal because the difference is smaller than the requirement, then the change in the (I,J) range is invalid, and the j+1 element needs to be added as the maximum or minimum value to obtain a valid sequence
3. Assuming that the difference between the sequence (I,J) is greater than the requirement, then we must remove the maximum or minimum value from the sequence to obtain a valid sequence, and it is not possible for the sequence to be valid only by adding elements to it.
1 //hdoj 35302#include <cmath>3#include <queue>4#include <vector>5#include <cstdio>6#include <cstring>7#include <cstdlib>8#include <iostream>9#include <algorithm>Ten #defineRep (i,n) for (int i=0;i<n;++i) One #defineF (i,j,n) for (int i=j;i<=n;++i) A #defineD (i,j,n) for (int i=j;i>=n;--i) - #definePB Push_back - #defineCC (A, B) memset (A,b,sizeof (a)) the using namespacestd; - intGetint () { - intv=0, sign=1;CharCh=GetChar (); - while(!isdigit (CH)) {if(ch=='-') sign=-1; Ch=GetChar ();} + while(IsDigit (CH)) {v=v*Ten+ch-'0'; Ch=GetChar ();} - returnv*Sign ; + } A Const intn=1e6+Ten, inf=~0u>>2; at Const Doubleeps=1e-8; - /*******************template********************/ - -deque<int>mx,mn; - intA[n]; - intMain () { in #ifndef Online_judge -Freopen ("Input.txt","R", stdin); to //freopen ("Output.txt", "w", stdout); + #endif - intn,m,k; the while(SCANF ("%d%d%d", &n,&m,&k)! =EOF) { * intstart=0, ans=0; $F (I,1, N) a[i]=getint ();Panax Notoginseng mx.clear (); Mn.clear (); -F (I,1, N) { the while(!mx.empty () && a[mx.back ()]<=A[i]) mx.pop_back (); + while(!mn.empty () && a[mn.back ()]>=A[i]) mn.pop_back (); A MX.PB (i); MN.PB (i); the while(A[mx.front ()]-a[mn.front ()]>k) { + if(Mx.front () <Mn.front ()) { -start=Mx.front (); $ Mx.pop_front (); $}Else{ -start=Mn.front (); - Mn.pop_front (); the } - }Wuyi if(A[mx.front ()]-a[mn.front ()]>=m) theAns=max (I-Start,ans); - } Wuprintf"%d\n", ans); - } About return 0; $}View Code
"Hdoj" "3530" subsequence