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 ...
随机推荐
- linux 管理进程和计划任务
目录 一.进程原理 二.进程工作过程 三.进程类型 四.管理进程 五.前后台调度 六.定时任务 七.管理定时任务 一.进程原理 进程:指正在运行的程序称之为进程 程序:指的是没有运行的代码 线程:真正 ...
- 技术招聘漫谈 | 正在招Golang工程师的你,赶快收藏这份识人秘籍!
各位技术面试官,欢迎来到新一期的技术招聘漫谈专栏. 在前两期的专栏中,我们解析了前端工程师以及 Java 工程师 这两个常见技术岗位的招聘技巧. 今天,我们想把目光聚焦在一个前景与"钱&qu ...
- Kubernetes(k8s)健康性检查:livenessprobe探测和readinessprobe探测
目录 一.系统环境 二.前言 三.Kubernetes健康性检查简介 四.创建没有探测机制的pod 五.添加livenessprobe探测 5.1 使用command的方式进行livenessprob ...
- MySQL读取的记录和我想象的不一致
摘要:并发的事务在运行过程中会出现一些可能引发一致性问题的现象,本篇将详细分析一下. 本文分享自华为云社区<MySQL读取的记录和我想象的不一致--事物隔离级别和MVCC>,作者:砖业洋_ ...
- React学习时,outlet配置(token判定,页面path监听)
尽管写过 outlet 路由的配置. 考虑到 token 判定和 路由页 变更,我不了解v6是不是有更详解的做法. 决定调一下配置,期望 在任何页面异步更新时,token 都可以在跳转前 被检测到,防 ...
- 【Shell】数组
数组 bash 只支持一维数组. 数组下标从 0 开始,下标可以是整数或算术表达式,其值应大于或等于 0. 创建数组 # 创建数组的不同方式 nums=([2]=2 [0]=0 [1]=1) colo ...
- 快速上手 | Datavines 两表值比对规则用法
Datavines 是一站式开源数据可观测性平台,提供元数据管理.数据概览报告.数据质量管理,数据分布查询.数据趋势洞察等核心能力,致力于帮助用户全面地了解和掌管数据,让您做到心中有数. 场景 比较某 ...
- C语言基础--数组详细说明
目录 一.什么是数组 二.一维数组 1.一维数组创建 2.一维数组的使用 2.1 索引值 2.2 遍历数组 2.3 如何使用sizeof()计算出数组的长度 三.二维数组 1.二维数组的创建 2.二维 ...
- 使用Stable Diffusion制作AI数字人视频的简明教程
基本方法 搞一张照片,搞一段语音,合成照片和语音,同时让照片中的人物动起来,特别是头.眼睛和嘴. 语音合成 语音合成的方法很多,也比较成熟了,大家可以选择自己方便的,直接录音也可以,只要能生成一个语音 ...
- redis 使用 nologin 用户启动
添加不可登录的redis用户 sudo useradd -M -s /sbin/nologin redis 为redis新建目录,并设置属性 mkdir -p /data/redis &&am ...