PAT (Advanced Level) 1064. Complete Binary Search Tree (30)
因为是要构造完全二叉树,所以树的形状已经确定了。
因此只要递归确定每个节点是多少即可。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<stack>
#include<map>
#include<algorithm>
using namespace std; int a[+],ans[+];
int f[+],sz[+];
int n; void t(int id)
{
if(*id>n&&*id+>n)
{
f[id]=; sz[id]=; return;
} if(*id<=n) t(*id);
if(*id+<=n) t(*id+); f[id]=sz[*id];
sz[id]=sz[*id]+sz[*id+]+;
} void dfs(int id,int L,int R)
{
ans[id]=a[L+f[id]];
if(*id<=n) dfs(*id,L,L+f[id]-);
if(*id+<=n) dfs(*id+,L+f[id]+,R);
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a++n); memset(f,,sizeof f);
memset(sz,,sizeof sz);
t(); dfs(,,n); for(int i=;i<=n;i++)
{
printf("%d",ans[i]);
printf("%s",(i<n)?" ":"\n");
} return ;
}
PAT (Advanced Level) 1064. Complete Binary Search Tree (30)的更多相关文章
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT题库-1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 【PAT甲级】1064 Complete Binary Search Tree (30 分)
题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...
- 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise
题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...
- pat 甲级 1064. Complete Binary Search Tree (30)
1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)
1064 Complete Binary Search Tree (30 分) A Binary Search Tree (BST) is recursively defined as a bin ...
- PAT Advanced 1064 Complete Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
- 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT Advanced 1099 Build A Binary Search Tree (30) [⼆叉查找树BST]
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following proper ...
随机推荐
- Linux下的两个聊天命令的使用方法
一. write用法:write 用户名:敲回车后,自己和对方将会同时处于聊天的状态,但是被发起连接的人只能收到发起聊天请求的人的聊天内容,但是不能回复,如果想要回复的话必须先向对方发起连接,这样以来 ...
- maven 代理
切换到{maven.home}\conf (ie: maven 安装目录) 拷贝settings.xml到自己的.m2目录 编辑.m2/settings.xml的代理部分: <proxy> ...
- login/logout切换
1. 前端按钮 <img border="0" width="18" height="18" src="<%=base ...
- 编译安装SVN不结合apache
前言:独立SVN服务器不结合APACHE安装使用,连接独立SVN服务器也不是用HTTP协议.这是比较快捷的SVN服务器配置方法. 一.下载工具 1.subversion-1.6.17.tar.gz 2 ...
- Qt Creator下载和安装(详细教程)
简介 Qt是跨平台的图形开发库,目前由Digia全资子公司 Qt Company 独立运营,官方网址: http://www.qt.io/ 也可以访问Qt项目域名:http://qt-project. ...
- css 相关
background-size: auto就会变成就是不会让图像变形的,会自动调整,一般是会设置多少箱像素的, background-size: XXpx XXpx;百分百那就铺满整个区域了 back ...
- Struts2的运行机制简介
1.客户端通过URL请求tomcat 2.URL找到对应站点的WEB.xml 发现里面有 struts2配置 3.执行StrutsPrepareAndExecuteFilter类的init方法 4 ...
- MySQL 异常错误码使用 及 对照表
DECLARE CONTINUE HANDLER FOR SQLWARNING, SQLEXCEPTION, NOT FOUND BEGIN GET DIAGNOSTICS CONDITION cod ...
- css3的box-sizing
给了两个并排带边框的div百分比宽度,假如不用box-sizing,边框的宽度会在行内显示.用box-sizing:border-box,可以去除边框的占位. 浏览器支持IE9以上及火狐.谷歌.Ope ...
- Struts2 请求数据的自动封装 及 自定义转换器类
请求数据自动封装: 实现原理:使用了参数拦截器.struts-default.xml中 <interceptor name="params" class="com. ...