实在不明白难度等级,难不成前缀和是个很变态的东西?

说白了就是单调队列裸题,都没加什么别的东西,就是一个前缀和的计算,然而这个题也不是要用它优化,而是必须这么做啊。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define re register
#define wc 0.0000000001
using namespace std;
inline int read()
{
    int x=0,c=1;
    char ch=' ';
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    while(ch=='-')c*=-1,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*c;
}
int s[1000001],t,q[1000001],n,m,ans;
int main()
{
    n=read();
    m=read();
    for(re int i=1;i<=n;i++)
    {
        t=read();
        s[i]=s[i-1]+t;
    }
    int head=1,tail=0;
    for(re int i=1;i<=n;i++)
    {
        while(head<=tail&&s[q[tail]]>=s[i])
        tail--;
        q[++tail]=i;
        while(q[head]<i-m)
        head++;
        ans=max(ans,s[i]-s[q[head]]);
    }
    cout<<ans;
}

 

相关文章:

  • 2022-12-23
  • 2022-01-20
  • 2021-07-19
  • 2021-08-02
  • 2022-12-23
  • 2021-09-15
  • 2021-09-08
  • 2022-12-23
猜你喜欢
  • 2021-11-27
  • 2022-01-24
  • 2021-08-04
  • 2022-12-23
  • 2022-12-23
  • 2021-09-11
相关资源
相似解决方案