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 ... 
随机推荐
- Java框架-MyBatis三剑客之MyBatis Generator(mybatis-generator MBG插件)详解
			生成器设计思路: 连接数据库 -> 获取表结构 -> 生成文件 1 下载与安装 官网文档入口 最方便的 maven 插件使用方式 贴至pom 文件 2 新建配置文件 填充配置信息(官网示例 ... 
- Java 分布式框架面试题合集
			Java 分布式框架面试题合集 1.什么是 ZooKeeper? 答:ZooKeeper 是一个开源的分布式应用程序协调服务,是一个典型的分布式数据一致性解决方案.设计目的是将那些复杂且容易出错的分布 ... 
- Spring初识、新建工程
			1.spring与三层架构的关系: spring负责管理项目中的所有对象,是一个一站式的框架,容器中的对象决定了spring的功能. 2.spring核心架构 Spring框架主要由六个模块组成,在开 ... 
- centos6.8安装lnmp
			一.配置CentOS 第三方yum源(CentOS默认的标准源里没有nginx软件包) [root@localhost ~]# yum install wget #安装下载工具wget[root@lo ... 
- 编译出适合自己的nginx
			上面是解压后的nginx源码 auto目录 上面的cc目录用于编译,lib库 os目录对系统进行判断,其他所有文件都是辅助conf脚本执行 判定nginx支持哪些模块,当前操作系统有哪些特性. CHA ... 
- 《Android Studio实战 快速、高效地构建Android应用》--二、在Android Studio中编程
			代码折叠 Ctrl+数字加号展开光标处已折叠代码块 Ctrl+数字减号折叠光标处已展开代码块 Ctrl+Shift+数字加号展开窗口中全部代码 Ctrl+Shift+数字减号折叠窗口中全部代码 注释代 ... 
- Luinx安装RocketMQ
			一.RocketMQ环境 准备两台虚拟机,分别为master01 和master02 二.安装JDK(两台虚拟机相同步骤) 1. 检查当前虚拟机环境有没有JDK rpm -qa|grep java ( ... 
- Altium Designer打印多块PCB到热转印纸上、拼板发给工厂
			接下来介绍的方法的优势有: 节省电脑性能(来自不同PCB文件的图案被放置时只在第1块会卡顿一小会儿,之后不再卡顿) 便于排版(拖放图案时绝不会改变图案内容,拖放图案时鼠标拖住的矩形框的尺寸与图案的尺寸 ... 
- 我的一个配置redux(实现一次存储与调用方法)之旅
			前言 : 今天呢,就配置一下redux,redux的重要性呢,就叭叭叭一大堆,什么也没有带着配置一次来的重要,因为许多涉及到的属性和方法,用法是活的,但格式是需要记忆的. 过程中不要嫌我唠叨,有的地方 ... 
- Golang设置https访问,以及http如何重定向到https
			设置https访问: 原始代码为http监听: func main() { server := &http.Server{ Addr: ":8080", ... } go ... 
