bzoj 1112: [POI2008]砖块Klo(splay)
来源:互联网 发布:苹果平板打不开软件 编辑:程序博客网 时间:2024/04/25 20:32
1112: [POI2008]砖块Klo
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 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
3
9
2
3
1
Sample Output
2
HINT
原题还要求输出结束状态时,每柱砖的高度.本题略去.
Source
题解: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
- bzoj 1112: [POI2008]砖块Klo(splay)
- 【BZOJ 1112】 [POI2008]砖块Klo
- bzoj 1112 [POI2008]砖块Klo
- 1112: [POI2008]砖块Klo Splay+中位数
- [BZOJ1112][POI2008]砖块Klo(splay)
- Bzoj1112:[POI2008]砖块Klo:splay
- 【BZOJ1112】[POI2008]砖块Klo【Splay】
- BZOJ 1112 POI2008 砖块Klo Treap
- BZOJ 1112 [POI2008]砖块Klo Treap
- bzoj 1112: [POI2008]砖块Klo treap
- 【BZOJ 1112】Poi2008砖块Klo treap
- 【BZOJ】1112 [POI2008]砖块Klo 平衡树
- BZOJ 1112: [POI2008]砖块Klo treap
- BZOJ 1112 [POI2008]砖块Klo Treap
- 1112: [POI2008]砖块Klo
- 1112: [POI2008]砖块Klo
- BZOJ 1112: [POI2008]砖块Klo 线段树维护区间中位数
- BZOJ 1112: [POI2008]砖块Klo 平衡树,思维,枚举
- DES ~ 对称加密算法
- java socket通信 总结
- 广东工业大学 新生赛 BProblem B: Zhazhahe究竟有多二
- web.config 之数据库连接串 --笔记
- 《寒门贵子》,一篇励志震撼的演讲稿
- bzoj 1112: [POI2008]砖块Klo(splay)
- 走马灯闪动的TextView
- PHP static局部静态变量和全局静态变量总结
- UML之活动图和状态图
- Linux 常用命令2
- powerdesigner15 逆向工程,mysql通过powerdesigner反向生成pdm文件
- 字符排序
- cvLoadImage()
- HTML5的本地存储功能,使用localStorage