您的位置:首页 > 其它

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

2016-10-09 22:18 477 查看

题目



样例输入:



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);
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: