[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 ...
随机推荐
- Pycharm如何快捷地交互式运行代码(>>>)
Pycharm如何快捷地交互式运行代码? 问题描述 在Pycharm直接一行行地交互运行已经写好的代码,不需要复制粘贴,怎么调出Pychram的交互式界面. 通过python自带的交互式界面 在搜索栏 ...
- ImageButton去边框&Button或者ImageButton的背景透明
在ImageButton中载入图片后,很多人会觉得有图片周围的白边会影响到美观,其实解决这个问题有两种方法 一种方法是将ImageButton的背景改为所需要的图片.如:android:backgro ...
- python函数2(返回值、传递列表...)
python函数2(返回值.传递列表...) 1.返回值 1.1.返回简单的值 #返回简单值 def get_formatted_name(first_name,last_name): "& ...
- BZOJ2326 [HNOI2011]数学作业(分块矩阵快速幂)
题意: 定义函数Concatenate (1 ..N)是将所有正整数 1, 2, …, N 顺序连接起来得到的数,如concatenate(1..5)是12345,求concatenate(1...n ...
- input . type=number.使用后问题点
所有主浏览器都支持type属性,但是,并非所有主流浏览器都支持所有不同的 input 类型. 以下 input 类型是 HTML5 中的新类型:color.date.datetime.datetime ...
- CentOS 7 GNOME桌面系统 网络配置
问题概述:在学习Linux系统的过程中,在WORKSTATION 14 PRO上安装了CentOS 7 Linux虚拟机,安装过程一切正常,但在应用过程中无法连接网络: 具体问题:1. 通过 ip a ...
- php面试笔记(4)-php基础知识-流程控制
本文是根据慕课网Jason老师的课程进行的PHP面试知识点总结和升华,如有侵权请联系我进行删除,email:guoyugygy@163.com 在面试中,考官往往喜欢基础扎实的面试者,而流程控制相关的 ...
- 学习CSS之用CSS实现时钟效果
一.机械时钟 1.最终效果 用 CSS 绘制的机械时钟效果如下: HTML 中代码结构为: <body> <div class="clock"> ...
- 用原生JS&PHP简单的AJAX实例
功能介绍: 1)file.html 使用 xmlhttp 请求服务器端文件 text ,并更新 file.html 的部分内容 2)update.html 使用 xmlhttp 通过 filewrit ...
- 常用MySQL操作
常用MySQL操作 更改MySQL数据库root的密码 将绝对路径加入环境变量并设置开机启动 # PATH=$PATH:/usr/local/mysql/bin # echo "PATH=$ ...