【文文殿下】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 ...
随机推荐
- ecplice中代码使用快捷键无法格式化,使用其他方法将代码格式化的步骤
选中需要进行格式化的代码--->右键--->source--->format,就可以将代码格式化了.
- 记录java版本不兼容的坑,(kafka运行报错)
启动kafka报错 错误原因是: 由较高版本的jdk编译的java class文件 试图在较低版本的jvm上运行的报错 解决办法是: 查看java版本 C:\Users\Administrator&g ...
- MacOs执行SQL出错(mysql)
上次修改过root密码之后,刚启动,执行sql就报错了. 错误是: Reset MySQL root password using ALTER USER statement after install ...
- 构建openssl debug版
一.简介 作为一种安全协议,openssl囊括了主要的密码算法.常用的密钥和证书封装管理功能以及SSL协议,并提供了丰富的应用程序供测试或其它目的使用. 参考: http://www.linuxidc ...
- Linux动态共享库
Linux操作系统上面的动态共享库大致分为三类: 一.操作系统级别的共享库和基础的系统工具库 libc.so, libz.so, libpthread.so等等,这些系统库会被放在/lib和/us ...
- p3412 [POI2005]SKO-Knights
传送门 分析 图1 我们假设我们现在有两个向量(2,3)和(4,2),将他们所能到达的点在几何画板上画出来,再将这些点用红线连起来,在将横坐标相同的点用蓝线连起来便能得到图1,就此我们可以发现可以用绿 ...
- cmake重新编译
删除文件夹下的文件 rm CMakeCache.txt 重新编译即可 安装g++ yum install gcc-c++
- 8.13Oracle新建用户、角色,授权
oracle数据库的权限系统分为系统权限与对象权限.系统权限( database system privilege )可以让用户执行特定的命令集.例如,create table权限允许用户创建表,gr ...
- pthread_once 和 pthread_key
http://blog.csdn.net/rickyguo/article/details/6259410 一次性初始化 有时候我们需要对一些posix变量只进行一次初始化,如线程键(我下面会讲到). ...
- 使用eclipse创建android项目的时候为什么会生成两个项目
使用eclipse创建android项目的时候为什么会生成两个项目 问题描述: 使用eclipse创建一个Android项目时,发现project列表中会多创建出一个appcompat_v7项目,再创 ...