洛谷p1725 露琪诺 单调队列优化的DP
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int n,L,R,a[400007]; const int INF=2e8+7; int dfs(int pos){ if(pos>n) return 0; int i,k=-INF; for(i=pos+L;i<=pos+R;++i){ k=max(k,dfs(i)); } return k+a[pos]; } int dp[400007],q[400007],head,tail,l,r; void solve(){ int i,j;head=0,tail=0; memset(dp,0,sizeof(dp)); l=n+R+1,r=n+R+1; //固定r移动l for(i=n;i>=0;--i){ // dp[i]=-INF; // for(j=i+L;j<=i+R;++j){ // dp[i]=max(dp[i],dp[j]); // } // dp[i]+=a[i]; r--; while(head!=tail&&q[head]>i+R) head++; while(r-l+1<R-L+1){ l--; if(head==tail){ q[tail++]=l; } else{ while(head!=tail&&dp[q[tail-1]]<=dp[l]) tail--; q[tail++]=l; } } dp[i]=dp[q[head]]+a[i];//少加了dp } } int main(){ while(~scanf("%d%d%d",&n,&L,&R)){ memset(a,0,sizeof(a)); int i; for(i=0;i<=n;++i) scanf("%d",a+i); solve(); printf("%d\n",dp[0]); } return 0; }