ACM思维题训练集合

You are given two integers n and d. You need to construct a rooted binary tree consisting of n vertices with a root at the vertex 1 and the sum of depths of all vertices equals to d.

A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex v is the last different from v vertex on the path from the root to the vertex v. The depth of the vertex v is the length of the path from the root to the vertex v. Children of vertex v are all vertices for which v is the parent. The binary tree is such a tree that no vertex has more than 2 children.

You have to answer t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤1000) — the number of test cases.

The only line of each test case contains two integers n and d (2≤n,d≤5000) — the number of vertices in the tree and the required sum of depths of all vertices.

It is guaranteed that the sum of n and the sum of d both does not exceed 5000 (∑n≤5000,∑d≤5000).

Output

For each test case, print the answer.

If it is impossible to construct such a tree, print “NO” (without quotes) in the first line. Otherwise, print “{YES}” in the first line. Then print n−1 integers p2,p3,…,pn in the second line, where pi is the parent of the vertex i. Note that the sequence of parents you print should describe some binary tree.

Example

inputCopy

3

5 7

10 19

10 18

outputCopy

YES

1 2 1 3

YES

1 2 3 3 9 9 2 1 6

NO

Note

Pictures corresponding to the first and the second test cases of the example:



丫的,改了一天。

如果b在构造的树的深度最大(左偏或右偏树)和最小(满二叉树)之内就能构成,然后从左偏树开始不断的将低端的点向上移动,知道达到要求。

#include <bits/stdc++.h>
using namespace std;
int f[210];
inline void solve()
{
memset(f, 0, sizeof(f));
int n, d, maxd = 0;
scanf("%d %d", &n, &d);
--n;
if (d > n * (n + 1) / 2)
{
printf("NO\n");
return;
} //1
for (int i = 1;; ++i)
{
maxd = i;
if (n > (1 << i))
{
d -= i * (1 << i);
f[i] = 1 << i;
n -= 1 << i;
}
else
{
d -= i * n;
f[i] = n;
n -= n;
break;
}
}
if (d < 0)
{
printf("NO\n");
return;
}
while (1)
{
if (d == 0)
break;
int p;
for (p = maxd; p >= 1; --p)
if (f[p] > 1)
break;
--d;
--f[p];
++f[p + 1];
if (p + 1 > maxd)
maxd = p + 1;
}
printf("YES\n");
int p = 1, np = 1, cnt;
for (int i = 1; i <= maxd; ++i)
{
int t = p;
cnt = 0;
for (int j = 1; j <= f[i]; ++j)
{
++p;
++cnt;
if (cnt >= 3)
{
++np;
cnt = 1;
}
printf("%d ", np);
}
np = t + 1;
}
printf("\n");
}
int main()
{
int t;
scanf("%d", &t);
for (int i = 1; i <= t; ++i)
solve();
return 0;
}

codeforce 1311E. Construct the Binary Tree (构造,就是个模拟)的更多相关文章

  1. [CF1311E] Construct the Binary Tree - 构造

    Solution 预处理出 \(i\) 个点组成的二叉树的最大答案和最小答案 递归做,由于只需要构造一种方案,我们让左子树大小能小就小,因此每次从小到大枚举左子树的点数并检验,如果检验通过就选定之 现 ...

  2. HDU 5573 Binary Tree 构造

    Binary Tree 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5573 Description The Old Frog King lives ...

  3. [Algorithm] Construct a Binary Tree and Binary Search

    function createNode(value) { return { value, left: null, right: null }; } function BinaryTree(val) { ...

  4. 详细讲解Codeforces Round #624 (Div. 3) E. Construct the Binary Tree(构造二叉树)

    题意:给定节点数n和所有节点的深度总和d,问能否构造出这样的二叉树.能,则输出“YES”,并且输出n-1个节点的父节点(节点1为根节点). 题解:n个节点构成的二叉树中,完全(满)二叉树的深度总和最小 ...

  5. CF1311E Construct the Binary Tree

    膜这场比赛的 \(rk1\) \(\color{black}A\color{red}{lex\_Wei}\) 这题应该是这场比赛最难的题了 容易发现,二叉树的下一层不会超过这一层的 \(2\) 倍,所 ...

  6. Data Structure Binary Tree: Construct Full Binary Tree from given preorder and postorder traversals

    http://www.geeksforgeeks.org/full-and-complete-binary-tree-from-given-preorder-and-postorder-travers ...

  7. [Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | 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 ...

  8. [Swift]LeetCode106. 从中序与后序遍历序列构造二叉树 | 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 ...

  9. [Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:  You may assume tha ...

随机推荐

  1. flask-include、set、with、模板继承

    flask-include.set.with include: 跟django的include类似,将一个html的代码块直接嵌入另一个html文件中 {%   include    'html    ...

  2. MongoDB查询mgov2的聚合方法

    1.多条表数据累计相加. respCount := struct { Rebatescore int64 //变量命名必须要和查询的参数一样.}{} o := bson.M{"$match& ...

  3. C语言输出菱形

    #include<stdio.h> #include<string.h> int main(){          int data[7][7] = {0};     for( ...

  4. JS入门系列(1)-原型-函数原型

    实例1: 首先定义一个Persion类或者说是函数 var p1 = Persion();:表示,作为普通函数调用 var p2 = new Persion();:表示,作为构造器调用 创建函数之后, ...

  5. Exchange 2016 OWA登陆异常

    今天,收到脚本的告警信息,有一台Exchange服务器OWA无法登陆! 手动进行了一下测试,发现确实存在问题,报错信息如下: 检查了一下该台服务器的日志,找到了如下信息 1.访问OWA的请求在HTTP ...

  6. Netty是如何处理新连接接入事件的?

    更多技术分享可关注我 前言 前面的分析从Netty服务端启动过程入手,一路走到了Netty的心脏——NioEventLoop,又总结了Netty的异步API和设计原理,现在回到Netty服务端本身,看 ...

  7. 【Java】【设计模式 Design Pattern】单例模式 Singleton

    什么是设计模式? 设计模式是在大量的实践中总结和理论化之后的最佳的类设计结构,编程风格,和解决问题的方式 设计模式已经帮助我们想好了所有可能的设计问题,总结在这些各种各样的设计模式当中,也成为GOF2 ...

  8. Geber文件,装配图,BOM表的输出

    一.Geber文件的输出步骤: 注:选择需要导出的层 注:所指箭头的地方都多加个零,让输出有更大的空间来容纳 总结:这就是最终的Geber文件了 二.NC   Drill file的输出: 三.IPC ...

  9. Python 代码编辑器怎么选?PyCharm、VS Code、Jupyter Notebook 都各有特色

    Python 代码编辑器怎么选?PyCharm.VS Code.Jupyter Notebook 都各有特色,Jupyter 适合做数据分析这些需要可视化的操作,PyCharm 更适合做完整的 Pyt ...

  10. redis:key命令(二)

    设置一个key:set name hello 获取一个key的值:get name 查看所有的key:keys * 查看key是否存在:exists name 移动key到指定库:move name ...