题目描述
Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.
This time mr. Boosch plans to sign 2k laws. He decided to choose exactly two non-intersecting segments of integers from 1 to n of length k and sign all laws, whose numbers fall into these segments. More formally, mr. Boosch is going to choose two integers a, b (1 ≤ a ≤ b ≤ n - k + 1, b - a ≥ k) and sign all laws with numbers lying in the segments [a; a + k - 1] and [b; b + k - 1] (borders are included).
As mr. Boosch chooses the laws to sign, he of course considers the public opinion. Allberland Public Opinion Study Centre (APOSC) conducted opinion polls among the citizens, processed the results into a report and gave it to the president. The report contains the absurdity value for each law, in the public opinion. As mr. Boosch is a real patriot, he is keen on signing the laws with the maximum total absurdity. Help him.
Input
The first line contains two integers n and k (2 ≤ n ≤ 2·105, 0 < 2k ≤ n) — the number of laws accepted by the parliament and the length of one segment in the law list, correspondingly. The next line contains n integers x1, x2, …, xn — the absurdity of each law (1 ≤ xi ≤ 109).
Output
Print two integers a, b — the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [a; a + k - 1]and [b; b + k - 1]. If there are multiple solutions, print the one with the minimum number a. If there still are multiple solutions, print the one with the minimum b.
Examples
Input
5 2 3 6 1 1 6
Output
1 4
Input
6 2 1 1 1 1 1 1
Output
1 3
Note
In the first sample mr. Boosch signs laws with numbers from segments [1;2] and [4;5]. The total absurdity of the signed laws equals 3 + 6 + 1 + 6 = 16.
In the second sample mr. Boosch signs laws with numbers from segments [1;2] and [3;4]. The total absurdity of the signed laws equals 1 + 1 + 1 + 1 = 4.
题意:在一段长度为n的数列中,选择两端不重叠的,长度为k的序列,使他们的和最大。
解析:一开始看到这个题,萌生了一个弱智的想法——没错就是线段树。一开始想的是用前缀和,表示从第i个数开始,向后数k个数的和,然后枚举左边以k为长度的区间,再用线段树求出右边不重合部分的最大值。但是后来,仔细一想,好像根本不需要这么做,只要从前往后,一边维护前面不重合部分的最大,一边枚举右边长度为k的最大就可以直接求出答案了。但是由于时间原因,第二份代码我还没有写,所以就只能贴出来第一种想法的代码了。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
long long a[400010];
long long sum[400010];
long long cnt[400010];
int n,k;
struct Edge{
int l,r;
long long maxx;
}tr[5000000];
void build(int d,int l,int r){
tr[d].l=l,tr[d].r=r;
if(l==r){
tr[d].maxx=cnt[l];
return ;
}
int ls=d*2,rs=d*2+1;
int mid=(l+r)/2;
build(ls,l,mid);build(rs,mid+1,r);
tr[d].maxx=max(tr[ls].maxx,tr[rs].maxx);
return ;
}
long long querry(int d,int l,int r){
int ll=tr[d].l,rr=tr[d].r;
// printf("%d %d %d %d %d\n",d,ll,rr,l,r);
if(r==rr&&l==ll){
return tr[d].maxx;
}
int ls=d*2,rs=d*2+1;
int mid=(ll+rr)/2;
if(mid>=r){
return querry(ls,l,r);
}
else if(mid<l){
return querry(rs,l,r);
}
else {
return max(querry(ls,l,mid),querry(rs,mid+1,r));
}
}
int main(){
cin>>n>>k;
for(int i=1;i<=n;i++){
scanf("%I64d",&a[i]);
}
for(int i=1;i<=n;i++){
sum[i]=sum[i-1]+a[i];
}
for(int i=1;i<=n;i++){
cnt[i]=sum[i+k-1]-sum[i-1];
}
long long ans_max=0;
long long ans_a=0;
long long ans_b=0;
build(1,1,n-k+1);
for(int i=1;i<=n-2*k+2;i++){
int l=i,r=i+k-1;
long long temp=cnt[l];
int rr=n-k+1;
if(rr>=r+1)temp+=querry(1,r+1,n-k+1);
if(temp>ans_max){
ans_max=temp;
ans_a=i;
ans_b=temp-cnt[l];
}
}
cout<<ans_a<<" ";
for(int i=ans_a+k;i<=n-k+1;i++){
if(ans_b==cnt[i]){
cout<<i<<endl;
return 0;
}
}
return 0;
}
今天的文章Maximum Absurdity分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/68506.html