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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值