Test instructions is said to give a sequence that now requires the oldest of the sequence of the series, requiring the difference between the maximum and minimum of the sub-range in the range of [M, K], for interval length
The practice is to maintain two queues, one to maintain the maximum value at the current location, one to maintain the minimum value, and then to calculate the current node I as the most frequent interval length of the right endpoint, then scan two queues to maintain monotonicity.
Then compare the difference of two queue headers,
1. If the difference satisfies the condition, then record the answer;
2. If the difference is less than m, then there is no answer at this time, stating that there is no interval with I as the right endpoint satisfies the condition (indicates the maximum value of the first I number minus the minimum value of the number of previous I <m, then no matter how to adjust the starting point, there is no solution)
3. If the difference is greater than k, indicating that the difference between the maximum and the minimum value of the interval is too large, we can reduce the difference by narrowing the maximum (the maximum (descending) queue to the right) or by increasing the minimum value (the minimum (increment) queue to the right). Which pointer to move depends on the number of these two values at the beginning of the team, whose numbers are small (the guarantee interval is legal).
Another point to note is that in the third case above, when moving the first pointer of the team, if the last element to be removed points to the subscript p, at this point the element of the team head points to the subscript is Q, at this time the legal interval for [P +1, I] instead of [Q, I]
1 //#pragma COMMENT (linker, "/stack:1677721600")2#include <map>3#include <Set>4#include <stack>5#include <queue>6#include <cmath>7#include <ctime>8#include <vector>9#include <cstdio>Ten#include <cctype> One#include <cstring> A#include <cstdlib> -#include <iostream> -#include <algorithm> the using namespacestd; - #defineINF 0x3f3f3f3f - #defineInf (-((LL) 1<<40)) - #defineLson k<<1, L, (L + R) >>1 + #defineRson k<<1|1, ((L + R) >>1) + 1, R - #defineMem0 (a) memset (A,0,sizeof (a)) + #defineMem1 (a) memset (A,-1,sizeof (a)) A #defineMem (A, B) memset (A, B, sizeof (a)) at #defineFIN freopen ("In.txt", "R", stdin) - #defineFOUT freopen ("OUT.txt", "w", stdout) - #defineRep (I, A, b) for (int i = A; I <= B; i + +) - #defineDec (i, A, b) for (int i = A; I >= b; i-) - -template<classT> T MAX (t A, T b) {returna > B?a:b;} intemplate<classT> T MIN (t A, T b) {returnA < b?a:b;} -template<classT> T GCD (t A, T b) {returnB? GCD (b, a%b): A; } totemplate<classT> T LCM (t A, T b) {returnA/GCD (A, b) *b; } + - //typedef __int64 LL; thetypedefLong LongLL; * Const intMAXN =100000+ -; $ Const intMAXM =110000;Panax Notoginseng Const DoubleEPS = 1e-8; -LL MOD =1000000007; the + intA[MAXN], L, H, N; A intQ1[MAXN], Q2[MAXN]; the + intFind_ans () { - intF1, F2, T1, t2, L1 =-1, L2 =-1, ans =0; $F1 = F2 = T1 = t2 =0; $Rep (I,0N1) { - while(F1 < T1 && A[q1[t1-1]] <= a[i]) T1--;//Maintain maximum queue (decrement) -q1[t1++] =i; the while(F2 < t2 && A[q2[t2-1]] >= a[i]) T2--;//Maintain minimum queue (increment) -q2[t2++] =i;Wuyi while(A[q1[f1]]-A[Q2[F2]] > H) {//The difference is too large theQ1[F1] < Q2[F2]? L1 = q1[f1 +]: L2 = q2[f2 + +]; - } Wu if(A[q1[f1]]-A[Q2[F2]] >= L) {//The difference satisfies the condition -ans = max (ans, I-Max (L1, L2)); About } $ } - returnans; - } - A intMain () + { the while(~SCANF (" %d%d%d", &n, &l, &H)) { -Rep (I,0N1) scanf ("%d", A +i); $printf"%d\n", Find_ans ()); the } the return 0; the}
HDU3530 subsequence (monotone queue)