【文文殿下】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 ...
随机推荐
- winform弹出文件和目录选择框
目录选择: FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径&quo ...
- 生成静态页html
代码: using System; using System.Collections; using System.Collections.Generic; using System.IO; using ...
- Scrum 团队成立 -- 软件工程
团队项目选题 : 金融工具:复利计算与投资记录项目继续升级,开发定位明确.功能专注的工具类软件 团队队员 : 蔡舜 , 林宇粲 , 王昕明 , 卢晓洵 团队目标 : 不断完善 团队口号 : 永不 ...
- livecd-creator: UnicodeEncodeError: 'ascii' codec can't encode characters in position 1282-1
通过livecd-creator进行构建ISO过程中出现题目中错误, livecd-creator命令属于包livecd-tools 问题原因: 编码错误,初步判断应该与编码相关的环境变量设置有关 解 ...
- 命名空间namespace ,以及重复定义的问题解析
名字空间是用来划分冲突域的,把全局名字空间划分成几个小的名字空间.全局函数,全局变量,以及类的名字是在同一个全局名字空间中,有时为了防止命名冲突,会把这些名字放到不同的名字空间中去. 首先我们看一下名 ...
- 博客停更转战简书http://www.jianshu.com/u/7ac4047c9cfa
博客停更转战简书 http://www.jianshu.com/u/7ac4047c9cfa
- 2018.09.16 bzoj3626: [LNOI2014]LCA(树链剖分)
传送门 树链剖分好题. 对于每个点维护一个值vi" role="presentation" style="position: relative;"&g ...
- phonegap android插件,启动activity并返回值
Your execute menthod is not quite right. When you do: return new PluginResult(PluginResult.Status.OK ...
- HDU 3681 Prison Break (二分 + bfs + TSP)
题意:给定上一个 n * m的矩阵,你的出发点是 F,你初始有一个电量,每走一步就会少1,如果遇到G,那么就会加满,每个G只能第一次使用,问你把所有的Y都经过,初始电量最少是多少. 析:首先先预处理每 ...
- LA 3942 && UVa 1401 Remember the Word (Trie + DP)
题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...