bzoj2096 [Poi2010]Pilots
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2096
【题解】
单调队列。维护上升&下降
# include <stdio.h> # include <string.h> # include <iostream> # include <algorithm> // # include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; const int M = 3e6 + 10; const int mod = 1e9+7; # define RG register # define ST static int K, n; int a[M]; int q1[M], q2[M]; int h1, t1, h2, t2, ans; // inc, dec int main() { h1 = h2 = 1, t1 = t2 = 0; int t = 1; cin >> K >> n; for (int i=1; i<=n; ++i) scanf("%d", a+i); for (int i=1; i<=n; ++i) { while(h1 <= t1 && a[q1[t1]] <= a[i]) --t1; while(h2 <= t2 && a[q2[t2]] >= a[i]) --t2; q1[++t1] = i; q2[++t2] = i; while(a[q1[h1]] - a[q2[h2]] > K) if(q1[h1] < q2[h2]) t = q1[h1]+1, ++h1; else t = q2[h2]+1, ++h2; ans = max(ans, i-t+1); } printf("%d\n", ans); return 0; }