• Contiguous Repainting


    题目描述

    There are N squares aligned in a row. The i-th square from the left contains an integer ai.

    Initially, all the squares are white. Snuke will perform the following operation some number of times:

    Select K consecutive squares. Then, paint all of them white, or paint all of them black. Here, the colors of the squares are overwritten.
    After Snuke finishes performing the operation, the score will be calculated as the sum of the integers contained in the black squares. Find the maximum possible score.

    Constraints
    1≤N≤105
    1≤K≤N
    ai is an integer.
    |ai|≤109

    输入

    The input is given from Standard Input in the following format:

    N K
    a1 a2 … aN

    输出

    Print the maximum possible score.

    样例输入

    5 3
    -10 10 -10 10 -10
    

    样例输出

    10
    

    提示

    Paint the following squares black: the second, third and fourth squares from the left.Contiguous Repainting

    除了最后一次刷,再这一k段序列的其他正数都可以取到

    把这k段序列当作刷白的中转站,反正就算黑了几个再把这k个刷白。

    最后再考虑这k个要不要刷黑

    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    const int maxn=1e5+5;
    ll s[maxn],a[maxn];
    int main(){
        int n,k,t;
        cin>>n>>k;
        for(int i=1;i<=n;i++){
            cin>>t;
            s[i]=s[i-1]+t;
            a[i]=a[i-1];
            if(t>0) a[i]+=t;
        }
        ll ans=0;
        for(int i=1;i+k-1<=n;i++){
            ll sum=a[n]-(a[i+k-1]-a[i-1]);
            if(s[i+k-1]-s[i-1]>0) sum+=s[i+k-1]-s[i-1];
            ans=max(ans,sum);
        }
        cout<<ans<<endl;
        return 0;
    }
    View Code
    不要忘记努力,不要辜负自己 欢迎指正 QQ:1468580561
  • 相关阅读:
    JQuery的ajax函数执行失败,alert函数弹框一闪而过
    ThinkPHP5.1中数据查询使用field方法数组参数起别名时遇到的问题
    Linux Web服务器集群搭建
    三层架构之UI层
    Aircoinst 三层架构ASP.NET开源
    [教程]KALI LINUX 2.0 2019 更新国内源
    工厂模式
    Java 实现序列化和反序列化
    Java实现缓冲流 编码 & 解码
    Java使用IO流读取文件显示到控制台2
  • 原文地址:https://www.cnblogs.com/smallocean/p/8886765.html
Copyright © 2020-2023  润新知