膜这场比赛的 \(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的更多相关文章

  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. http轮询,长轮询

    轮询,长轮询 轮询 轮询:客户端定时向服务器发送Ajax请求,服务器接到请求后马上返回响应信息并关闭连接. 优点:后端程序编写比较容易. 缺点:请求中有大半是无用,浪费带宽和服务器资源. 实例:适于小 ...

  2. ios---scrollview用法总结

    一.使用步骤: 1.添加子组件到scrollview //必要步骤 2.设置clipsToBounds来确定超出范围是否被剪裁 (默认yes) self.scrolltest.clipsToBound ...

  3. kali-2019.4中文乱码问题的解决

    1.安装完kali-2019.4版出现乱码问题 2.更新源,用vi编辑器,在/etc/apt/resources.list中添加清华源 #清华大学 [更新源]deb https://mirrors.t ...

  4. CSS学习笔记--Div+Css布局实战(入门)

    基本页面布局 本教程带着大家做一个简单的页面布局 最重效果如下: 1.第一部,先创建上下左右4个DIV <!DOCTYPE html> <html> <head lang ...

  5. JSP-导入taglib 出现classNotFound异常

    案例 前端登录跳转到指定jsp,报classNoFoundException,原因是页面导入 <%@ taglib uri="http://java.sun.com/jsp/jstl/ ...

  6. Arduino系列之光照传感器(三)

    今天,我将简单做一个当光照值低于某个值的时候,灯光自动打开,当高于某个值的时候,自动关闭. 设计代码原理: 首先,定义一个全局变量,并赋予初始值 然后,初始化程序 将设定某个IO口为输出模式 读取光度 ...

  7. 时序数据库 Apache-IoTDB 源码解析之系统架构(二)

    上一章聊到时序数据是什么样,物联网行业中的时序数据的特点:存量数据大.新增数据多(采集频率高.设备量多).详情请见: 时序数据库 Apache-IoTDB 源码解析之前言(一) 打一波广告,欢迎大家访 ...

  8. css- :before :after

    :before和:after的作用就是在指定的元素内容(而不是元素本身)之前或者之后插入一个包含content属性指定内容的行内元素,最基本的用法如下: #example:before { conte ...

  9. 快速理解YOLO目标检测

    YOLO(You Only Look Once)论文 近些年,R-CNN等基于深度学习目标检测方法,大大提高了检测精度和检测速度. 例如在Pascal VOC数据集上Faster R-CNN的mAP达 ...

  10. 机器学习总结-LR(对数几率回归)

    LR(对数几率回归) 函数为\(y=f(x)=\frac{1}{1+e^{-(w^{T}x+b)}}\). 由于输出的是概率值\(p(y=1|x)=\frac{e^{w^{T}x+b}}{1+e^{w ...