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 ...
随机推荐
- Python 时间 time
import time print time.strftime("%Y-%m-%d") import datetime print datetime.datetime.now() ...
- php优惠券生成-去重
记录一次优惠券生成-去重 方法一 /** * 生成批量礼品消费券 */ public function giftCardAddOp() { //接收get值 $num = $_GET['gift_nu ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 复制表
如果需要完全的复制MySQL的数据表,包括表的结构,索引,默认值等. 如果仅仅使用CREATE TABLE ... SELECT 命令,是无法实现的. 如何完整的复制MySQL数据表,步骤如下: 使用 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL NULL 值处理
MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数据表中的数据,但是当提供的查询条件字段为 NULL 时,该命令可能就无法正常工作. 为了处理这种情况,MySQL提供了三大运算符 ...
- PAT Advanced 1102 Invert a Binary Tree (25) [树的遍历]
题目 The following is from Max Howell @twitter: Google: 90% of our engineers use the sofware you wrote ...
- ElasticSearch的9200和9300端口的区别
9200用于外部通讯,基于http协议,程序与es的通信使用9200端口. 9300jar之间就是通过tcp协议通信,遵循tcp协议,es集群中的节点之间也通过9300端口进行通信.
- XML技术详解
XML 1.XML概述 XML可扩展标记语言是一种基于文本的语言用作应用程序之间的通信模式,是一个非常有用的描述结构化信息的技术.XML工具使得转化和处理数据变得十分容易,但同样也要领域相关的标准和代 ...
- Tomcat server.xml常用配置 含有外带文件及默认host
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE server-xml [<!ENTITY ...
- php速成_day4
一.微信公众平台概述 1.微信发展史 1)2011年1月21日,腾讯推出微信应用程序.(张小龙) 2)2012年8月20日,腾讯推出微信公众平台功能,同年11月开放第三方接口 3)2013年11月注册 ...
- 堆--P1168 中位数
题目描述 给出一个长度为N的非负整数序列Ai,对于所有1≤k≤(N+1)/2,输出A1,A3,…,A2k−1的中位数.即前1,3,5,…个数的中位数. 输入格式 第1行为一个正整数N,表示了序列长度 ...