E46 单调队列优化DP 琪露诺

视频链接:454 单调队列优化DP 琪露诺_哔哩哔哩_bilibili

Luogu P1725 琪露诺

复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N=2e5+10;
int n,L,R;
int a[N],f[N],q[N];

int main(){
  scanf("%d%d%d",&n,&L,&R);
  for(int i=0;i<=n;i++) scanf("%d",&a[i]);
  memset(f,-0x3f,sizeof f); f[0]=0;
  
  int h=1, t=0; int ans=-2e9;
  for(int i=L;i<=n;i++){
    while(h<=t && f[q[t]]<=f[i-L]) t--;
    q[++t]=i-L;
    if(q[h]<i-R) h++;
    f[i]=f[q[h]]+a[i];
    if(i>n-R) ans=max(ans,f[i]);
  }
  printf("%d\n",ans);
}
复制代码
复制代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N=2e5+10;
int n,L,R;
int a[N],f[N],q[N];

int main(){
  scanf("%d%d%d",&n,&L,&R);
  for(int i=0;i<=n;i++) scanf("%d",&a[i]);
  memset(f,-0x3f,sizeof f); f[0]=0;
  
  int h=1, t=0; int ans=-2e9;
  for(int i=L;i<=n;i++){
    while(h<=t && q[h]<i-R) h++;
    while(h<=t && f[q[t]]<=f[i-L]) t--;
    q[++t]=i-L;
    f[i]=f[q[h]]+a[i];
    if(i>n-R) ans=max(ans,f[i]);
  }
  printf("%d\n",ans);
}
复制代码

 

posted @   董晓  阅读(302)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示