zoukankan      html  css  js  c++  java
  • BZOJ1078 [SCOI2008]斜堆 堆

    欢迎访问~原文出处——博客园-zhouzhendong

    去博客园看该题解


    题目传送门 - BZOJ1078


    题意概括

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


    题解

      看看大佬怎么写————>http://www.cppblog.com/MatoNo1/archive/2013/03/03/192131.html


    代码

    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    using namespace std;
    const int N=105;
    int n,root,fa[N],son[N][2],ans[N];
    int main(){
    	scanf("%d",&n);
    	memset(son,-1,sizeof son);
    	memset(fa,-1,sizeof fa);
    	for (int i=1,a;i<=n;i++){
    		scanf("%d",&a);
    		if (a<100)
    			fa[i]=a,son[a][0]=i;
    		else
    			fa[i]=a-100,son[a-100][1]=i;
    	}
    	root=0;
    	for (int i=n;i>=0;i--){
    		int now=root;
    		while (son[now][1]!=-1)
    			now=son[now][0];
    		int s=son[now][0];
    		if (s!=-1&&son[s][0]==-1&&son[s][1]==-1)
    			now=s;
    		ans[i]=now;
    		if (now==root){
    			root=son[now][0],fa[root]=-1;
    			continue;
    		}
    		s=son[now][0];
    		if (s!=-1)
    			fa[s]=fa[now];
    		son[fa[now]][0]=s;
    		for (s=fa[now];s!=-1;s=fa[s])
    			swap(son[s][0],son[s][1]);
    		fa[now]=son[now][0]=son[now][1]=-1;
    	}
    	for (int i=0;i<=n;i++)
    		printf("%d ",ans[i]);
    	return 0;
    } 
    

      

  • 相关阅读:
    QT调用其他UI并使用QLabel(text)
    QT调用单例模式脚本
    QT 调用另一个UI实现方式
    QT 键值
    (一) Mybatis 源码解析之源码概述
    设计模式之 模板模式开发
    十二、线程池
    (十一)并发容器ConcurrentHashMap
    mybatis plus 踩坑记 -- 自动填充
    C/C++ file
  • 原文地址:https://www.cnblogs.com/zhouzhendong/p/BZOJ1078.html
Copyright © 2011-2022 走看看