Contiguous Repainting

时间限制: 2 Sec  内存限制: 256 MB
提交: 69  解决: 22
[提交][状态][讨论版][命题人:admin]

题目描述

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.

来源

AtCoder Grand Contest 008

[题意]

选择K 连续 涂颜色,可以重复涂,每次可以涂白或者黑,问 操作玩完后黑色快的总和最大是多少;

[思路]

可以发现,经过nk次操作后最后一次k范围是不可以控的,K外面,是可以将负数涂成白色,只保留正数黑色;

两次前缀和, 跑连续的k 移动, 取最大, 类似咸鱼翻身那道题

【code】

#include <iostream>
#include <bits/stdc++.h>typedef long long ll;const int MAXN=1e5+5;
typedef long long ll;
const int INF = 0x3f3f3f3f;
using namespace std;ll a[MAXN];
ll sum[MAXN];
ll sum_int[MAXN];
int main()
{int n,k;cin>>n>>k;memset(sum,0,sizeof(sum));memset(sum_int,0,sizeof(sum_int));for(int i=1;i<=n;i++){cin>>a[i];sum[i]=sum[i-1]+a[i];if(a[i]>0) sum_int[i]=sum_int[i-1]+a[i];else sum_int[i]=sum_int[i-1];}ll ans=-INF;for(int i=1;i<=(n-k+1);i++){ll res=0;int j=i+k-1;if( sum[j]-sum[i-1] >0) res+= sum[j]-sum[i-1];res+= sum_int[i-1]-sum[0];res+= sum_int[n]-sum_int[j];ans=max(ans,res);}cout<<ans<<endl;return 0;
}

123

转载于:https://www.cnblogs.com/sizaif/p/9078360.html

AtCoder Grand Contest 008: Contiguous Repainting(思维)相关推荐

  1. 【每日亿题#12】AtCoder Grand Contest 021 (A ~ F)全部题解

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 AtCoder Grand Contest 021 题解 A. Digit Sum 2 B. ...

  2. AtCoder Grand Contest 017

    AtCoder Grand Contest 017 A - Biscuits 有\(n\)个数,问有多少个集合的数的和模\(2\)余\(P\). 随便\(dp\)一下就好了. #include< ...

  3. AtCoder题解——AtCoder Grand Contest 048——A - atcoder < S

    题目相关 题目链接 AtCoder Grand Contest 048 A 题,https://atcoder.jp/contests/agc048/tasks/agc048_a. Problem S ...

  4. AtCoder题解 —— AtCoder Grand Contest 050 —— B - Three Coins —— 动态规划

    题目相关 题目链接 AtCoder Grand Contest 050 B 题,https://atcoder.jp/contests/agc050/tasks/agc050_b. Problem S ...

  5. Atcoder Grand Contest 010 B - Boxes 差分

    B - Boxes 题目连接: http://agc010.contest.atcoder.jp/tasks/agc010_b Description There are N boxes arrang ...

  6. AtCoder Grand Contest 010 D - Decrementing

    题目传送门:https://agc010.contest.atcoder.jp/tasks/agc010_d 题目大意: 有\(n\)个数\(A_i\),它们的\(gcd\)是1,A.B两人轮流操作, ...

  7. Atcoder Grand Contest 012 B - Splatter Painting解题报告

    题目:http://agc012.contest.atcoder.jp/tasks/agc012_b 有一个n点m边的图,(不一定联通) 还有q个操作:每次将一个点v及其周围距离<=d的点涂成颜 ...

  8. [atcoder]AtCoder Grand Contest 027题解

    [题目链接] https://agc027.contest.atcoder.jp/ A [题解] 题意: 是把xxx个糖果分给nnn个人,一个人如果恰好分到aia_{i}ai​个糖果就会高兴.求最多使 ...

  9. UPC个人训练赛第十五场(AtCoder Grand Contest 031)

    传送门: [1]:AtCoder [2]:UPC比赛场 [3]:UPC补题场 参考资料 [1]:https://www.cnblogs.com/QLU-ACM/p/11191644.html B.Re ...

最新文章

  1. python将字符串中的数字相加求和的实现
  2. onnx模型推理(python)
  3. wireshark捕获选项不能用_wireshark的一些基础用法,欢迎收藏
  4. cve-2020-0796_CVE20200796 | Windows SMBv3客户端/永痕之黑漏洞复现
  5. java 内部类传值_Java 封装(内部类)
  6. 对于当前时点不能进行事物的事后确认。
  7. uni-app中v-html中的元素添加样式
  8. .net学科-杨中科-Unity3D视频教程
  9. 二叉树的前中后序遍历
  10. 自动化技术、计算机技术核心期刊整理及介绍
  11. 服务器修改传奇道士神兽升级,1.76复古传奇道士玩家快速升级神兽的技巧
  12. Java|IO流之字符流
  13. 博弈论 Nim游戏与SG函数
  14. Java之图片裁剪工具类-yellowcong
  15. 获取全国行政区域划分数据
  16. 响应式Web设计(四):响应式Web设计的优化
  17. ino查看工具android版,Tian Wang INO
  18. pmon下修改分辨率示范
  19. shell脚本-----99乘法表过程解析
  20. AI绘画火爆,到现在还只是冰山一角?AIGC掀起当代新艺术浪潮

热门文章

  1. 入选 Forrester 领导者象限,阿里云 Serverless 产品能力全球第一
  2. JAVA应用开发MQ实战最佳实践——Series2:消息队列RocketMQ性能测试案例
  3. Springboot中艾特Controller和艾特RestController之间的区别
  4. 腾讯天美六位TA:技术美术这份职业会长期存在吗?
  5. PS5独占游戏RETURNAL:华丽的黑暗电子声音是如何制作的(新 3D 音频引擎)下
  6. 《我的侠客》主策划何铮:如何打出武侠游戏的新招
  7. 使用SQLPLUS生成HTML报表
  8. 北风设计模式课程---享元模式
  9. LeetCode 404. 左叶子之和(Sum of Left Leaves)
  10. 寒假集训【1.26】