CodeChef REIGN解题报告

REIGN题解

中文题面见https://vj.ti12z.cn/d30971fe9f8f47fb2a3e458793ff579c?v=1564349228

The Baratheons have been ruling in the Seven Kingdoms for many years. They have seen a lot: prosperity and hunger, tranquility and rebellions, live and death. But nevertheless, they still hold the throne.

King Joffrey Baratheon's reign is running now. As a wise man, he honors the history of his family. So, he commanded to build up two monuments, that will remind about some historical periods of the Baratheons.

Formally, the Baratheons have been ruling for N years. Every year is described by an integer Ai, the level of prosperity in i-th year. If i-th year was a great year, then Ai might be a positive integer. Otherwise, if i-th year was a horrible year, then Ai might be a negative integer.

Each historical period can be described as two integers S and F, the start and the finish of the period respectively. Of course, S is not greater than F for each period, that we consider in this task.

You are to pick two historical periods, but there are some rules:

Two periods shouldn't have common years. I.e. a period [1, 5] has no common years with a period [6, 7];
The first period should start earlier than the second one. I.e. a period [1, 5] starts earlier than [6, 7];
Two periods shouldn't be too close to each other. There must be at least K years between the finish of the first period and the start of the second period. I.e. periods [1, 5] and [10, 10] can be chosen in case K equals to 4, while they can't in case K equals to 5.
The sum of the levels of prosperity in chosen years should be as big as possible.
Afterwards, you should report the sum of the levels of prosperity in chosen years to your King. Make everything right, otherwise King Joffrey won't be merciful!

Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first line of each test case contains two integers N and K denoting the length of the Baratheons' reign and the minimal amount of years between two chosen periods.

The second line contains N space-separated integers A1, A2, ..., AN denoting the levels of prosperity in corresponding years.

Output
For each test case, output a single line containing the required integer.

Constraints
1 ≤ T ≤ 5;
2 ≤ N ≤ 105;
0 ≤ K ≤ 105;
-109 ≤ Ai ≤ 109 for every 1 ≤ i ≤ N;
K + 2 ≤ N

Example
Input:
3
5 3
-1 1 2 3 -1
8 3
5 5 -1 -2 3 -1 2 -2
6 0
5 -1 5 0 -1 9

Output:
-2
12
18

Explanation
There are T = 3 test cases the input.

The first test case: N equals to 5, K equals to 3, A[] equals to {-1, 1, 2, 3, -1}. There is the only option: we have to choose [1, 1] and [5, 5] periods.

The second test case: N equals to 8, K equals to 3, A[] equals to {5, 5, -1, -2, 3, -1, 2, -2}. It is optimal to choose [1, 2] and [7, 7] periods. That is the only optimal choice that you can make.

The second test case: N equals to 6, K equals to 0, A[] equals to {5, -1, 5, 0, -1, 9}. It is optimal to choose [1, 3] and [6, 6] periods. But that is not the only optimal choice that you can make.

题解

看起来好像是求两个距离为K的最大子段和?
用ff和gg数组分别保存从前往后和从后往前的最大子段和
至于最大子段和直接用f和g求就是了
最后枚举距离k的前一位求最大值

代码

#include <cstdio>
#define ll long long
#define inf 0x7f7f7f7f
using namespace std;
ll read(){
	ll x = 0, f = 1; char c = getchar();
	while(c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
	while(c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar();}
	return x * f;
}
const int maxn=1e5+5;
ll n,k;
ll a[maxn],f[maxn],g[maxn],ff[maxn],gg[maxn],ans1,ans2,ans;
ll max(ll a,ll b) {return a>b?a:b;}
void init(){
	n=read(),k=read();
	for (ll i(1);i<=n;++i) a[i]=read();
}
void doit(){
	f[1]=ff[1]=a[1],g[n]=gg[n]=a[n],ans=-inf;
	for (ll i(2);i<=n;++i){
		f[i]=f[i-1]+a[i];
		if (f[i-1]<=0) f[i]=a[i];
		ff[i]=max(f[i],ff[i-1]);
		int j=n-i+1;
		g[j]=g[j+1]+a[j];
		if (g[j+1]<=0) g[j]=a[j];
		gg[j]=max(g[j],gg[j+1]);
	}
	for (ll i(1);i+k+1<=n;++i)
		ans=max(ans,ff[i]+gg[i+k+1]);
	printf("%lld
",ans);
}
signed main(){
	ll T=read();
	while (T--){
		init();
		doit();
	}
	return 0; 
}
/*
4
5 3
-1 1 2 3 -1
8 3
5 5 -1 -2 3 -1 2 -2
6 0
5 -1 5 0 -1 9
9 1
-9 -10 -11 -1 999999 -9 -9 -9 -9
最后一个是毒瘤样例*/

原文地址:https://www.cnblogs.com/cancers/p/11282153.html