洛谷 P2171 Hz吐泡泡
2024-08-29 00:47:32
题目背景
Hz大大是一种可爱的动物(神)。他很喜欢吐泡泡(更喜欢写作业)。
题目描述
这天,Hz大大心血来潮,吐了n个不同的泡泡玩(保证没有重复的泡泡)。因为他还要写作业,所以他请你帮他把这些泡泡排序成树(左子树<=根<右子树)。输出它的后序遍历。
输入输出格式
输入格式:
共2行。
第一行,1个整数n。(1<=n<=300000)
第二行,n个数,代表泡泡的大小。
输出格式:
共2行。
第一行,输出树的深度。
第二行,输出数的后序遍历。
详见样例输出。
输入输出样例
说明
水题一道。
思路:模拟堆
#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);
}
最新文章
- move 和 CopyMemory的区别
- qml android 的一个例子qtHangMan
- SPI 四种模式
- Cocos2d-android游戏引擎-介绍
- iOS Storyboard适配问题
- 从理解开始 谈谈px rem 和 em 的区别与联系
- 超大文本文件浏览器Snaptext,支持不限制大小的文本文件浏览
- Vs2017 typescript 开发小问题
- Gson解析空字符串异常的处理
- scipy 安装错误及解决
- yii2 阿里云短信 aliyun-dysms
- HELLO JAVA!
- Linux服务器密钥安全登录
- http协议基础(三)几种数据传输方式
- Unable to load the Wrapper&#39;s native library because none of the following files及解决方法
- eclipse创建Maven工程没有Maven Dependencies
- iphone手机微信端html5 Geolocation定位失效的问题
- [GO]通道的关闭
- MySQL索引基础知识点
- postgresql 的一些操作
热门文章
- bzoj1296: [SCOI2009]粉刷匠(DP)
- legend---八、php对象如何转换成js对象
- canvas指定的宽高写在行间和写在style里面的区别?
- 《剑指offer》二叉树的镜像
- js控制分页打印、打印分页示例
- HDU I Hate It(线段树单节点更新,求区间最值)
- ipad mini2 升级9.0.2后解锁白屏解决
- [转载][来自csdn]RTS和CTS是什么意思?
- 【Round #36 (Div. 2 only) C】Socks Pairs
- 洛谷 P1542 包裹快递