luogu P1725 琪露诺 单调队列dp
#include<map> #include<queue> #include<time.h> #include<limits.h> #include<cmath> #include<ostream> #include<iterator> #include<set> #include<stack> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define rep_1(i,m,n) for(int i=m;i<=n;i++) #define mem(st) memset(st,0,sizeof st) int read() { int res=0,ch,flag=0; if((ch=getchar())=='-') //判断正负 flag=1; else if(ch>='0'&&ch<='9') //得到完整的数 res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+ch-'0'; return flag?-res:res; } typedef long long ll; typedef pair<int,int> pii; typedef unsigned long long ull; typedef pair<double,double> pdd; const int inf = 0x3f3f3f3f; const int N=2e6+10; int a[N]; int f[N]; int q[N]; void solve() { int n,l,r; cin>>n>>l>>r; for(int i=0; i<=n; i++) cin>>a[i]; int hh=1,tt=1; memset(f,0xcf,sizeof(f));f[0]=0; int ans=-0x3f3f3f3f; for(int i=l; i<=n; i++) { while(hh<tt&&q[hh]+r<i) hh++; while(hh<tt&&f[q[tt-1]]<=f[i-l]) tt--; q[tt++]=i-l; f[i]=f[q[hh]]+a[i]; if(i+r>n) ans=max(ans,f[i]); } cout<<ans<<endl; } int main() { int t=1; while(t--) solve(); return 0; }