CodeForces - 332B ​​​​​​​ Maximum Absurdity (前缀和 + 线段树查询区间最大,以及返回下标)

Maximum Absurdity

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·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 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 numbera. 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 的 子数组,是两个子数组的和最大。

思路:首先肯定能想到 前缀和 预处理出每一段 长度为 k 的子数组的和。然后 就是 两个和最大的问题,但是 两段又不能相交,如果直接枚举的话就是 两个 for , 时间复杂度为 O(n^2),很显然会超时。 对于 区间问题,大都可以用线段树来解决,所以 可以枚举一个起点,然后另一个 通过 线段树 查询剩下区间的最大值,其时间复杂度为  O(N logN);

AC代码:

#include<bits/stdc++.h>
using namespace std;

#define pli pair<ll,int>
typedef long long ll;
const int maxn = 2e5 + 10;
ll x[maxn];
ll sum[maxn];
ll D[maxn];
struct node{
    int l,r;
    pli A;
}tr[maxn << 2];

pli MAX(pli A,pli B){
    if(A.first != B.first) return max(A, B);        /// 对于 pair, 默认情况下 都是按照 first 进行比较,排序的时候也是
    return pli(A.first,min(A.second,B.second));     ///first 相同,也就是 和 相同,返回起点小的
}

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

pli query(int ql,int qr,int l,int r,int i){
    //printf("ql: %d qr:%d   l: %d r: %d\n\n",ql,qr,l,r);
    if(ql <= l && r <= qr){
        //printf("tr: %lld %d\n",tr[i].A.first,tr[i].A.second);
        return tr[i].A;
    }
    int mid = (l + r) >> 1;
    pli maxx = make_pair(0ll,0);    /// 0ll  , 就是 (long long) 0;
    if(ql <= mid) maxx = MAX(maxx,query(ql,qr,l,mid,i << 1));
    if(qr > mid) maxx = MAX(maxx,query(ql,qr,mid + 1,r,i << 1 | 1));
    //printf("maxx: %d %d\n\n",maxx.first,maxx.second);
    return maxx;
}

int main()
{
    int n,k;
    while(~scanf("%d%d",&n,&k)){
        for(int i = 1;i <= n;i ++)
            scanf("%I64d",&x[i]);
        sum[0] = 0;
        for(int i = 1;i <= n;i ++)
            sum[i] = sum[i-1] + x[i];
        int p = 0;
        for(int i = k;i <= n;i ++)
            D[i-k+1] = sum[i] - sum[i-k];                     ///即区间 [i-k+1,i] ;

        build(1,n - k + 1,1);
        ll maxx = -1;int a,b;
        for(int i = 1;i <= n - 2 * k + 1;i ++){
            ll t = D[i];
            pli tmp = query(i + k,n - k + 1,1,n - k + 1,1);     ///在剩余部分的区间中查询最大值
            //printf("%lld %lld %d\n",t,tmp.first,tmp.second);
            if(maxx < t + tmp.first){
                maxx = t + tmp.first;
                a = i; b = tmp.second;
            }
            else if(maxx == t + tmp.first){
                if(a == i) b = tmp.second;
            }
        }
        printf("%d %d\n",a,b);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值