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 ...
随机推荐
- ASP.NET MVC 使用TempData
ASP.NET MVC的TempData用于传输一些临时的数据,例如在各个控制器Action间传递临时的数据或者给View传递一些临时的数据,相信大家都看过"在ASP.NET页面间传值的方法 ...
- ASP.NET MVC View向Controller提交数据
我们知道使用MVC的一个很重的的用途就是把Controller和View之间进行解耦,通过控制器来调用不同的视图,这就注定了Controller和View之间的传值是一个很重的知识点,这篇博文主要解释 ...
- OpenCV ——双线性插值(Bilinear interpolation)
1,原理 在图像的仿射变换中,很多地方需要用到插值运算,常见的插值运算包括最邻近插值,双线性插值,双三次插值,兰索思插值等方法,OpenCV提供了很多方法,其中,双线性插值由于折中的插值效果和运算速度 ...
- Hadoop概论
1.Hadoop核心项目:HDFS(分布式文件系统)和MapReduce(并行计算框架) 2.HDFS的架构 主从结构 主节点,只有一个:namenode(接受用户操作要求:维护文件系统的目录结构:管 ...
- ios控件 UILabel
UILabel 的作用是显示文本 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 40)]; lab ...
- 宏定义重写NSLog
只需要 在ProjectName_Prefix.pch 中追加你对应的宏定义,不用import 就可以直接使用了. #define DEBUG //宏输出函数 #ifdef DEBUG #define ...
- 为PO手写添加配置文件(hbm.xml)
- ListView控件的Insert、Edit和Delete功能(第二部分)
本系列文章将通过一个简单的实例,结合我自己使用ListView的情况,展示如何用ASP.NET 3.5 ListView控件进行基本的Insert.Edit和Delete操作. 系统要求: Windo ...
- iOS \'The sandbox is not sync with the Podfile.lock\'问题解决
iOS \'The sandbox is not sync with the Podfile.lock\'问题解决 HUANGDI 发表于 2015-02-27 09:51:13 问题描述: gith ...
- 自定义switch开关
自定义一个switch开关 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...