CF 332 B.Maximum Absurdity【DP】

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.

Example
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.

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long int
const ll MIN = 1e9 + 10;
const int maxn = 2e5 + 10;
ll n; 
ll k;
ll a[maxn];
ll b[maxn];
ll ans[maxn];
struct node
{
    ll val;
    ll ID;
}num[maxn];
//bool cmp(node a, node b)
//{
//  return a.val > b.val;
//  if (a.val == b.val)
//  {
//      return a.ID < b.ID;
//  }
//}
int main()
{
    while (cin >> n>> k)
    {
        //cin >> k;
        memset(b, 0, sizeof(b));
        memset(a, 0, sizeof(a));  
        memset(ans, 0, sizeof(ans));
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
            b[i] = b[i - 1] + a[i];
        }
        for (int i = 1; i <= n - k + 1; i++)
        {
            ans[i] = b[i + k - 1] - b[i - 1];
        }
        int L,R,tmp; ll M = -MIN, S = -MIN;
        for (int i = n - 2 * k + 1; i >= 1; i--)
        {
            if (M <= ans[i + k])
            {
                M = ans[i + k];
                tmp = i + k;
            }
            if (S <= M + ans[i])
            {
                S = M + ans[i];
                L = i;
                R = tmp;
            }
        }
        cout << L << " " << R << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值