膜这场比赛的 \(rk1\)

\(\color{black}A\color{red}{lex\_Wei}\)

这题应该是这场比赛最难的题了

容易发现,二叉树的下一层不会超过这一层的 \(2\) 倍,所以我们先构造出来一颗尽量满的二叉树,然后慢慢向下调整,调整的方法是从最上面一个一个弄下来。

然后你慢慢调整的复杂度最多是 \(d\) ,复杂度 \(O(d)\)

  1. #include <bits/stdc++.h>
  2. using namespace std ;
  3. const int maxn = 5e3 + 5;
  4. int n , d , p[maxn] , fa[maxn] ;
  5. vector < int > dep[maxn] ;
  6. signed main() {
  7. int t;
  8. cin >> t ;
  9. while(t --) {
  10. memset(p , 0 , sizeof(p)) , memset(fa , 0 , sizeof(fa)) ;
  11. cin >> n >> d ;
  12. int sum = 0 , bs = 0 ;
  13. for(int i = 1 ; i <= n ;) {
  14. int rem = min(n - i + 1 , 1 << bs) ;
  15. p[bs] = rem , sum += rem * bs;
  16. i += rem , bs ++ ;
  17. }
  18. if(sum > d) {
  19. puts("no") ;
  20. continue ;
  21. }
  22. int pos = bs;
  23. while(sum < d) {
  24. int k = 0 ;
  25. for(int i = 1 ; i <= n ; i ++)
  26. if((p[i] - 1) * 2 >= p[i + 1] + 1) {
  27. k = i ;
  28. break ;
  29. }
  30. if(! k)
  31. break ;
  32. sum ++ , p[k] -- , p[k + 1] ++ ;
  33. }
  34. if(sum < d) {
  35. puts("no") ;
  36. continue ;
  37. }
  38. puts("yes") ;
  39. int cnt = 1 ;
  40. for(int i = 0 ; i <= n ; i ++)
  41. dep[i].clear() ;
  42. dep[0].push_back(1);
  43. dep[0].push_back(1);
  44. for(int i = 1 ; i <= n ; i ++) {
  45. while(p[i] --) {
  46. fa[++ cnt] = dep[i - 1].back() ;
  47. dep[i].push_back(cnt) ;
  48. dep[i].push_back(cnt) ;
  49. dep[i - 1].pop_back() ;
  50. }
  51. }
  52. for(int i = 2 ; i <= n ; i ++)
  53. cout << fa[i] << ' ' ;
  54. cout << '\n' ;
  55. }
  56. return 0 ;
  57. }

CF1311E Construct the Binary Tree的更多相关文章

  1. [CF1311E] Construct the Binary Tree - 构造

    Solution 预处理出 \(i\) 个点组成的二叉树的最大答案和最小答案 递归做,由于只需要构造一种方案,我们让左子树大小能小就小,因此每次从小到大枚举左子树的点数并检验,如果检验通过就选定之 现 ...

  2. codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)

    ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...

  3. [Algorithm] Construct a Binary Tree and Binary Search

    function createNode(value) { return { value, left: null, right: null }; } function BinaryTree(val) { ...

  4. Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals

    http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-travers ...

  5. 详细讲解Codeforces Round #624 (Div. 3) E. Construct the Binary Tree(构造二叉树)

    题意:给定节点数n和所有节点的深度总和d,问能否构造出这样的二叉树.能,则输出“YES”,并且输出n-1个节点的父节点(节点1为根节点). 题解:n个节点构成的二叉树中,完全(满)二叉树的深度总和最小 ...

  6. [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  7. [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  8. Leetcode Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  9. LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. 前端 network

    控制台 :https://blog.csdn.net/m0_37724356/article/details/79884006 原文链接:https://segmentfault.com/a/1190 ...

  2. Shell之作业控制

    命令 含义 jobs 列出所有正在运行的作业 ^Z(Ctrl+z) 暂停作业 bg 启动被暂停的作业 fg 将后台作业调到前台 kill 向指定作业发送kill信号 nohup 忽略所有发送给子命令的 ...

  3. Hyper-V虚拟机设置外部网络访问

    在Hyper-V管理器中新建一个虚拟交换机,类型为 内部 ,修改名称为 nat 在虚拟机的设置页面中,将网络适配器设置为新建的虚拟交换机 nat 打开win10->控制面板->网络和共享中 ...

  4. springIOC源码接口分析(十一):ConfigurableApplicationContext

    一 实现接口 关系图: ConfigurableApplicationContext接口实现了三个接口,ApplicationContext, Lifecycle, Closeable, Applic ...

  5. Hexo Next 接入 google AdSense 广告

    前言 个人网站 www.yanlongwang.net 已经运营近一年,每日的浏览量不断上升,现在维持在两位数,打算承接一点广告赚睡后收入,用来维持网站的日常运营,希望能覆盖网站的服务器和域名开销. ...

  6. tomcat 持久区溢出

    知识点:堆内存设置(JVM堆内存)java的堆内存分为两块:permantspace(持久带) 和 heap spaceOOM1.年老代溢出,表现为:java.lang.OutOfMemoryErro ...

  7. 硬件原理系列之LED数码管(一)

    LED数码管也叫数码显示器,由8段(7段,8多一位小数点)发光二极管组成,控制不同组合,就可以显示不同字符 dp示小数点,COM为公共端,根据连接方式的不同,分为共阴极和共阳极 工作原理:若选用共阴极 ...

  8. gRPC in ASP.NET Core 3.x -- Protocol Buffer(2)Go语言的例子(上)

    上一篇文章(大约半年前写的):https://www.cnblogs.com/cgzl/p/11246324.html 建立Go项目 在GOPATH的src下面建立一个文件夹 protobuf-go, ...

  9. C++静态成员函数小结

    类中的静态成员真是个让人爱恨交加的特性.我决定好好总结一下静态类成员的知识点,以便自己在以后面试中,在此类问题上不在被动.  静态类成员包括静态数据成员和静态函数成员两部分.  一 静态数据成员:  ...

  10. 【5min+】 巨大的争议?C# 8 中的接口

    系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...