[CF1311E] Construct the Binary Tree - 构造

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 - 构造的更多相关文章
- CF1311E Construct the Binary Tree
膜这场比赛的 \(rk1\) \(\color{black}A\color{red}{lex\_Wei}\) 这题应该是这场比赛最难的题了 容易发现,二叉树的下一层不会超过这一层的 \(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 ...
- HDU 5573 Binary Tree 构造
Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...
- [Algorithm] Construct a Binary Tree and Binary Search
function createNode(value) { return { value, left: null, right: null }; } function BinaryTree(val) { ...
- 详细讲解Codeforces Round #624 (Div. 3) E. Construct the Binary Tree(构造二叉树)
题意:给定节点数n和所有节点的深度总和d,问能否构造出这样的二叉树.能,则输出“YES”,并且输出n-1个节点的父节点(节点1为根节点). 题解:n个节点构成的二叉树中,完全(满)二叉树的深度总和最小 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- CSS selector? [class*=“span”]怎么理解
我在Twitter 中有看到如下selector: .show-grid [class*="span"] { background-color: #eee; text-align: ...
- WeChall_PHP 0817 (PHP, Exploit)
I have written another include system for my dynamic webpages, but it seems to be vulnerable to LFI. ...
- Open Images V4 下载自己需要的类别
OpenImages V4数据集描述1)这个v4数据集主要有两种用途:对象检测及分类,意思是说可以用这个数据集训练出对象检测模型,用于识别图像中的对象类别及位置边框.视觉关系检测,比如你用这个v4数据 ...
- Codeforces 1064D Labyrinth(双端队列BFS)
题意: 给一个图,"*"不可以走,给你一个起点,限制向左走L次,向右走R次,上下不限制,问你最多可以走到多少个格子 思路: BFS,每次将上下走的策略加入队首,左右加入队尾,(相当 ...
- 小程序云开发--内容安全审查API云调用
云调用 云调用是小程序·云开发提供的在云函数中调用微信开放接口的能力,需要在云函数中通过 wx-server-sdk 使用. 接口方法 openapi.security.msgSecCheck 需在 ...
- 一文读懂什么是一致性hash算法
Hash,一般翻译做散列.杂凑,或音译为哈希,是把任意长度的输入通过散列算法变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入值的空间,不同的输入可能会 ...
- Go语言实现:【剑指offer】字符串的排列
该题目来源于牛客网<剑指offer>专题. 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,b ...
- 动手学习Pytorch(6)--卷积神经网络基础
卷积神经网络基础 本节我们介绍卷积神经网络的基础概念,主要是卷积层和池化层,并解释填充.步幅.输入通道和输出通道的含义. 二维卷积层 本节介绍的是最常见的二维卷积层,常用于处理图像数据. 二维 ...
- 怎么理解Laravel的核心架构
使用过larave框架的朋友都知道laravel框架里面除了提供一些基本的功能(如控制器.视图.模型)之外,还有中间件.门面.契约等,这些东西是如何在laravel框架运用起来的呢?今天就和大家详聊一 ...
- PHP 中 new static 和 new self 的区别
今天老大在公司 问了一下 new static 和 new self 的区别 公司十个程序 竟然没有一个回答上来 后面画面自补 ... 本屌丝回家后 就百度了解了下 这二者区别 : ...