CF1311E Construct the Binary Tree
膜这场比赛的 \(rk1\)
\(\color{black}A\color{red}{lex\_Wei}\)
这题应该是这场比赛最难的题了
容易发现,二叉树的下一层不会超过这一层的 \(2\) 倍,所以我们先构造出来一颗尽量满的二叉树,然后慢慢向下调整,调整的方法是从最上面一个一个弄下来。
然后你慢慢调整的复杂度最多是 \(d\) ,复杂度 \(O(d)\)
#include <bits/stdc++.h>
using namespace std ;
const int maxn = 5e3 + 5;
int n , d , p[maxn] , fa[maxn] ;
vector < int > dep[maxn] ;
signed main() {
int t;
cin >> t ;
while(t --) {
memset(p , 0 , sizeof(p)) , memset(fa , 0 , sizeof(fa)) ;
cin >> n >> d ;
int sum = 0 , bs = 0 ;
for(int i = 1 ; i <= n ;) {
int rem = min(n - i + 1 , 1 << bs) ;
p[bs] = rem , sum += rem * bs;
i += rem , bs ++ ;
}
if(sum > d) {
puts("no") ;
continue ;
}
int pos = bs;
while(sum < d) {
int k = 0 ;
for(int i = 1 ; i <= n ; i ++)
if((p[i] - 1) * 2 >= p[i + 1] + 1) {
k = i ;
break ;
}
if(! k)
break ;
sum ++ , p[k] -- , p[k + 1] ++ ;
}
if(sum < d) {
puts("no") ;
continue ;
}
puts("yes") ;
int cnt = 1 ;
for(int i = 0 ; i <= n ; i ++)
dep[i].clear() ;
dep[0].push_back(1);
dep[0].push_back(1);
for(int i = 1 ; i <= n ; i ++) {
while(p[i] --) {
fa[++ cnt] = dep[i - 1].back() ;
dep[i].push_back(cnt) ;
dep[i].push_back(cnt) ;
dep[i - 1].pop_back() ;
}
}
for(int i = 2 ; i <= n ; i ++)
cout << fa[i] << ' ' ;
cout << '\n' ;
}
return 0 ;
}
CF1311E Construct the Binary Tree的更多相关文章
- [CF1311E] Construct the Binary Tree - 构造
Solution 预处理出 \(i\) 个点组成的二叉树的最大答案和最小答案 递归做,由于只需要构造一种方案,我们让左子树大小能小就小,因此每次从小到大枚举左子树的点数并检验,如果检验通过就选定之 现 ...
- codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)
ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...
- [Algorithm] Construct a Binary Tree and Binary Search
function createNode(value) { return { value, left: null, right: null }; } function BinaryTree(val) { ...
- 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 ...
- 详细讲解Codeforces Round #624 (Div. 3) E. Construct the Binary Tree(构造二叉树)
题意:给定节点数n和所有节点的深度总和d,问能否构造出这样的二叉树.能,则输出“YES”,并且输出n-1个节点的父节点(节点1为根节点). 题解:n个节点构成的二叉树中,完全(满)二叉树的深度总和最小 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- 前端 network
控制台 :https://blog.csdn.net/m0_37724356/article/details/79884006 原文链接:https://segmentfault.com/a/1190 ...
- Shell之作业控制
命令 含义 jobs 列出所有正在运行的作业 ^Z(Ctrl+z) 暂停作业 bg 启动被暂停的作业 fg 将后台作业调到前台 kill 向指定作业发送kill信号 nohup 忽略所有发送给子命令的 ...
- Hyper-V虚拟机设置外部网络访问
在Hyper-V管理器中新建一个虚拟交换机,类型为 内部 ,修改名称为 nat 在虚拟机的设置页面中,将网络适配器设置为新建的虚拟交换机 nat 打开win10->控制面板->网络和共享中 ...
- springIOC源码接口分析(十一):ConfigurableApplicationContext
一 实现接口 关系图: ConfigurableApplicationContext接口实现了三个接口,ApplicationContext, Lifecycle, Closeable, Applic ...
- Hexo Next 接入 google AdSense 广告
前言 个人网站 www.yanlongwang.net 已经运营近一年,每日的浏览量不断上升,现在维持在两位数,打算承接一点广告赚睡后收入,用来维持网站的日常运营,希望能覆盖网站的服务器和域名开销. ...
- tomcat 持久区溢出
知识点:堆内存设置(JVM堆内存)java的堆内存分为两块:permantspace(持久带) 和 heap spaceOOM1.年老代溢出,表现为:java.lang.OutOfMemoryErro ...
- 硬件原理系列之LED数码管(一)
LED数码管也叫数码显示器,由8段(7段,8多一位小数点)发光二极管组成,控制不同组合,就可以显示不同字符 dp示小数点,COM为公共端,根据连接方式的不同,分为共阴极和共阳极 工作原理:若选用共阴极 ...
- gRPC in ASP.NET Core 3.x -- Protocol Buffer(2)Go语言的例子(上)
上一篇文章(大约半年前写的):https://www.cnblogs.com/cgzl/p/11246324.html 建立Go项目 在GOPATH的src下面建立一个文件夹 protobuf-go, ...
- C++静态成员函数小结
类中的静态成员真是个让人爱恨交加的特性.我决定好好总结一下静态类成员的知识点,以便自己在以后面试中,在此类问题上不在被动. 静态类成员包括静态数据成员和静态函数成员两部分. 一 静态数据成员: ...
- 【5min+】 巨大的争议?C# 8 中的接口
系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...