zoukankan      html  css  js  c++  java
  • BZOJ1078 斜堆

    http://hzwer.com/5790.html  代码

    http://www.cppblog.com/MatoNo1/archive/2013/03/03/192131.html  //原理讲解  

    斜堆(skew heap)是一种常用的数据结构。它也是二叉树,且满足与二叉堆相同的堆性质:每个非根结点的值
    都比它父亲大。因此在整棵斜堆中,根的值最小。但斜堆不必是平衡的,每个结点的左右儿子的大小关系也没有任
    何规定。在本题中,斜堆中各个元素的值均不相同。 在斜堆H中插入新元素X的过程是递归进行的:当H为空或者X
    小于H的根结点时X变为新的树根,而原来的树根(如果有的话)变为X的左儿子。当X大于H的根结点时,H根结点的
    两棵子树交换,而X(递归)插入到交换后的左子树中。 给出一棵斜堆,包含值为0~n的结点各一次。求一个结点
    序列,使得该斜堆可以通过在空树中依次插入这些结点得到。如果答案不惟一,输出字典序最小的解。输入保证有
    解。

    Input

      第一行包含一个整数n。第二行包含n个整数d1, d2, ... , dn, di < 100表示i是di的左儿子,di>=100表示i
    是di-100的右儿子。显然0总是根,所以输入中不含d0。

    Output

      仅一行,包含n+1整数,即字典序最小的插入序列。

    Sample Input

    6
    100 0 101 102 1 2

    Sample Output

    0 1 2 3 4 5 6

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n,top,root;
    int ls[105],rs[105],fa[105],ans[105];
    void solve(){
        int x=root;
        while(rs[x]!=-1) x=ls[x];
        int t=ls[x];
        if(t!=-1&&ls[t]==-1&&rs[t]==-1) x=t;
        ans[++top]=x;
        if(x==root) root=ls[root];
        int f=fa[x];
        if(f!=-1) ls[f]=ls[x],fa[ls[f]]=f;
        while(f!=-1) swap(ls[f],rs[f]),f=fa[f];
    }
    int main(){
        fa[0]=-1;
        memset(ls,-1,sizeof(ls));
        memset(rs,-1,sizeof(rs));
        scanf("%d",&n);
        for(int i=1;i<=n;++i) {
            int x;scanf("%d",&x);
            if(x<100) ls[x]=i,fa[i]=x;
            else rs[x-100]=i,fa[i]=x-100;
        }
        for(int i=1;i<=n+1;++i)
            solve();
        while(top)  printf("%d ",ans[top--]);
    }

  • 相关阅读:
    unity assert server 与 cache server
    Excel文件读写
    String与StringBuilder之间区别(转)
    c# 文件遍历
    C#整数三种强制类型转换int、Convert.ToInt32()、int.Parse()的区别
    2014年读过的书总结
    求职在年末
    被辞退于年末
    Unity Svn(转)
    公司的人员流动
  • 原文地址:https://www.cnblogs.com/mfys/p/7144866.html
Copyright © 2011-2022 走看看