BZOJ 2096: [Poi2010]Pilots

时间:2023-12-29 14:41:56

Description

求一个最长的序列,最大值最小值之差不超过 \(k\) .

Sol

单调队列.

一个队列直接上就行..

Code

/**************************************************************
Problem: 2096
User: BeiYu
Language: C++
Result: Accepted
Time:4024 ms
Memory:71604 kb
****************************************************************/ #include <bits/stdc++.h>
using namespace std; #define debug(a) cout<<#a<<"="<<a<<" " typedef long long LL;
const int N = 3e6+50; LL n,k,ans;
LL a[N];
LL q[2][N],h[2],t[2],l; inline LL in(LL x=0,char ch=getchar()) { while(ch>'9' || ch<'0') ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();return x; }
int main() {
k=in(),n=in();
for(int i=1;i<=n;i++) a[i]=in(); ans=1;
q[0][h[0]=t[0]=1]=1,q[1][h[1]=t[1]=1]=1,l=1; for(int i=2;i<=n;i++) {
while(h[0]<=t[0] && a[i]<a[q[0][t[0]]]) t[0]--;
q[0][++t[0]]=i; // cout<<i<<":"<<endl;
// for(int j=h[0];j<=t[0];j++) cout<<q[0][j]<<endl; while(h[1]<=t[1] && a[i]>a[q[1][t[1]]]) t[1]--;
q[1][++t[1]]=i; // for(int j=h[1];j<=t[1];j++) cout<<q[1][j]<<endl; for(;a[q[1][h[1]]]-a[q[0][h[0]]]>k;l++) {
if(q[0][h[0]]==l) h[0]++;
if(q[1][h[1]]==l) h[1]++;
}
ans=max(ans,i-l+1);
// debug(l),debug(ans)<<endl;
// cout<<"--------------------"<<endl;
}cout<<ans<<endl;
return 0;
}