Codeforces Round #193 (Div. 2) B. Maximum Absurdity (DP、RMQ两种解法)

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.

题意:求两段长度为k的子列的最大和,两段不能相交,输出两个子列的开始位置。

思路(1):DP的思想,枚举第二个子列的开始位置。维护这个位置前的长度为k的子列的最大值就够了。

复杂度:O(n)

代码:

#include <cstdio>
#include <cstring>
#define maxn 200005
using namespace std;

int n,m,k;
long long a[maxn],sum[maxn];

int main()
{
    int i,j,s,st,e;
    long long ma,t,tmp,prema;
    scanf("%d%d",&n,&k);
    sum[0]=0;
    for(i=1;i<=n;i++)     // sum存前n项和
    {
        scanf("%I64d",&a[i]);
        sum[i]=sum[i-1]+a[i];
    }
    ma=-1;
    prema=-1;
    for(j=k+1;j<=n-k+1;j++) 
    {
        t=sum[j+k-1]-sum[j-1];   // 枚举子列二
        tmp=sum[j-1]-sum[j-k-1]; 
        if(tmp>prema)       // 维护子列一的最大值
        {
            s=j-k;
            prema=tmp;
        }
        if(prema+t>ma)
        {
            st=s;    // 注意这行必须有 不能直接输出s、e  因为最大值没有更新时s也可能在变
            e=j;
            ma=prema+t;
        }
    }
    printf("%d %d\n",st,e);
    return 0;
}

思路(2):RMQ,枚举子列一,查询子列二的最大值。(RMQ的查询可以达到O(1))

复杂度:RMQ初始化O(n*lg(n))+O(n)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 300005
using namespace std;

int n,m,mm,k,ans,t;
long long a[maxn],sum[maxn];
long long f[maxn][64];

long long maxx(long long x1,long long x2)
{
    return x1<x2?x2:x1;
}
void init()
{
    int i,j;
    mm=n-k+1;
    for(i=1;i<=mm;i++)
    {
        f[i][0]=sum[i];
    }
    for(j=1;(1<<j)<=mm;j++)
    {
        for(i=1;i+j-1<=mm;i++)
        {
            f[i][j]=maxx(f[i][j-1],f[i+(1<<(j-1))][j-1]);   // 数组得开大一点 不然这里可能越界
        }
    }
}
long long query(int l,int r)
{
    t=0;
    while((1<<(t+1))<=r-l+1) t++;
    return maxx(f[l][t],f[r-(1<<t)+1][t]);
}
int main()
{
    int i,j,s,e;
    long long ma,tmp;
    scanf("%d%d",&n,&k);
    sum[1]=0;
    for(i=1;i<=n;i++)
    {
        scanf("%I64d",&a[i]);
        if(i<=k) sum[1]+=a[i];
    }
    for(i=2;i<=n-k+1;i++)    // sum[i]存 a[i]~a[i+k-1] 的和
    {
        sum[i]=sum[i-1]-a[i-1]+a[i+k-1];
    }
    init();
    m=n-2*k+1;
    ma=-1;
    for(i=1;i<=m;i++)        // 枚举第一段 查询第二段 扫描一遍可以得到s和ma
    {
        tmp=sum[i]+query(i+k,n-k+1);
        if(tmp>ma)
        {
            s=i;
            ma=tmp;
        }
    }
    tmp=ma-sum[s];
    for(i=s+k;i<=n-k+1;i++) // 根据s、ma就很好得到e了
    {
        if(sum[i]==tmp)
        {
            e=i;
            break ;
        }
    }
    printf("%d %d\n",s,e);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值