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

12 篇文章 0 订阅
8 篇文章 0 订阅

题目

这里写图片描述
样例输入:
这里写图片描述
3 4
1 3 4
样例输出:
这里写图片描述
8 7 4 4
这里写图片描述


剖解题目

。。。。。。。。


思路

看到ai每个都是正整数,就立刻想到最大的肯定是全部,下一个肯定就是删掉头尾任意一个数后得到的子序列。
很明显是堆嘛。。。然而我几乎没有打过堆。QwQ。
我是正直的人,才不会用c++中的自带堆╭(╯^╰)╮。
/(ㄒoㄒ)/~~
手打的时候搞了很久,关于那个判断重复的地方不知道怎么想的搞了个模拟链表上去,然后调不出来,就炸了。


解法

部分分就不说了。暴力能拿40分。
正解在上面,这里就说说关于判重的问题。
一:可以用hash判重。
二:可以不用判重。只要一开始把[1,n],[2,n]…….[n-1,n]加入堆中,每次遇到一个区间[l,r],就只需要将[l,r-1]扔入堆中就可以了。


代码

#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define ll long long

using namespace std;

const int maxn=1e5+5,ha1=146639,ha2=143401,mo=396323;
ll a[maxn],hep[maxn*3][3],ha[mo][2];
int n,k,num;

bool hash(ll x,ll l,ll r)
{
    ll y=x;
    while ((ha[y][0]!=l||ha[y][1]!=r)&&ha[y][0]) y=(y+1)%mo;
    if (!ha[y][0]) {
        ha[y][0]=l; ha[y][1]=r;
        return false;
    }
    return true;
}
void up(int x)
{
    while (x!=1){
        if (hep[x/2][0]<hep[x][0]){
            swap(hep[x/2][0],hep[x][0]);
            swap(hep[x/2][1],hep[x][1]);
            swap(hep[x/2][2],hep[x][2]);
        }
        x/=2;
    }
}
void down(int x)
{
    hep[x][0]=0; hep[x][1]=0; hep[x][2]=0;
    while (hep[x][0]<hep[x<<1][0]||hep[x][0]<hep[x<<1|1][0]) {
        if (hep[x<<1][0]>hep[x<<1|1][0]) {
            swap(hep[x<<1][0],hep[x][0]);
            swap(hep[x<<1][1],hep[x][1]);
            swap(hep[x<<1][2],hep[x][2]);
            x<<=1;
        }
        else {
            swap(hep[x<<1|1][0],hep[x][0]);
            swap(hep[x<<1|1][1],hep[x][1]);
            swap(hep[x<<1|1][2],hep[x][2]);
            x=x<<1|1;
        }
    }
}
void put(ll val,ll l,ll r)
{
    if (l>r) return;
    ll id=l*ha1%mo*r%mo*ha2%mo;
    if (hash(id,l,r)) return;
    while (hep[num][0]) ++num;
    hep[num][0]=val; hep[num][1]=l; hep[num][2]=r;
    up(num);
}
int main()
{
    freopen("ksum.in","r",stdin);
    freopen("ksum.out","w",stdout);
    scanf("%d%d",&n,&k);
    ll sum=0;
    fo(i,1,n){
        scanf("%lld",&a[i]);
        sum+=a[i];
    }
    hep[1][0]=sum; hep[1][1]=1; hep[1][2]=n;
    num=1; ll now;
    fo(i,1,k){
        printf("%lld ",hep[1][0]);
        now=hep[1][0]-a[hep[1][1]];
        put(now,hep[1][1]+1,hep[1][2]);
        now=hep[1][0]-a[hep[1][2]];
        put(now,hep[1][1],hep[1][2]-1);
        down(1); 
    }
}

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值