2016-05-31 16:34:09
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1078
挖掘斜堆的性质233 http://www.cppblog.com/MatoNo1/archive/2013/03/03/192131.html
首先它不如左偏树稳定,是均摊logn,而左偏树是严格的
最后加入的点有两个性质
1.一定是极左的点
2.它没有右子树,要么是叶子节点,要么原树的某一部分子树变为它的左儿子。
可以得出结论每一个非叶节点都会有左子树
假设某一个节点x(不为叶子节点)符合条件,在其祖先中也有一个节点y符合条件.
因为在插入过程中要交换左右两棵子树,则证明插入前的原树中y节点只有右子树而没有左子树,这与结论矛盾。
故最后插入的点一定是符合条件的最浅深度的点,当它有一个叶子节点时,为了满足字典序最小,最后插入的是它的左儿子。
1 #include<bits/stdc++.h> 2 #define inf 1000000000 3 #define ll long long 4 using namespace std; 5 int read(){ 6 int x=0,f=1;char ch=getchar(); 7 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 8 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 9 return x*f; 10 } 11 int n,top,root,ls[105],rs[105],fa[105],ans[105]; 12 void solve(){ 13 int x=root; 14 while(rs[x]!=-1)x=ls[x]; 15 int t=ls[x]; 16 if(t!=-1&&ls[t]==-1&&rs[t]==-1)x=t; 17 ans[++top]=x; 18 if(x==root)root=ls[x]; 19 int f=fa[x]; 20 if(f!=-1)ls[f]=ls[x],fa[ls[x]]=f; 21 while(f!=-1)swap(ls[f],rs[f]),f=fa[f]; 22 } 23 int main(){ 24 n=read(); 25 memset(ls,-1,sizeof(ls)); 26 memset(rs,-1,sizeof(rs)); 27 fa[0]=-1; 28 for(int i=1;i<=n;i++){ 29 int x=read(); 30 if(x<100)ls[x]=i,fa[i]=x; 31 else rs[x-100]=i,fa[i]=x-100; 32 } 33 for(int i=1;i<=n+1;i++)solve(); 34 while(top)printf("%d ",ans[top--]); 35 return 0; 36 }