【jzoj4678】【钱仓】【平衡树】【avl】【队列】
来源:互联网 发布:初三化学视频教程软件 编辑:程序博客网 时间:2024/05/16 04:46
题目大意
有n个循环的槽,有n个物品分配在槽里,可以将一个物品沿顺时针方向移动到另一个槽里,花费为距离的平方,求最少花费使每个槽刚好有一个物品。
解题思路
显然每个物品移动不超过一圈,可以找到一个地方开始逐一移动满足要求。可以看做每个槽有多余(+)或需求(-),一个合法的开头做前缀和后一定大于等于零。可以发现一个点只要有剩余就可以覆盖更多的点,如果一个点被别人覆盖一定优于没被人覆盖,因为被人覆盖不会减少多余,这样每个点只会被人覆盖一次,而合法的开头可以覆盖全部。
有一个显然的结论,一个物品应移动到最近的地方,可以把原来各物品挤掉,这样一定不会更劣。我们可以维护有那些店要放到当前的槽,选最远的点放到当前的槽。这就要用到数据结构了,若维护距离就要用到平衡树,支持插入询问和区间修改。然而这个方法很笨,我就是用了。可以将点重标号后用线段树,甚至可以倒着做维护最远的空位,然后放进去。
code
#include<set>#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>#define LL long long#define fo(i,j,k) for(int i=j;i<=k;i++)#define fd(i,j,k) for(int i=j;i>=k;i--)using namespace std;int const maxn=300000,inf=2147483647;int n,cntpon=1,c[maxn+10],cc[maxn+10],next[maxn+10],key[maxn+10],dep[maxn+10],add[maxn+10],father[maxn+10],son[maxn+10][3];void updata(int pos){ dep[pos]=max(dep[son[pos][0]],dep[son[pos][1]])+1;}void pushtag(int pos){ key[pos]+=add[pos]; fo(i,0,1) if(son[pos][i])add[son[pos][i]]+=add[pos]; add[pos]=0;}void rotate(int pos){ if(!father[father[pos]])return; if(add[pos])pushtag(pos); int tag=(son[father[pos]][1]==pos),tmp=father[pos]; son[father[tmp]][son[father[tmp]][1]==tmp]=pos; father[pos]=father[tmp]; son[tmp][tag]=son[pos][!tag]; if(son[pos][!tag])father[son[pos][!tag]]=tmp; son[pos][!tag]=tmp; father[tmp]=pos; updata(tmp);updata(pos);}void insert(int pos,int val){ if(add[pos])pushtag(pos); int tag=(val>=key[pos]); if(!son[pos][tag]){ son[pos][tag]=++cntpon; father[cntpon]=pos; key[cntpon]=val; dep[cntpon]=1; }else insert(son[pos][tag],val); if(abs(dep[son[pos][0]]-dep[son[pos][1]])>1)rotate(son[pos][dep[son[pos][1]]-dep[son[pos][0]]>1]); else updata(pos);}int getdelmax(int pos){ for(;son[pos][1];pos=son[pos][1]) if(add[pos])pushtag(pos); if(add[pos])pushtag(pos); son[father[pos]][1]=son[pos][0]; if(son[pos][0])father[son[pos][0]]=father[pos]; int tmp=father[pos];father[pos]=0; for(;tmp;updata(tmp),tmp=father[tmp]); return key[pos];}int main(){ freopen("d.in","r",stdin); freopen("d.out","w",stdout); scanf("%d",&n); fo(i,1,n)scanf("%d",&c[i]),cc[i]=c[i]-1,next[i]=i%n+1; int pos; fo(i,1,n) if((cc[i]>=0)&&(next[i]!=i)){ for(;(next[i]!=i)&&(cc[i]+cc[next[i]]>=0);){ cc[i]+=cc[next[i]]; int tmp=next[next[i]]; next[next[i]]=next[i]; next[i]=tmp; } if(next[i]==i){ pos=i; break; } } LL ans=0;key[1]=-inf,dep[1]=1; fo(i,1,n){ fo(j,1,c[pos])insert(1,0); int tmp=getdelmax(1); ans+=1ll*tmp*tmp; if(son[1][1])add[1]++; pos=pos%n+1; } printf("%lld\n",ans); return 0;}
0 0
- 【jzoj4678】【钱仓】【平衡树】【avl】【队列】
- 【JZOJ4678】钱仓
- AVL(平衡树)
- AVL平衡树实现
- AVL 平衡二叉树
- 平衡二叉树(AVL)
- AVL平衡树实现
- 平衡二叉树(AVL)
- AVL平衡树
- AVL 平衡二叉树
- avl平衡二叉树
- 二叉平衡树AVL
- 平衡二叉树(AVL)
- 平衡二叉树(AVL)
- 平衡二叉树 AVL
- 平衡二叉树(AVL)
- 平衡二叉树(AVL)
- 平衡二叉树(AVL)
- 解决toolbar标题不显示问题
- IOS 使用自定义手势屏蔽按钮解决方法/UITapGestureRecognizer屏蔽Button
- Tensorflow一些常用基本概念与函数(2)
- 关于Qt的Qml中支持注册的C++接口函数重载的问题
- 怎样快速将excel表格内容转换到word中
- 【jzoj4678】【钱仓】【平衡树】【avl】【队列】
- js闭包简要分析
- Spark Streaming Backpressure分析
- CocoaPods 安装 使用(-)
- 数据结构图和图的基本操作
- linux 小记
- UVAlive 5135 Mining Your Own Business [点双连通分量] [求割顶]
- POJ 1573 Robot Motion(BFS)
- postfix 邮件发送