动态规划 求两个没交集的区间的和的最大值的范围的左边界

来源:互联网 发布:js删除json数组 对象 编辑:程序博客网 时间:2024/06/08 10:00

Description

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 [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 a, b — 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.

Sample Input

Input
5 23 6 1 1 6
Output
1 4
Input
6 21 1 1 1 1 1
Output
1 3

Hint

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.



这道题一开始用暴力枚举肯定超时,就去找了下题解,原来是用动归做,

sum[i]储存送1到i的和,q[i]中储存的是从1到i中区间为k的的最大值以及区间和最大时其右边端点的位置

sum[i]-sum[i-k]代表从i-k+1到i的区间的和,接着只要判断这个是不是比之前大,是的话就将这个大的赋值进去q,否则将上一个的值赋给q


#include <iostream>#include <cstring>#include <cmath>#include <algorithm>long long sum[500001];struct pos{long long max_;int pos;}q[500001];using namespace std;int main(){memset(q,0,sizeof(q));int n,k,l,r;cin >> n >> k;sum[0]=0;long long  mm=-1;for(int i=1; i <= n; ++i){cin >> sum[i];sum[i]+=sum[i-1];}for(int i=k; i <= n; ++i){if(sum[i]-sum[i-k]>q[i-1].max_){q[i].max_=sum[i]-sum[i-k];q[i].pos=i;}else{q[i].max_=q[i-1].max_;q[i].pos=q[i-1].pos;}if(sum[i]-sum[i-k]+q[i-k].max_>mm){mm=sum[i]-sum[i-k]+q[i-k].max_;l=q[i-k].pos-k+1;r=i-k+1;}}cout << l << " " << r << endl;}




0 0