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

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

.

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

  1. CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)

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

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

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

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

随机推荐

  1. 从996到ICU——我们终将被自我厌恶

    自从996.icu在github上收获了18万点赞以来,超时工作,超负荷劳动似乎成了天下所有“无产阶级者”的共同点.对于这样的剥削制度,我想再多的批评和抨击都不为过.但是今天我想说3个小故事,只为引起 ...

  2. 算法笔记--st表

    概述:用倍增法求区间最值的离线算法,O(nlogn)预处理,O(1)访问. 预处理: 状态:st[i][j]:[i,i+2^j)之间的最值 状态转移:如果j等于0,st[i][j]=a[i] 如果j大 ...

  3. centos7: svbversion版本的安装配置+tortoisesvn登录验证

    centos7: svbversion版本的安装配置+tortoisesvn登录验证 命令工具:svnadmin create #创建版本库 hotcopy #版本库热备份 Islocks #打印所有 ...

  4. 雷林鹏分享:Ruby 安装 - Unix

    Ruby 安装 - Unix 下面列出了在 Unix 机器上安装 Ruby 的步骤. 注意:在安装之前,请确保您有 root 权限. 下载最新版的 Ruby 压缩文件.请点击这里下载. 下载 Ruby ...

  5. webService开发(JDK版)

    最近做社保查询的东西,然而这个是三个公司一起做的,需要调其他公司的接口,他们公司用了webService这个当年比较流行的技术,于是乎就研究了一下这个webService. HTTP协议 + XML方 ...

  6. 54 Django 模型层(1) 单表查询

    单表操作: 一 项目的操作顺序: 1 在model.py文件中创建表结构 class Book(models.Model): id=models.AutoField(primary_key=True) ...

  7. vue新手入门指导,一篇让你学会vue技术栈,本人初学时候的文档

    今天整理文档突然发现了一份md文档,打开一看 瞬间想起当年学习vue的艰难路,没人指导全靠自己蒙,下面就是md文档内容,需要的小伙伴可以打开个在线的md编译器看一看,我相信不管是新人还是老人  入门总 ...

  8. nyoj-833-博弈

    833-取石子(七) 内存限制:64MB 时间限制:1000ms 特判: No通过数:16 提交数:30 难度:1 题目描述: Yougth和Hrdv玩一个游戏,拿出n个石子摆成一圈,Yougth和H ...

  9. zzuli1985(dp/水dfs郑轻比赛)

    再一次感受到dp的威力 1985: 即将到来的新生赛 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 112  Solved: 28 SubmitStat ...

  10. 联想笔记本V470安装Win8.1 X64位系统,关机黑屏、电源灯亮

    以前的WIN7 X86系统用了很长时间了,软件业装了很多,现在使用的时候就有点卡了,最近决定重装个系统,后台发现开发的有一个东西要求WIN8 X64位的,就下载了一个准备直接安装了,也从此开始了整整2 ...