Contiguous Repainting(C++)(前缀和)

目录

1.题目描述

2.AC


1.题目描述

 Contiguous Repainting

时间限制: 2.000 Sec  内存限制: 256 MB

题目描述

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.

样例输入 Copy

5 3
-10 10 -10 10 -10

样例输出 Copy

10

提示

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

标签

前缀和

2.AC

#include <iostream>
#include <cstdio>
using namespace std;
int n, k;
long long a[100005], sum[100005], sump[100005], ans;
int main () {
	scanf("%d%d", &n, &k);
	for (int i = 1; i <= n; i++) {
		scanf ("%lld", &a[i]);
		sum[i] = sum[i-1] + a[i];
		sump[i] = sump[i-1] + a[i]*(a[i]>0);
	}
	for (int i = k; i <= n; i++) {
		long long temp = sum[i] - sum[i-k];
		temp = sump[n] - sump[i] + sump[i-k] + temp*(temp>0);
		ans = max (ans, temp);
	}
	cout<<ans;
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值