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 ...
随机推荐
- 7-8 估值一亿的AI核心代码
题目描述: 以上图片来自新浪微博. 本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是: 无论用户说什么,首先把对方说的话在一行中原样打印出来: 消除原文中多余空格:把相邻单词间的多个空格 ...
- 800名科学家Nature联名发文主张废除p值!
每年,全球有数百万学生修读统计学课程.随着世界上的数据量越来越大,统计学已成为越来越受欢迎的话题.如果大多数学生都从这门课中记住一点,那可能就是"统计显著性"和"p 值& ...
- CANoe _ DBC 的创建过程
在Canoe中创建DBC(Database Container)文件,用于描述和定义CAN总线上的节点.消息和信号,遵循以下步骤: 1.打开Canoe 启动Canoe软件. 2.创建新项目 在Cano ...
- Custom directive is missing corresponding SSR transform and will be ignored
背景 最近在给业务组件库集成指令库,将各个项目中常用的指令如一键复制.元素和弹窗拖拽等封装到一起,进行统一发版维护. 业务组件库项目架构采用的是pnpm+vite+vue3+vitepress,其中v ...
- 在线免费ChatGPT,官方api
作为一款强大的语言模型,ChatGPT在自然语言处理领域享有较高声誉.现在,您可以在我们的在线平台上免费体验ChatGPT的功能了! 经过不断地优化和改进,我们的在线聊天机器人已经能够针对各种话题展示 ...
- 前端树形结构图treeShapeStruct,可拖拽移动,点击展开收缩,无限添加子集
快速实现树形结构图,可拖拽移动,点击展开收缩,无限添加子集; 下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=12650 效果图如 ...
- SSM项目整合思路以及代码
当前真实的企业开发中,SpringBoot已经统一Java企业应用开发,很少有在使用SSM项目来进行项目开发的. 可是在教学阶段,还是很有必要给学生讲解Java开发中一套很经典的框架,SSM框架.作为 ...
- 一个C#跨平台的机器视觉和机器学习的开源库
大家都知道OpenCV是一个跨平台的机器视觉和机器学习的开源库,可以运行在Linux.Windows.Android和Mac OS操作系统上,由C++开发. 今天给大家介绍一个用C#对OpenCV封装 ...
- 【Linq】 join 子句中其中一个表达式的类型不正确。在对“GroupJoin”的调用中,类型推理失败。
报错代码: 原因分析:on后面的两个关联条件 数据类型不匹配,调整为一致即可.
- kubernetes(k8s):解决不在同一网段加入集群失败问题
执行下面命令,将内外网进行映射. iptables -t nat -A OUTPUT -d 10.140.128.121 -j DNAT --to-destination 10.170.129.153 ...