ksum

题目描述

这里写图片描述

瞎搞

经典题了

#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=100000+10;
int left[maxn],right[maxn];
ll heap[maxn],sum[maxn];
int i,j,k,l,r,t,n,m,tot;
int read(){
    int x=0;
    char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x;
}
void add(ll x,int l,int r){
    heap[++tot]=x;
    left[tot]=l;right[tot]=r;
    int j=tot;
    while (j>1&&heap[j]>heap[j/2]){
        swap(heap[j],heap[j/2]);
        swap(left[j],left[j/2]);
        swap(right[j],right[j/2]);
        j/=2;
    }
}
void pop(){
    heap[1]=heap[tot];
    left[1]=left[tot];right[1]=right[tot];
    tot--;
    int j=1,k;
    while (j*2<=tot){
        if (j*2+1>tot||heap[j*2]>heap[j*2+1]) k=j*2;else k=j*2+1;
        if (heap[j]<heap[k]){
            swap(heap[j],heap[k]);
            swap(left[j],left[k]);
            swap(right[j],right[k]);
            j=k;
        }
        else break;
    }
}
int main(){
    freopen("ksum.in","r",stdin);freopen("ksum.out","w",stdout);
    n=read();k=read();
    fo(i,1,n){
        t=read();
        sum[i]=sum[i-1]+(ll)t;
    }
    fo(i,1,n) add(sum[n]-sum[i-1],i,n);
    while (k--){
        printf("%lld ",heap[1]);
        l=left[1];r=right[1];
        pop();
        if (l!=r) add(sum[r-1]-sum[l-1],l,r-1);
    }
    printf("\n");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值