膜这场比赛的 \(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. java泛型梳理

    java泛型梳理 概述 泛型,即参数化类型,是在JDK1.5之后才开始引入的. 所谓参数化类型是指所操作的数据类型在定义时被定义为一个参数,然后在使用时传入具体的类型. 这种参数类型可以用在类,接口, ...

  2. C++快读模板

    C++的快速读入模板 inline int read() { ; char ch = getchar(); ') { if (ch == '-') flag = true; ch = getchar( ...

  3. mybatis从数据库中取数据且分组,返回分组数据

    mapper.xml文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PU ...

  4. 小程序redio 样式

    小程序 radio 不写样式大概是这样的 数据为测试数据. 为了增加用户体验我们一下 美化后大概是这样的 给他设定头部.左边间距 ,和自身最小宽度 上代码: wxml: <radio-group ...

  5. CSS绝对定位absolute详解

    转:https://www.jianshu.com/p/a3da5e27d22b     之前介绍过CSS浮动float详解,本篇介绍的绝对定位absolute和浮动float有部分相似性.如果能理解 ...

  6. Python学习框架(持续更新)

    1.数据类型 整型:整数,1.2.3...这种 浮点型:简单理解就是小数,1.23.3.141572653等等 字符型:“这是字符”,简单说就是我们说的话,都可以作为字符 布尔值:只有2种,true. ...

  7. c++ 类成员的初始化顺序

    class TestClass1 { public: TestClass1() { cout << "TestClass1()" << endl; } Te ...

  8. HTTP&HTTPS协议详解之HTTP篇

    一.HTTP简介 01.什么是HTTP HTTP(HyperText Transfer Protocol ,超文本传输协议),是一个基于请求与响应的,无状态的,应用层的协议,常基于TCP/IP协议传输 ...

  9. urllib 模块 - module urllib

    urllib 模块 - urllib module 获取 web 页面, html = urllib.request.urlopen("http://www.zzyzz.top/" ...

  10. Spark RDD基本概念、宽窄依赖、转换行为操作

    目录 RDD概述 RDD的内部代码 案例 小总结 转换.行动算子 宽.窄依赖 Reference 本文介绍一下rdd的基本属性概念.rdd的转换/行动操作.rdd的宽/窄依赖. RDD:Resilie ...