单调队列
zhk手写单调队列定义:
初始时 l=r=0
(l,r] 为有效元素
删除最左元素 ++l
删除最右元素 --r
队列有有效元素:l+1<=r ,即 l<r
取最左点 q[l+1]
取最右点 q[r]
#include<iostream> #include<cstdio> #define ri register int #define u int namespace opt { inline u in() { u x(0),f(1); char s(getchar()); while(s<'0'||s>'9') { if(s=='-') f=-1; s=getchar(); } while(s>='0'&&s<='9') { x=(x<<1)+(x<<3)+s-'0'; s=getchar(); } return x*f; } } using opt::in; #define NN 1000005 #include<algorithm> namespace mainstay { u q[NN],a[NN],l,r,N,ans; inline void solve(){ N=in(); for(ri i(1);i<=N;++i) a[i]=a[i+N]=in(); for(ri i(1);i<=(N<<1);++i){ while(l+1<=r&&q[l+1]<i-(N>>1)) ++l; if(l+1<=r) ans=std::max(ans,a[q[l+1]]-q[l+1]+i+a[i]); while(l+1<=r&&a[q[r]]-q[r]<=a[i]-i) --r; q[++r]=i; } std::cout<<ans; } } int main() { //freopen("x.txt","r",stdin); std::ios::sync_with_stdio(false); mainstay::solve(); }