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

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

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cmath>
  4. #include<queue>
  5. #include<vector>
  6. #include<string>
  7. #include<stack>
  8. #include<map>
  9. #include<algorithm>
  10. using namespace std;
  11.  
  12. int a[+],ans[+];
  13. int f[+],sz[+];
  14. int n;
  15.  
  16. void t(int id)
  17. {
  18. if(*id>n&&*id+>n)
  19. {
  20. f[id]=; sz[id]=; return;
  21. }
  22.  
  23. if(*id<=n) t(*id);
  24. if(*id+<=n) t(*id+);
  25.  
  26. f[id]=sz[*id];
  27. sz[id]=sz[*id]+sz[*id+]+;
  28. }
  29.  
  30. void dfs(int id,int L,int R)
  31. {
  32. ans[id]=a[L+f[id]];
  33. if(*id<=n) dfs(*id,L,L+f[id]-);
  34. if(*id+<=n) dfs(*id+,L+f[id]+,R);
  35. }
  36.  
  37. int main()
  38. {
  39. scanf("%d",&n);
  40. for(int i=;i<=n;i++) scanf("%d",&a[i]);
  41. sort(a+,a++n);
  42.  
  43. memset(f,,sizeof f);
  44. memset(sz,,sizeof sz);
  45. t();
  46.  
  47. dfs(,,n);
  48.  
  49. for(int i=;i<=n;i++)
  50. {
  51. printf("%d",ans[i]);
  52. printf("%s",(i<n)?" ":"\n");
  53. }
  54.  
  55. return ;
  56. }

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. Sikuli:创新的图形化编程技术

    Sikuli是一种使用截图进行UI自动化测试的技术.Sikuli包括sikul脚本,基于Jython的API以及sikuli IDE.Sikuli可以实现任何你可以在显示器上看到ui对象的自动化,你可 ...

  2. wmic应用实例

    实例应用 1.磁盘管理 查看磁盘的属性 wmic logicaldisk list brief ::caption=标题.driveID=驱动器ID号.model=产品型号.Partitions=分区 ...

  3. JavaScript高级程序设计:第七章

    函数表达式 1.函数表达式的特征: 定义函数的方式有两种:一种是函数声明,另一种就是函数表达式.函数声明的语法是这样的: function  functionName(arg0,arg1,arg2){ ...

  4. hdu_5793_A Boring Question(打表找规律)

    题目链接:hdu_5793_A Boring Question 题意: 自己看吧,说不清楚了. 题解: 打表找规律 #include<cstdio> typedef long long l ...

  5. hdu_5727_Necklace(二分匹配)

    题目连接:hdu_5727_Necklace 题意: 有2*n个珠子,n个阳珠子,n个阴珠子,现在要将这2n个珠子做成一个项链,珠子只能阴阳交替排,有些阳珠子周围如果放了指定的阴珠子就会变坏,给你一个 ...

  6. PAXOS may not terminate

    It’s easy to see that Paxos does have a failure mode. When two proposers are active at the same time ...

  7. OC画笔CGContextRef

    1.画线 CGContextRef context = UIGraphicsGetCurrentContext();//context相当于画布 CGContextSetStrokeColorWith ...

  8. 数据库NULL和 ‘’ 区别

    NULL判断时 : IS NOT NULL ''判断时: !=''

  9. MFC下对串口的操作以及定时器的调用

    最近研究了一下MFC下对串口的操作,测试了一下对设备的读写. 1.打开串口 GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(FALSE); m_hComm = ...

  10. javascript string去除两边空格

    function trim(){ return this.replace(/(^\s*)|(\s*$)/g,""); g整个字符串 }