题目背景

Hz大大是一种可爱的动物(神)。他很喜欢吐泡泡(更喜欢写作业)。

题目描述

这天,Hz大大心血来潮,吐了n个不同的泡泡玩(保证没有重复的泡泡)。因为他还要写作业,所以他请你帮他把这些泡泡排序成树(左子树<=根<右子树)。输出它的后序遍历。

输入输出格式

输入格式:

共2行。

第一行,1个整数n。(1<=n<=300000)

第二行,n个数,代表泡泡的大小。

输出格式:

共2行。

第一行,输出树的深度。

第二行,输出数的后序遍历。

详见样例输出。

输入输出样例

输入样例#1: 复制

8
1 4 3 9 10 35 2 7
输出样例#1: 复制

deep=5
2
3
7
35
10
9
4
1

说明

水题一道。

思路:模拟堆

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
int n,cnt,deep,bns,root;
struct data{
int ls,rs,val;
}tr[];
int max(int a,int b){
if(a<b) return b;
else return a;
}
void insert(int& rt,int x){
++bns;
if(!rt){ rt=++cnt;tr[rt].val=x;deep=max(deep,bns);return; }
if(x>tr[rt].val) insert(tr[rt].rs,x);
else insert(tr[rt].ls,x);
return;
}
void dfs(int rt){
if(tr[rt].ls) dfs(tr[rt].ls);
if(tr[rt].rs) dfs(tr[rt].rs);
printf("%d\n",tr[rt].val);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
bns=;int x;
scanf("%d",&x);
insert(root,x);
}
printf("deep=%d\n",deep);
dfs(root);
}

最新文章

  1. move 和 CopyMemory的区别
  2. qml android 的一个例子qtHangMan
  3. SPI 四种模式
  4. Cocos2d-android游戏引擎-介绍
  5. iOS Storyboard适配问题
  6. 从理解开始 谈谈px rem 和 em 的区别与联系
  7. 超大文本文件浏览器Snaptext,支持不限制大小的文本文件浏览
  8. Vs2017 typescript 开发小问题
  9. Gson解析空字符串异常的处理
  10. scipy 安装错误及解决
  11. yii2 阿里云短信 aliyun-dysms
  12. HELLO JAVA!
  13. Linux服务器密钥安全登录
  14. http协议基础(三)几种数据传输方式
  15. Unable to load the Wrapper&#39;s native library because none of the following files及解决方法
  16. eclipse创建Maven工程没有Maven Dependencies
  17. iphone手机微信端html5 Geolocation定位失效的问题
  18. [GO]通道的关闭
  19. MySQL索引基础知识点
  20. postgresql 的一些操作

热门文章

  1. bzoj1296: [SCOI2009]粉刷匠(DP)
  2. legend---八、php对象如何转换成js对象
  3. canvas指定的宽高写在行间和写在style里面的区别?
  4. 《剑指offer》二叉树的镜像
  5. js控制分页打印、打印分页示例
  6. HDU I Hate It(线段树单节点更新,求区间最值)
  7. ipad mini2 升级9.0.2后解锁白屏解决
  8. [转载][来自csdn]RTS和CTS是什么意思?
  9. 【Round #36 (Div. 2 only) C】Socks Pairs
  10. 洛谷 P1542 包裹快递