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

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 equals3 + 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 equals1 + 1 + 1 + 1 = 4.


题意: 求两个长度为k的不相交区间, 使得这两个区间之内的值相加和最大,输出两个区间的开始位置.

分析: 最简单的想法就是枚举每个元素为第一个区间的开始, 求这个区间之内的元素的和, 然后枚举另一个区间, 不断更新最大值, 然而这样复杂度较高,

但是第二次枚举的时候可以用线段树来查询最大值, 于是复杂度可以降到O(NlogN).


#include<bits/stdc++.h>

#define inf 0x3f3f3f3f
using namespace std;

typedef long long ll;
typedef pair<ll,int> pii;

const int N=200010,MOD=1e9+7;
int val[N];
ll sum[N],b[N];

struct st
{
    int l,r;
    pii mx;
}a[N<<2];

pii MAX(pii a,pii b)
{
    if(a.first != b.first) return max(a,b); //如果两个区间的和相等, 那么取下标小的区间
    return pii(a.first,min(a.second,b.second));
}

void build(int l,int r,int i)
{
    a[i].l=l,a[i].r=r;
    if(l==r){
        a[i].mx.first=b[l];
        a[i].mx.second=l;
        return;
    }
    int mid = l+r>>1;
    build(l,mid,i<<1);
    build(mid+1,r,i<<1|1);
    a[i].mx = MAX(a[i<<1].mx,a[i<<1|1].mx);
}

pii query(int l,int r,int i)//返回最大值的位置, 
{
    if(a[i].l>=l && a[i].r<=r) return a[i].mx;
    if(a[i].l>r || a[i].r<l) return pii(-1,-1);
    return MAX(query(l,r,i<<1),query(l,r,i<<1|1));
}

int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++) scanf("%d",val+i);
    for(int i=1;i<=n;i++){
        sum[i] = sum[i-1]+val[i];
    }
    for(int i=1;i+k-1<=n;i++)
    {
        b[i] = sum[i+k-1]-sum[i-1];
    }
    build(1,n+1-k,1);
    int x,y;
    ll mx=0;
    for(int i=k+1;i+k-1<=n;i++){
        pii now = query(1,i-k,1);
        if(b[i]+now.first > mx){
            x = now.second;
            y = i;
            mx = b[i]+now.first;
        }
    }
    printf("%d %d\n",x,y);
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值