CodeForces - 1098.DIV1.C: Construct a tree(贪心,构造)
Misha walked through the snowy forest and he was so fascinated by the trees to decide to draw his own tree!
Misha would like to construct a rooted tree with n
vertices, indexed from 1 to n, where the root has index 1. Every other vertex has a parent pi, and i is called a child of vertex pi. Vertex u belongs to the subtree of vertex v iff v is reachable from u while iterating over the parents (u, pu, ppu, ...). Clearly, v belongs to its own subtree, and the number of vertices in the subtree is called the size of the subtree. Misha is only interested in trees where every vertex belongs to the subtree of vertex 1
.
Below there is a tree with 6
vertices. The subtree of vertex 2 contains vertices 2, 3, 4, 5. Hence the size of its subtree is 4
.
The branching coefficient of the tree is defined as the maximum number of children in any vertex. For example, for the tree above the branching coefficient equals 2
. Your task is to construct a tree with n vertices such that the sum of the subtree sizes for all vertices equals s
, and the branching coefficient is minimum possible.
Input
The only input line contains two integers n
and s — the number of vertices in the tree and the desired sum of the subtree sizes (2≤n≤105; 1≤s≤1010
).
Output
If the required tree does not exist, output «No». Otherwise output «Yes» on the first line, and in the next one output integers p2
, p3, ..., pn, where pi denotes the parent of vertex i
.
Examples
3 5
Yes
1 1
4 42
No
6 15
Yes
1 2 3 1 5
Note
Below one can find one of the possible solutions for the first sample case. The sum of subtree sizes equals 3+1+1=5
, and the branching coefficient equals 2
.
Below one can find one of the possible solutions for the third sample case. The sum of subtree sizes equals 6+3+2+1+2+1=15
, and the branching coefficient equals 2
.
题意:给定N,S,让你构造一个大小为N的数,使得每个节点子树大小之和为S,如果存在,请构造一个树,使得儿子最多的点的儿子数量(P)最少。
思路:我们发现对于大小一定的树,越瘦长K越大(一条链,最大为N*(N+1)/2),越矮胖越小(菊花树,最小为N+N-1),那么如果K不在这个范围我们输出-1;如果在,我们一定看i有构造一个满足题意的树。 我们可以二分得到P。然后来构造。 我的构造方式是先构造一条链,此时的sum=N*(N+1)/2;如果sum>S,我们就把最下面的点移到上面的某个位置,知道sum=S。
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;ll N,S;
ll fa[maxn],q[maxn],d[maxn],head,tail,sz[maxn],son[maxn];
bool check(ll Mid)
{
ll tN=N,now=,p=,res=;
while(tN){
res+=min(p,tN)*now;
if(res>S) return false;
tN-=min(p,tN);
p*=Mid; now++;
} return true;
}
int main()
{
cin>>N>>S;
ll Mn=N+N-; ll Mx=N*(N+)/;
if(S<Mn||S>Mx) return puts("NO"),;
ll L=,R=N-,Mid,res;
while(L<=R){
Mid=(L+R)/;
if(check(Mid)) res=Mid,R=Mid-;
else L=Mid+;
}
puts("YES");
rep(i,,N) sz[i]=; ll Now=Mx,D=;
for(int i=N;;i--){
if(Now==S) break;
if(sz[D]==sz[D-]*res) D++;
if(Now-S>=i-D){
sz[D]++; sz[i]--;
Now-=(i-D);
}
else {
sz[i]--; sz[i-(Now-S)]++;
Now=S;
}
}
head=tail=; q[head]=; d[]=;
ll p=;
rep(i,,N) {
if(sz[i]==) break;
L=p+; R=p+sz[i];
rep(j,L,R){
while(d[q[head]]!=i-||son[q[head]]==res){
head++;
}
fa[j]=q[head]; son[q[head]]++;
q[++tail]=j; d[j]=i;
}
p=R;
}
rep(i,,N) printf("%lld ",fa[i]);
return ;
}
CodeForces - 1098.DIV1.C: Construct a tree(贪心,构造)的更多相关文章
- CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)
题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...
- 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 ...
- [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 ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- Construct a tree from Inorder and Level order traversals
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is a ...
- 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
随机推荐
- 团队作业7—团队项目设计完善&编码测试
一.根据OOD详细设计工作要点,修改完善团队项目系统设计说明书和详细设计说明. <软件设计方案说明书>Github仓库地址:https://github.com/RNTF6/web 完善内 ...
- 创意时钟 人形时钟 可惜不是 https
; (function () { $('#header').css({ 'position':'relative' }).prepend('<div id="clockWrap&quo ...
- xhr文件类型说明
2017-08-08 18:31:08 xhr : XML HTTP Request,是一种在后台与服务器进行交互的数据.这意味着可以在不加载整个网页的情况下,对网页中的部分内容进行更新. 这是Aja ...
- 2017-03-03 Oracle在.Net中出现未在本地计算机上注册“OraOLEDB.Oracle”提供程序的错误
在前面的Oracle配置完成后,打开项目运行出错,出现未在本地计算机上注册“OraOLEDB.Oracle”提供程序的错误,看到“注册”两个字,首先想到,难道还要用命令行注册一下?果不其然,需要手动注 ...
- 常用HTTP contentType与后端处理方式
常用HTTP contentType与后端处理方式 1.Content-Type:application/x-www-form-urlencoded; charset=UTF-8 前端export.j ...
- 20170801xlVBA含有公式出现弹窗合并
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Public Sub GatherD ...
- Android设计模式之工厂模式
定义 工厂模式是我们最常用的实例化对象模式了,是用工厂方法代替new操作的一种模式.著名的Jive论坛 ,就大量使用了工厂模式,工厂模式在Java程序系统可以说是随处可见.因为工厂模式就相当于创建实例 ...
- python-day53--前端js
一.基本语法(ECMA) 单行注释 // /* 多行注释 */ 变量赋值 默认以换行符作为结束符,有分好以分号作为结束符号 JS的引入方式: 1. <script> </script ...
- 170301、使用Spring AOP实现MySQL数据库读写分离案例分析
使用Spring AOP实现MySQL数据库读写分离案例分析 原创 2016-12-29 徐刘根 Java后端技术 一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解决方案 ...
- UVA-1533 Moving Pegs (路径寻找问题)
Description Venture MFG Company, Inc. has made a game board. This game board has 15 holes and thes ...