CodeForces - 332B. Maximum Absurdity(前綴和+dp)

B. Maximum Absurdity
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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.

題目大意:讓你找到兩個不相交的連續的長度爲k的區間,使得這兩個區間的總和值最大。
解題思路:我們可以運用前綴和的知識來解決這一題,先用兩個數組來記錄某個點往前和往後的最大值,再枚舉這個點,不斷更新和鉅鹿兩個區間和的最大值,dp的思想。

AC代碼:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, a, b) G[a].push_back(b);
using namespace std;
typedef long long LL;
typedef pair<int, int> par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

int n, k;
int a[200010], idx1[200010], idx2[200010];
LL sum[200010], m1[200010], m2[200010];

int main()
{
    while(~scanf("%d%d", &n, &k)) {
        sum[0] = 0;
        mem0(m1);
        mem0(m2);
        int l, r;
        LL ans = 0;
        for(int i = 1; i <= n; i ++) {
            in1(a[i]);
            sum[i] = sum[i-1]+a[i]; //前綴和數組
        }
        for(int i = 1; i <= n-2*k+1; i ++) { //記錄往前的最大值和區間位置
            if(sum[i+k-1] - sum[i-1] > m1[i+k-2]) {
                m1[i+k-1] = sum[i+k-1] - sum[i-1];
                idx1[i+k-1] = i;
            }else {
                m1[i+k-1] = m1[i+k-2];
                idx1[i+k-1] = idx1[i+k-2];
            }
        }
        for(int i = n-k+1; i >= k+1; i --) { //後面的
            if(sum[i+k-1] - sum[i-1] >= m2[i+1]) {
                m2[i] = sum[i+k-1] - sum[i-1];
                idx2[i] = i;
            }else {
                m2[i] = m2[i+1];
                idx2[i] = idx2[i+1];
            }
        }
        for(int i = k; i <= n-k; i ++) {
            if(m1[i]+m2[i+1] > ans) { //尋求最優解
                ans = m1[i]+m2[i+1];
                l = idx1[i];
                r = idx2[i+1];
            }
        }
        printf("%d %d\n", l, r);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章