CodeForces - 1098.DIV1.C: Construct a tree(贪心,构造)

时间:2022-09-10 10:06:04

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

.

CodeForces - 1098.DIV1.C: Construct a tree(贪心,构造)

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

Input
3 5
Output
Yes
1 1
Input
4 42
Output
No
Input
6 15
Output
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

.

CodeForces - 1098.DIV1.C: Construct a tree(贪心,构造)

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

.

CodeForces - 1098.DIV1.C: Construct a tree(贪心,构造)

题意:给定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(贪心,构造)的更多相关文章

  1. CodeForces - 748D Santa Claus and a Palindrome &lpar;贪心&plus;构造&rpar;

    题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...

  2. Leetcode&comma; 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 ...

  3. &lbrack;LeetCode&rsqb; 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 ...

  4. 【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 ...

  5. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  6. Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...

  7. 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 ...

  8. 【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 ...

  9. 36&period; Construct Binary Tree from Inorder and Postorder Traversal &amp&semi;&amp&semi; Construct Binary Tree from Preorder and Inorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...

随机推荐

  1. 2016阿里巴巴校招offer面经

    前段时间参加阿里巴巴校招,非常荣幸,很快就拿到了offer,经历了三轮技术面试和一轮hr面,面试官们都非常nice,在此感谢一下各位面试官,你们辛苦了,百忙之中抽时间面试!为了帮助更多人想进阿里巴巴的 ...

  2. 标准IO库函数复习

    打开文件,打开文件一定要成对写,养成好习惯很重要.比如 fopen()fclose<ol> <li>fopen()</li> <pre lang=" ...

  3. 【无聊放个模板系列】HDU 1358 KMP

    #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #inc ...

  4. Redis学习手册&lpar;实例代码&rpar;

    在之前的博客中已经非常详细的介绍了Redis的各种操作命令.运行机制和服务器初始化参数配置.本篇博客是该系列博客中的最后一篇,在这里将给出基于Redis客户端组件访问并操作Redis服务器的代码示例. ...

  5. linux内核学习之二:编译内核

    在linux内核学习系列的第一课中讲述了搭建学习环境的过程(http://www.cnblogs.com/xiongyuanxiong/p/3523306.html),环境搭好后,马上就进入到下一环节 ...

  6. Windows Server 2008企业64位版防火墙添加端口的方法

    原始地址:http://www.veryhuo.com/a/view/48280.html 什么是防火墙的入站规则和出站规则 简单的说 出站就是你访问外网 入站就是外网访问你 记得在两年前写过一篇教程 ...

  7. 为Textview里面的ImageSpan添加点击响应事件

    对于图文混排的TextView,用户在浏览到里面的图片的时候,往往有点击图片preview大图或者preview之后保存图片的需求,这就需要为Textview里面的ImageSpan设置点击响应事件. ...

  8. 记录JavaFx中非常重要的细节,入门了也未必知道

    title: 记录JavaFx中非常重要的细节 JavaFx中有一些疑难杂症,或许你以为你掌握了JavaFx,但是也未必知道我所说的这些问题和解决方案,如果有帮助到你的,可以加群最大最活跃的JavaF ...

  9. 功能代码(1)---通过Jquery来处理复选框

    实现以下功能: 1:选中第一个复选框,那么下面所有的复选框都选中,去除选中第一个复选框,下面的都不选中 2:当点击全选按钮,上面足球.篮球.游泳.唱歌 全部选中 3:当点击全不选按钮,上面四个全部取消 ...

  10. python基础知识9---字符串拼接,深浅拷贝,三元运算

    一.字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3 ...