[POI2010]PIL-Pilots
【代码】
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 3001000;
ll q_mn[N],q_mx[N],head_mx = 1,head_mn = 1, tail_mx = 0, tail_mn = 0, a[N];
ll ans = 0;
int main() {
ll n, t, pre = 1;
scanf("%lld%lld", &t, &n);
for(int i = 1; i <= n; ++i) {
scanf("%lld",&a[i]);
while(head_mx <= tail_mx && a[i] > a[q_mx[tail_mx]])--tail_mx;
while(head_mn <= tail_mn && a[i] < a[q_mn[tail_mn]])--tail_mn;
q_mx[++tail_mx] = i;
q_mn[++tail_mn] = i;
while(a[q_mx[head_mx]] - a[q_mn[head_mn]] > t) {
if(q_mx[head_mx] > q_mn[head_mn]) {
pre = q_mn[head_mn] + 1;
++head_mn;
}else {
pre = q_mx[head_mx] + 1;
++head_mx;
}
}
ans = max(ans, i - pre + 1);
}
printf("%lld\n", ans);
return 0;
}