Codeforces Round #193 (Div. 2) B. Maximum Absurdity

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 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·1050 < 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.

Sample test(s)
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.

     开始比较天真,写了个枚举两个区间的算法时间复杂度o(n^2)果断超时了。然后就想用rmq但rmq的时间复杂度o(n*logn)担心也会超时。于是就想用dp做了。回想了各种dp题。以前求连续区间和都只是一个区间没处理两个区间的。无奈为了不让rating掉的太惨只能硬着头皮上了。写了下最优解的结构

ans=MAX(sum[i]-sum[i-k]+sum[j]-sum[j-k])。i<j。sum[i]代表前i项和。i和j是区间的右端点。

答案要求左端点。左端点用右端点-k+1就行了。所以是一样的。

ans=MAX(sum[i]-sum[i-k])+sum[j]-sum[j-k]。

所以我们枚举j。即右区间的右端点就行了。再用状态转移辅助量q[i]表示

前1到i个元素连续k个元素的最大值。那么

ans=q[j-k]+sum[j]-sum[j-k]。

q[i]=MAX(q[i-1],sum[i]-sum[i-k])。

时间复杂度瞬间就降到o(n)了。

问题就迎刃而解了。。。。。。。

代码如下:

#include <iostream>
#include<stdio.h>
#include<string.h>
#define MAX(a,b) ((a)>(b)?(a):(b))
using namespace std;

__int64 sum[200100],ma;
struct node
{
    __int64 maxx;//记录值
    int id;//记录下标
} q[200100];

int main()
{
    int n,k,i,p1,p2;

    sum[0]=0;
    while(~scanf("%d%d",&n,&k))
    {
        ma=-1;
        memset(q,0,sizeof q);
        for(i=1; i<=n; i++)
        {
            scanf("%I64d",&sum[i]);
            sum[i]+=sum[i-1];
        }
        for(i=k; i<=n; i++)//由于是从大到小枚举的所以a,b都是最小的
        {
            if(sum[i]-sum[i-k]>q[i-1].maxx)//更新q[i]
            {
                q[i].maxx=sum[i]-sum[i-k];
                q[i].id=i;
            }
            else
            {
                q[i].maxx=q[i-1].maxx;
                q[i].id=q[i-1].id;
            }
            if(sum[i]-sum[i-k]+q[i-k].maxx>ma)
            {
                ma=sum[i]-sum[i-k]+q[i-k].maxx;
                p1=q[i-k].id-k+1;//左起点
                p2=i-k+1;//右起点
            }
        }
        printf("%d %d\n",p1,p2);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值