因为是要构造完全二叉树,所以树的形状已经确定了。

因此只要递归确定每个节点是多少即可。

#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)的更多相关文章

  1. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  2. PAT题库-1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  3. 【PAT甲级】1064 Complete Binary Search Tree (30 分)

    题意:输入一个正整数N(<=1000),接着输入N个非负整数(<=2000),输出完全二叉树的层次遍历. AAAAAccepted code: #define HAVE_STRUCT_TI ...

  4. 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 ...

  5. pat 甲级 1064. Complete Binary Search Tree (30)

    1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  6. PAT 甲级 1064 Complete Binary Search Tree (30 分)(不会做,重点复习,模拟中序遍历)

    1064 Complete Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a bin ...

  7. 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 ...

  8. 1064 Complete Binary Search Tree (30分)(已知中序输出层序遍历)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  9. 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 ...

随机推荐

  1. 下载google play上的APP

    googol搜索  download apk directly,然后即可看到

  2. hdu_1429_胜利大逃亡(续)(BFS状压)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 题意:迷宫的加强版,迷宫里有钥匙和门,问在指定的时间下能否逃出 题解:用二进制位来记录是否有该门 ...

  3. 在 Visual Studio 2010 中创建 SharePoint 2010 事件接收器

    Microsoft Visual Studio 2010 提供了一个可用于生成事件接收器的项目类型,事件接收器会在 Microsoft SharePoint 2010 网站上选择事件之前或之后执行操作 ...

  4. 我的C笔记

    最近更新: 1,父进程fork一个子进程,当向父进程发送一个SIGINT或其它信号时,子进程是否会接受到该信号? 2,父进程调用system执行一个程序时,向父进程发送一个信号时,system运行中的 ...

  5. 关于stringWithFormat: - 两段NSString如何合成一段

    http://blog.sina.com.cn/s/blog_6b1e4a0601019pib.html str = [NSString stringWithFormat:@"%@,%@&q ...

  6. process lasso 优化原理

    <星际争霸2:虚空之遗>很多玩家的CPU性能并不低,但是在星际2中的表现就总会出现掉帧的情况,那么应该如何提升CPU的性能就成了玩家关注的话题,下面小编就为大家带来星际争霸2虚空之遗cpu ...

  7. Mongoose如何实现统计查询、关联查询

    [问题]Mongoose如何实现统计查询.关联查询  发布于 4 年前  作者 a272121742  13025 次浏览 最近业务上提出一个需求,要求能做统计,我们设计的文档集,统计可能跨越的文档会 ...

  8. Entity Framework 学习中级篇2—存储过程(上)

    目前,EF对存储过程的支持并不完善.存在以下问题: l         EF不支持存储过程返回多表联合查询的结果集. l         EF仅支持返回返回某个表的全部字段,以便转换成对应的实体.无法 ...

  9. Zabbix3.0 客户端搭建

    zabbix客户端安装 我们这边使用编译安装 软件包版本 zabbix-3.0.3.tar.gz 添加用户组 #groupadd zabbix #useradd -s /sbin/nologin -g ...

  10. linux 查看磁盘、文件夹、文件大小(df du)

    du 查看文件夹大小 1.查看当前文件夹中所有文件夹及其子文件夹的大小,注意是文件夹大小,不是文件 # du -h -rw-r--r-- 1 root root 82785865 6月 9 15:53 ...