Contiguous Repainting
题目描述
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
输入
N K
a1 a2 … aN
输出
样例输入
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+;
ll s[maxn],a[maxn];
int main(){
int n,k,t;
cin>>n>>k;
for(int i=;i<=n;i++){
cin>>t;
s[i]=s[i-]+t;
a[i]=a[i-];
if(t>) a[i]+=t;
}
ll ans=;
for(int i=;i+k-<=n;i++){
ll sum=a[n]-(a[i+k-]-a[i-]);
if(s[i+k-]-s[i-]>) sum+=s[i+k-]-s[i-];
ans=max(ans,sum);
}
cout<<ans<<endl;
return ;
}
Contiguous Repainting的更多相关文章
- AtCoder Grand Contest 008
AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...
- A@[G!C]%008
A@[G!C]%008 A Simple Calculator 细节题. B Contiguous Repainting 最后只要有连续\(K\)个鸽子同色就可以构造方案,枚举+前缀和 C Tetro ...
- 【AtCoder】AGC008
AGC008 A - Simple Calculator 如果符号相同,那么如果y比x大直接走,否则需要两次反号 如果符号不同,需要绝对值的差加一次反号 如果有一个是0,且y比x要小,那只需要一次反号 ...
- [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大
17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence w ...
- 论文阅读之 DECOLOR: Moving Object Detection by Detecting Contiguous Outliers in the Low-Rank Representation
DECOLOR: Moving Object Detection by Detecting Contiguous Outliers in the Low-Rank Representation Xia ...
- Find longest contiguous sub array
It's still an Amazon interview question. Given an array containing only stars '*' and hashes '#' . F ...
- [LeetCode] Contiguous Array 邻近数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- [Swift]LeetCode525. 连续数组 | Contiguous Array
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- 994.Contiguous Array 邻近数组
描述 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and ...
随机推荐
- C++优先队列详解
转自csdn的文章,仅作为学习笔记.原文链接:https://blog.csdn.net/weixin_36888577/article/details/79937886 普通的队列是一种先进先出的数 ...
- mysql 杂
mysql> show create table table_name|view_name \G; 可以查看是视图还是表 \G 放到sql语句后,可以使每个字段打印到单独的行 1 I ...
- 【2017西安邀请赛:A】XOR(线段树+线性基)
前言:虽然已经有很多题解了,但是还是想按自己的理解写一篇. 思路:首先分析题目 一.区间操作 —— 线段树 二.异或操作 —— 线性基 这个两个不难想,关键是下一步的技巧 “或”运算 就是两个数的二进 ...
- 程序员用 Python 扒出 B 站那些“惊为天人”的UP主!
前言 ! 近期B站的跨年晚会因其独特的创意席卷各大视频网站,给公司带来了极大的正面影响,股价也同时大涨,想必大家都在后悔没有早点买B站的股票: 然而今天我们要讨论的不是B站的跨年晚会,而是B站 ...
- Vue-router(1)之component标签
1. 使用 <component>标签实现组件切换 <component> 是Vue提供的标签语法:有一个is属性,is的作用就是显示指定的组件 <template> ...
- 改变UILable里面文字的大小和颜色
UILabel *lb = [[UILabel alloc]init]; NSMutableAttributedString *attriStr = [[NSMutableAttributedStri ...
- Mybatis实现if trim(四)
1. 准备 请先完成Mybatis实现增删改查(二)和Mybatis实现条件查询(三)的基本内容 2. 关于多条件查询的疑问 在Mybatis实现条件查询(三)中我们实现了多条件(商品编码.商品名称. ...
- STM32F407读编码器没上拉电阻遇见的问题
在调试之前由于本科阶段参加飞思卡尔智能汽车的竞赛,一直在使用与竞赛相关的单片机和编码器,后来由于工程的需要开始使用STM32的板子,在调试编码器的时候遇见了,使用了STM32的官方标准库中的定时器正交 ...
- python numpy和矩阵
2.numpy数据选取 lst=[[1, 2, 3], [4, 5, 6]] np.array(lst)[:-1] Out[32]: array([[1, 2, 3]]) np.array(lst)[ ...
- Java数据的存储
在JAVA中,有六个不同的地方可以存储数据: 1. 寄存器(register).这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据需求进 ...