JZOJ4815. 【NOIP2016提高A组五校联考4】

来源:互联网 发布:中兴u960s软件下载 编辑:程序博客网 时间:2024/05/23 20:29

Description

这里写图片描述

Input

这里写图片描述

Output

这里写图片描述

Sample Input

样例输入1:

3 4
1 3 4

样例输入2:

3 3
10 2 7

Sample Output

样例输出1:

8 7 4 4

样例输出2:

19 12 10

Data Constraint

这里写图片描述

Hint

这里写图片描述

分析

因为N非常大,
所以我们不可能将所有的子串枚举出来。

其实,我们只需要找到前K个子串就可以了。,
有一些子串是可以不用枚举的。

因为所有的数都是正整数,
所以最大的子串就一定是全部加起来,

那么,第二大的呢?
要么去掉最左边的一个,要么去掉最右边的一个。

我们可以得到一个结论:
一个子串[l,r]是当前最大的,
那么,下一次再选最大的时候才有可能选到[l+1,r]和[l,r-1]

我们就建一个堆,先放入[1,n]
然后取出最大的[l,r],
然后在加入[l+1,r],[l,r-1]
只要判断一下重复就可以了。

code(c++)

#include <cstdio>#include <algorithm>#include <cstring>#include <string.h>#include <cmath>#include <math.h> #include<queue>using namespace std;struct note{int l,r;long long s;}; priority_queue <note> q;int n,k,a[100003];long long s;note t,z;bool operator <(note a,note b){return a.s<b.s;}int main(){    freopen("ksum.in","r",stdin);    freopen("ksum.out","w",stdout);    scanf("%d%d",&n,&k);    for(int i=1;i<=n;i++)    {        scanf("%d",&a[i]);        s+=a[i];    }    t.s=s;t.l=1;t.r=n;    q.push(t);    for(int i=1;i<=k;i++)    {        t=q.top();        q.pop();        printf("%lld ",t.s);        if(t.l!=t.r)        {            z=t;z.r--;z.s-=a[t.r];            q.push(z);            if(t.r==n)            {                z=t;z.l++;z.s-=a[t.l];                q.push(z);            }        }    }}
2 0