bzoj 1112: [POI2008]砖块Klo(splay)

来源:互联网 发布:苹果平板打不开软件 编辑:程序博客网 时间:2024/04/25 20:32

1112: [POI2008]砖块Klo

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 1775  Solved: 616
[Submit][Status][Discuss]

Description

N柱砖,希望有连续K柱的高度是一样的. 你可以选择以下两个动作 1:从某柱砖的顶端拿一块砖出来,丢掉不要了. 2:从仓库中拿出一块砖,放到另一柱.仓库无限大. 现在希望用最小次数的动作完成任务.

Input

第一行给出N,K. (1 ≤ k ≤ n ≤ 100000), 下面N行,每行代表这柱砖的高度.0 ≤ hi ≤ 1000000

Output

最小的动作次数

Sample Input

5 3
3
9
2
3
1

Sample Output

2

HINT

原题还要求输出结束状态时,每柱砖的高度.本题略去.

Source

[Submit][Status][Discuss]


题解:splay

用splay维护区间的中位数,先将前k个数加入splay,维护splay中的数大小有序。

然后不断将区间后移,每次加入一个节点,对应得删除一个节点。

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#define N 100003#define LL long long using namespace std;const LL inf=1e18;int n,m,size[N],cnt[N],f[N],ch[N][3],root,sz;LL h[N],sum[N],key[N];void clear(int x){sum[x]=size[x]=cnt[x]=key[x]=f[x]=ch[x][1]=ch[x][0]=0;}int get(int x){return ch[f[x]][1]==x;}void update(int now){size[now]=(ch[now][0]?size[ch[now][0]]:0)+(ch[now][1]?size[ch[now][1]]:0)+1;sum[now]=(ch[now][0]?sum[ch[now][0]]:0)+(ch[now][1]?sum[ch[now][1]]:0)+key[now];}void rotate(int x){int y=f[x]; int z=f[y]; int which=get(x);ch[y][which]=ch[x][which^1]; f[ch[x][which^1]]=y;ch[x][which^1]=y; f[y]=x; f[x]=z;if (z) ch[z][ch[z][1]==y]=x;update(y); update(x);}void splay(int x,int tar){for (int fa;(fa=f[x])!=tar;rotate(x)) if (f[fa]!=tar) rotate(get(x)==get(fa)?fa:x);if (!tar) root=x;}void insert(LL x){int now=root,fa; if (!root) {++sz; size[sz]=1; key[sz]=x; sum[sz]=x; root=sz;return;}while (true){fa=now;now=ch[now][x>key[now]];if (!now) {sz++;  ch[fa][x>key[fa]]=sz; f[sz]=fa; key[sz]=x; size[sz]=1;  sum[sz]=x;update(sz); update(fa); splay(sz,0);return;}}}LL find(int x){int now=root;while (true){if (x<=size[ch[now][0]]) now=ch[now][0];else {x-=size[ch[now][0]];if (x==1) {splay(now,0);return key[now];}x--;now=ch[now][1];}}}int pre(){int now=ch[root][0];while (ch[now][1]) now=ch[now][1];return now;}void del(int x){splay(x,0);    if (!ch[root][0]&&!ch[root][1])       {          clear(root); root=0; return;       }      if (!ch[root][0])       {          int ordr=root; root=ch[root][1]; f[root]=0; clear(ordr); update(root); return;       }      if (!ch[root][1])       {          int ordr=root; root=ch[root][0]; f[root]=0; clear(ordr); update(root); return;       }      int leftbig=pre(),oldr=root;      splay(leftbig,0);  f[ch[oldr][1]]=root;  ch[root][1]=ch[oldr][1]; clear(oldr); update(root);      return;  }int main(){freopen("a.in","r",stdin);//freopen("my.out","w",stdout);scanf("%d%d",&n,&m);int maxn=0; for (int i=1;i<=n;i++) scanf("%I64d",&h[i]);LL ans=inf;for (int i=1;i<=m;i++)  insert(h[i]);int mid=m/2+1; LL t=find(mid);ans=(LL)size[ch[root][0]]*t-sum[ch[root][0]]+sum[ch[root][1]]-(LL)size[ch[root][1]]*t;//cout<<ans<<endl;for (int i=m+1;i<=n;i++) {insert(h[i]);del(i-m);LL t=find(mid);     ans=min(ans,(LL)size[ch[root][0]]*t-sum[ch[root][0]]+sum[ch[root][1]]-(LL)size[ch[root][1]]*t);}printf("%I64d\n",ans);}



0 0
原创粉丝点击