随笔 - 530  文章 - 0  评论 - 3  阅读 - 10098 

长度为n 的整数序列  要求从中找出一段连续的长度不超过m的子序列,使得这个序列的和最大

 

复制代码
#include <iostream>
using namespace std;
 const int N=2e5+3;
 
 #define int long long
 int a[N],s[N],f[N],n,m;
 int ans; 
 int hh,tt,q[N];
 
 void solve(){
     hh=0,tt=0; 
     ans=-1e18;
     if(n<=m) ans=max(ans,s[n]);
     for(int i=1;i<=n;i++){
         while(hh<=tt&&q[hh]<i-m) hh++;
             ans=max(ans,s[i]-s[q[hh]]);
             
         while(hh<=tt&&s[i]<s[q[tt]]) tt--;
         q[++tt]=i;
     }
    cout<<ans<<endl;
 }
 main(){
     cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i],s[i]=s[i-1]+a[i]; 
     solve();
 }
复制代码

 

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