hdu 3530
单调队列
两个
一个维护单增序列
另一个维护单降序列
#include <bits/stdc++.h> const int N = 1e5 + 10; int n, m, k, Ans; struct Node { int id, val; } Q_max[N], Q_min[N]; int head_max, head_min, from, tail_max, tail_min; int main() { while(~ scanf("%d %d %d", &n, &m, &k)) { head_max = head_min = from = 1, tail_max = tail_min = Ans = 0; Node tmp; for(int i = 1; i <= n; i ++) { scanf("%d", &tmp.val); tmp.id = i; while(head_max <= tail_max && tmp.val >= Q_max[tail_max].val) tail_max --; Q_max[++ tail_max] = tmp; while(head_min <= tail_min && tmp.val <= Q_min[tail_min].val) tail_min --; Q_min[++ tail_min] = tmp; while(Q_max[head_max].val - Q_min[head_min].val > k) { if(Q_max[head_max].id < Q_min[head_min].id) { from = Q_max[head_max].id + 1; head_max ++; } else { from = Q_min[head_min].id + 1; head_min ++; } } if(Q_max[head_max].val - Q_min[head_min].val <= k && Q_max[head_max].val - Q_min[head_min].val >= m) { Ans = std:: max(Ans, i - from + 1); } } printf("%d\n", Ans); }