Solution

预处理出 \(i\) 个点组成的二叉树的最大答案和最小答案

递归做,由于只需要构造一种方案,我们让左子树大小能小就小,因此每次从小到大枚举左子树的点数并检验,如果检验通过就选定之

现在还需要确定左右子树各分配多少答案上去,一种构造性的想法是,要么让左子树选最小,要么让右子树选最大。对于任意一种其它方案,我们可以通过把左子树上的答案不断移到右子树上,直到某一边达到界限,故等价。

打错变量名查半天……

#include <bits/stdc++.h>
using namespace std; #define int long long
const int N = 5005; int T,n,d,mx[N],mn[N],f[N]; void dfs(int tot,int off,int sum) {
for(int i=0;i<tot;i++) {
int lsiz=i, rsiz=tot-1-i;
int vmin=mn[lsiz]+mn[rsiz]+tot-1;
int vmax=mx[lsiz]+mx[rsiz]+tot-1;
if(vmin<=sum && sum<=vmax) {
int tmp=mn[lsiz];
if(mn[lsiz]+mx[tot-1-lsiz]+tot-1<sum) tmp=sum-tot+1-mx[tot-1-lsiz];
if(lsiz) {
f[off+1]=off;
dfs(lsiz,off+1,tmp);
}
if(rsiz) {
f[off+1+lsiz]=off;
dfs(rsiz,off+1+lsiz,sum-tot+1-tmp);
}
return;
}
}
} signed main() {
ios::sync_with_stdio(false);
for(int i=1;i<=5000;i++) {
mx[i]=i*(i-1)/2;
mn[i]=mn[i-1]+(int)(log2(i)+1e-7);
} cin>>T;
while(T--) {
cin>>n>>d;
if(d<mn[n] || d>mx[n]) cout<<"NO"<<endl;
else {
cout<<"YES"<<endl;
dfs(n,1,d);
for(int i=2;i<=n;i++) cout<<f[i]<<" ";
cout<<endl;
}
}
}

[CF1311E] Construct the Binary Tree - 构造的更多相关文章

  1. CF1311E Construct the Binary Tree

    膜这场比赛的 \(rk1\) \(\color{black}A\color{red}{lex\_Wei}\) 这题应该是这场比赛最难的题了 容易发现,二叉树的下一层不会超过这一层的 \(2\) 倍,所 ...

  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. HDU 5573 Binary Tree 构造

    Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...

  4. [Algorithm] Construct a Binary Tree and Binary Search

    function createNode(value) { return { value, left: null, right: null }; } function BinaryTree(val) { ...

  5. 详细讲解Codeforces Round #624 (Div. 3) E. Construct the Binary Tree(构造二叉树)

    题意:给定节点数n和所有节点的深度总和d,问能否构造出这样的二叉树.能,则输出“YES”,并且输出n-1个节点的父节点(节点1为根节点). 题解:n个节点构成的二叉树中,完全(满)二叉树的深度总和最小 ...

  6. 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 ...

  7. [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | 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. [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | 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] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:  You may assume tha ...

随机推荐

  1. ubuntu16.04(其他版本也可)批量修改图片名---shell编程

    在windows系统中有很多好用的图片排序软件,可以批量的进行图片排序.然而在ubuntu中,图片排序只能自己写一个shell脚本,编写shell代码.下面是具体的操作步骤.(1).新建一个renam ...

  2. Pandas Statistical Functions

    import pandas as pd import random import numpy as np n_rows=5 n_cols=2 df = pd.DataFrame(np.random.r ...

  3. WeChall_Training: Get Sourced (Training)

    The solution is hidden in this page Use View Sourcecode to get it 解题: 网页源码,最后一行 <!-- You are look ...

  4. WeChall_Training: ASCII (Training, Encoding)

    In a computer, you can only work with numbers.In this challenge you have to decode the following mes ...

  5. spring mvc框架入门

    目录 1.web框架分层 2.什么是springmvc 3.springmvc的优势 4.springmvc和struct的区别 (spring mvc框架入门 1.web框架分层 一般web框架可以 ...

  6. BZOJ2038 小Z的袜子(莫队之源)

    题意+思路: 给你m个区间询问,问每个区间内的$\displaystyle \frac{\sum x^2-(R-L+1)}{(R-L)(R-L+1)} $,其中x为每种数字的个数,用cnt存储: 所以 ...

  7. Ops:命名规范

    前言 好的命名规范见名知义,可以极大的提高工作效率,对于运维工作的标准化至关重要,这里,分享本DevOps小组内讨论的命名规范,希望有参考意义,如果小伙伴们有好的建议或补充,欢迎留言. 1. ansi ...

  8. nginx设置域名转发到指定端口

    1.修改nginx.conf文件,将worker_processes  1 修改为  worker_processes  auto 2.创建端口代理配置文件(域名地址如:xx.baidu.com , ...

  9. javascript A*算法 寻路算法 获取最短路径算法

    //A算法 自动寻路 路径 class GetAutoPath{ constructor(id, map, sPos, ePos, mapArr){ //this.type = id.type; th ...

  10. ELF文件之四——使用链接脚本-2个函数-data

    main.c ; int main() { ; } int add() { ; } main.o 反汇编可以看到多了.text节的反汇编,存储的是全局变量的初始化数值 main.o对比,text段后面 ...