Maximum Absurdity

题目描述

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 ab (1 ≤ a ≤ b ≤ n - k + 1, b - a ≥ k) and sign all laws with numbers lying in the segments [aa + k - 1] and [bb + 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 ab — the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [aa + k - 1]and [bb + 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;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值