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 ...
随机推荐
- 自定义BeanDefinitionRegistryPostProcessor
自定义BeanDefinitionRegistryPostProcessor 概述 BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProce ...
- markdown时序图语法
语法 - 代表实线 , 主动发送消息,比如 request请求 > 代表实心箭头 , 同步消息,比如 AJAX 的同步请求 -- 代表虚线,表示返回消息,spring Controller re ...
- CSRF攻击原理
CSRF CSRF(Cross-site request forgery)跨站请求伪造,CSRF是一种夹持用户在已经登陆的web应用程序上执行非本意的操作的攻击方式.相比于XSS,CSRF是利用了系统 ...
- Linux文件和目录的属性及权限总结
本文讲述的是文件或目录的属性及权限,比如索引节点inode.文件类型.文件权限及属主:还对setuid.setgid及粘贴位进行了相关的讲解.其中,对ln.chmod.chown.chgrp.umas ...
- JSP&Servlet学习笔记----第4章
HTTP是基于请求/响应的无状态的通信协议. 使服务器记得此次请求与之后请求关系的方式,叫做会话管理. 隐藏域:由浏览器在每次请求时主动告知服务器多次请求间必要的信息.仅适用于一些简单的状态 管理,如 ...
- 【Qt学习笔记】Qt+VS2010的配置
http://blog.csdn.net/jocyln9026/article/details/8575218 关于Qt Qt是1991年由Trolltech公司开发的一个跨平台的C++图形用户界面应 ...
- JavaScript 初学者容易犯的几个错误,你中招没?
JavaScript 是对初学者比较友好的一门编程语言,基本上花个半小时看下语法就能写出能运行的代码.JavaScript 是动态脚本语言,对数据类型没有太多的限制,写起来非常灵活.但正因为如此,初学 ...
- Android Studio MainActivity中的R为红色
csdn解决链接 https://blog.csdn.net/M283592338/article/details/79880413
- 《Python学习手册 第五版》 -第6章 动态类型
本章主要讲述变量.对象.引用三者直接的关联及区别,详细说明了在变量赋值的操作中,计算机内部到底发生了什么,有哪些是不被人察觉和需要明确了解的 1.先从最简单的赋值语句开始 a=3 这一句,基本就能涵盖 ...
- Linux 内存分析工具——free命令
在Linux系统经常被用作服务器系统.当服务器内存吃紧的时候,free命令是我们最常使用的内存分析工具. free使用介绍 free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及 ...