【文文殿下】CF1098C Construct a tree 题解
题解
挺水的一道题. Rating $ \color{orange} {2300}$ 以下送命题。
首先我们知道,所有子树大小之和就是节点个数加上从根到所有节点的路径长度之和。
他要求度数尽可能小,所以我们二分度数\(k\).显然,度数越小,子树和越大。
对于一个\(k\)叉树,他的子树大小之和为\(n+k^2+k^3+...+rem\)
我们通过二分得到最大的边界\(k\)
然后,此时我们的子树大小\(s\)是要小于等于规定的子树大小和的。
我们考虑扩大子树大小。
显然,我们让某些节点,往深度扩展将会扩大我们的子树大小。
我们记录每个深度的节点个数,已经每个节点的深度。
我们尝试从深度最深的节点开始往下扩展,直至子树大小达到规定大小。
Tips:n-1叉树的大小显然为2n-1 而一条链的大小为 n(n+1)/2 。如果k不在这个范围内,则无解。
具体实现非常简单。代码如下
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 1e6+10;
ll n,k,cnt[maxn],d[maxn];
bool check(int x) {
ll i(2),j,t=1,num=k-n,dep=0;
memset(d,0,sizeof d);
memset(cnt,0,sizeof cnt);
while(i<=n) {
t*=x;++dep;
for(j=1;j<=t&&i<=n;++i,++j) cnt[dep]++,d[i]=dep,num-=dep;
}
if(num<0) return false;
j=n;
while(num) {
++dep;
if(cnt[d[j]]==1) --j;
t = min(num,dep-d[j]);
cnt[d[j]]--;
d[j]+=t;
cnt[d[j]]++;
num-=t;
--j;
}
return true;
}
int main() {
cin>>n>>k;
if(k<(1LL*2*n-1)||k>(1LL*n*(n+1)/2)) {
puts("No");
exit(0);
}
int l = 1, r = n;
while(l<r) {
int mid = (l+r)>>1;
if(check(mid)) r=mid;
else l = mid+1;
}
check(r);
puts("Yes");
int pos;
d[pos=1]=0; sort(d+2,d+1+n); memset(cnt,0,sizeof cnt);
for(int i = 2;i<=n;++i) {
while(d[pos]!=d[i]-1||cnt[pos]==r) ++pos;
cout<<pos<<' ';cnt[pos]++;
}
return 0;
}
【文文殿下】CF1098C Construct a tree 题解的更多相关文章
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode java
题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
- Construct Binary Tree from Preorder and Inorder Traversal leetcode java
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 【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 889. Construct Binary Tree from Preorder and Postorder Traversal
原题链接在这里:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-postorder-traversal/ 题 ...
- [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 OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
随机推荐
- golang协程进行同步方法
1.使用chanel func main() { done := make(chan bool) ticker := time.NewTicker(time.Millisecond * 1000) g ...
- Js中的this关键字(吉木自学)
研究生毕业答辩完,开始继续为转行努力.小白要奋斗了,加油.本文引自JS核心系列:浅谈函数的作用域. 在一个函数中,this总是指向当前函数的所有者对象,this总是在运行时才能确定其具体的指向, 也才 ...
- Bug:src/lxml/lxml.etree.c:84:20: 致命错误:Python.h:没有那个文件或目录
问题描述: pip批量安装软件包时,出现如上题目错误,卡在了lxm依赖于python中的python-devel 问题原因: 缺失python-devel开发包所导致,python.h存在于pytho ...
- pip安装包
默认的在安装文件的Lib\site-packages\路径下面 cmd窗口 cd切换路径:C:\Users\admin\AppData\Local\Programs\Python\Python36\S ...
- 2018.09.01 poj3071Football(概率dp+二进制找规律)
传送门 概率dp简单题. 设f[i][j]表示前i轮j获胜的概率. 如果j,k能够刚好在第i轮相遇,找规律可以发现j,k满足: (j−1)>>(i−1)" role=" ...
- 2018.08.28 洛谷P4360 [CEOI2004]锯木厂选址(斜率优化dp)
传送门 一道斜率优化dp入门题. 是这样的没错... 我们用dis[i]表示i到第三个锯木厂的距离,sum[i]表示前i棵树的总重量,w[i]为第i棵树的重量,于是发现如果令第一个锯木厂地址为i,第二 ...
- 2018.07.07 BZOJ2212: Poi2011Tree Rotations(线段树合并)
2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MB Description Byteasar the garde ...
- Android线程和线程Handler基础一览
线程概览 线程是任何多任务系统的基石.可以被认为是一个主进程的多个子进程.这样做的目的就是了增加应用的性能. 应用主线程 当一个Android应用被打开的时候,系统会默认开辟一个线程.这个线程就被叫做 ...
- C语言中 指针的基础知识总结, 指针数组的理解
1: 指针变量所占的字节数与操作系统为位数有关,64位操作系统下,占8个字节,32位操作系统下,占4个字节 2: 指针变量的本质是用来放地址,而一般的变量是放数值的3: 脱衣服法则: a[2] 变成 ...
- 【转】Android贪吃蛇源代码
/*TileView:tile有瓦片的意思,用一个个tile拼接起来的就是地图.TileView就是用来呈现地图的类*/ public class TileView extends View { /* ...