CodeForces 1311E Construct the Binary Tree
题意
给定\(n\)和\(d\),构造一颗\(n\)个节点的二叉树(以\(1\)为根),所有节点到\(1\)的距离和为\(d\),不行输出\(NO\),否则输出\(YES\)和\(2\)-\(n\)的父亲编号
分析
最大和显然是一条链,如果最大和仍小于\(d\),则不行,否则先构造出一条链,然后枚举当前的层数,如果当前层数的下一层仍然可以填,则将链的尾端放置在下一层可得到最大的变化,如果这个变化大于所需要的变化,则找到挂置的层数,否则则挂在下一层,若下一层已满,则将所枚举层数下移
#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#define start ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define int ll
#define ls st<<1
#define rs st<<1|1
#define pii pair<int,int>
#define rep(z, x, y) for(int z=x;z<=y;++z)
#define com bool operator<(const node &b)
const int maxn = (ll) 5e3 + 5;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
int T = 1;
int pre[maxn];
set<int> lay[maxn];
vector<int> son[maxn];
void solve() {
int n, d;
cin >> n >> d;
int now = 0;
son[1].clear();
lay[1].clear();
lay[1].insert(1);
rep(i, 2, n) {
pre[i] = i - 1, now += i - 1, lay[i].clear();
lay[i].insert(i);
son[i].clear();
son[i - 1].push_back(i);
}
if (now < d) {
cout << "NO\n";
return;
}
int nowLay = 1;
for (int i = n; i >= 1; --i) {
if (i <= nowLay + 1)
break;
int maxx = i - nowLay - 1;
if (maxx >= now - d) {
if (lay[i - 1 - (now - d)].empty()) {
++nowLay;
++i;
continue;
}
int fa = *lay[i - 1 - (now - d)].begin();
pre[i] = fa;
now = d;
break;
} else {
now -= maxx;
int fa = *lay[nowLay].begin();
son[fa].push_back(i);
if (son[fa].size() == 2)
lay[nowLay].erase(fa);
son[pre[i]].erase(son[pre[i]].begin());
pre[i] = fa;
lay[nowLay + 1].insert(i);
lay[i].erase(i);
if (lay[nowLay].empty())
++nowLay;
}
}
if (now != d) {
cout << "NO\n";
return;
}
cout << "YES\n";
rep(i, 2, n)cout << pre[i] << ' ';
cout << '\n';
}
signed main() {
start;
cin >> T;
while (T--)
solve();
return 0;
}
CodeForces 1311E Construct the Binary Tree的更多相关文章
- codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)
ACM思维题训练集合 You are given two integers n and d. You need to construct a rooted binary tree consisting ...
- [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 ...
- CF1311E Construct the Binary Tree
膜这场比赛的 \(rk1\) \(\color{black}A\color{red}{lex\_Wei}\) 这题应该是这场比赛最难的题了 容易发现,二叉树的下一层不会超过这一层的 \(2\) 倍,所 ...
- [CF1311E] Construct the Binary Tree - 构造
Solution 预处理出 \(i\) 个点组成的二叉树的最大答案和最小答案 递归做,由于只需要构造一种方案,我们让左子树大小能小就小,因此每次从小到大枚举左子树的点数并检验,如果检验通过就选定之 现 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
随机推荐
- Github疯传!200本计算机经典书籍!
好书在精不在多,每一本经典书籍都值得反复翻阅,温故而知新! 下面分享几本计算机经典书籍,都是我自己看过的. 重构 改善既有代码的设计 就像豆瓣评论所说的,看后有种醍醐灌顶.欲罢不能的感觉.无论你是初学 ...
- 生信服务器 | 更改 CentOS/RHEL 6/7 中的时区
这几天在学习折腾 docker 的时候遇到一个很常见的问题,就是 run container 的时候发现大部分 image 默认使用的时间都是 UTC (Universal Time Coordin ...
- JupyterLab Server 搭建与使用笔记
两三个月前,有幸拿到了云筏的一个 4 核 16G,1TB硬盘,300M带宽位于欧洲的云服务器,自带的开箱即用的 RStudio Server 也非常给力,但最近这两天在升级 R 的时候遇上了不少问题, ...
- Windows全能终端神器MobaXterm
MobaXterm 又名 MobaXVT,是一款增强型终端.X 服务器和 Unix 命令集(GNU/ Cygwin)工具箱. MobaXterm 可以开启多个终端视窗,以最新的 X 服务器为基础的 X ...
- Galaxy 生信平台(三):xlsx 上传与识别
我在<Firefox Quantum 向左,Google Chrome 向右>中,曾经吐槽过在 Firefox 中使用 Galaxy 上传本地的 Excel 文件时,会出现 xlsx 无法 ...
- 为什么 Biopython 的在线 BLAST 这么慢?
用过网页版本 BLAST 的童鞋都会发现,提交的序列比对往往在几分钟,甚至几十秒就可以得到比对的结果:而通过调用 API 却要花费几十分钟或者更长的时间!这到底是为什么呢? NCBIWWW 基本用法 ...
- k8s实战案例之部署Zookeeper集群
1.Zookeeper简介 zookeeper是一个开源的分布式协调服务,由知名互联网公司Yahoo创建,它是Chubby的开源实现:换句话讲,zookeeper是一个典型的分布式数据一致性解决方案, ...
- C++面试八股文:C++中,函数的参数应该传值还是传引用?
某日二师兄参加XXX科技公司的C++工程师开发岗位第8面: 面试官:C++中,函数的参数应该传值还是传引用? 二师兄:要看参数的用途.如果是出参,必须传引用.如果是入参,主要考虑参数类型的大小,来决定 ...
- 开源 API 网关的访问策略(一)
许多企业和组织面临着网关访问控制的挑战,因为传统的访问控制方法往往过于笨重和繁琐.这些方法可能涉及复杂的规则集.繁琐的手动配置过程.缺乏灵活性和可扩展性等问题.此外,随着云计算和移动设备的广泛应用,访 ...
- 园子的商业化努力-行行AI人才培养「常青藤计划」
各位园子的小伙伴: 感谢大家长期对园子的支持,AI大模型出现之后,各行各业都在积极思考如何应对,如何把业务场景和AI结合.在这个过程中,AI人才缺乏是最核心的问题. 基于此,园子打算在AI人才培养方面 ...