石家庄设计网站公司,小程序制作的方法有哪些,全自动引流推广软件免费,网络工具传送门 文章目录题意#xff1a;思路#xff1a;题意#xff1a;
给你n,dn,dn,d#xff0c;让你构造有nnn个点的二叉树#xff0c;他们每个节点深度和为ddd。 n,d≤3000n,d\le 3000n,d≤3000.
思路#xff1a;
先考虑不能构造出来的情况#xff0c;设sumsumsum为最小…传送门
文章目录题意思路题意
给你n,dn,dn,d让你构造有nnn个点的二叉树他们每个节点深度和为ddd。 n,d≤3000n,d\le 3000n,d≤3000.
思路
先考虑不能构造出来的情况设sumsumsum为最小的深度和那么sumdsumdsumd或n∗(n−1)2d\frac{n*(n-1)}{2}d2n∗(n−1)d的时候无解。 考虑构造的深度最小的情况我们让每个点iii的父亲等于⌊i2⌋\left \lfloor \frac{i}{2} \right \rfloor⌊2i⌋即可这样深度最小我们先让dd−sumdd-sumdd−sum。记最长链的最下端的编号为ppp那么当我们depth[p]−depth[i]1ddepth[p]-depth[i]1ddepth[p]−depth[i]1d的时候我们直接让iii跑到ppp的下面让后将ppp更新为iii就好了。如果depth[p]−depth[i]1ddepth[p]-depth[i]1ddepth[p]−depth[i]1d那么说明如果跳到这个点深度就多了那么我们就向上找ppp的父亲每跳一次就让d−−d--d−−直到ddd为000将iii放到跳到的点的下面就好了。
// Problem: E. Construct the Binary Tree
// Contest: Codeforces - Codeforces Round #624 (Div. 3)
// URL: https://codeforces.com/contest/1311/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize(Ofast,no-stack-protector,unroll-loops,fast-math)
//#pragma GCC target(sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tunenative)
//#pragma GCC optimize(2)
#includecstdio
#includeiostream
#includestring
#includecstring
#includemap
#includecmath
#includecctype
#includevector
#includeset
#includequeue
#includealgorithm
#includesstream
#includectime
#includecstdlib
#define X first
#define Y second
#define L (u1)
#define R (u1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].ltr[u].r1)
#define Len(u) (tr[u].r-tr[u].l1)
#define random(a,b) ((a)rand()%((b)-(a)1))
#define db puts(---)
using namespace std;//void rd_cre() { freopen(d://dp//data.txt,w,stdout); srand(time(NULL)); }
//void rd_ac() { freopen(d://dp//data.txt,r,stdin); freopen(d://dp//AC.txt,w,stdout); }
//void rd_wa() { freopen(d://dp//data.txt,r,stdin); freopen(d://dp//WA.txt,w,stdout); }typedef long long LL;
typedef unsigned long long ULL;
typedef pairint,int PII;const int N1000010,mod1e97,INF0x3f3f3f3f;
const double eps1e-6;int n,d;
int depth[N],fa[N];int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);int _; scanf(%d,_);while(_--){scanf(%d%d,n,d);int sum0; depth[0]-1; int root1;for(int i1;in;i) {if((i(i-1))0) rooti;depth[i]depth[i/2]1,fa[i]i/2,sumdepth[i];}if(sumd||dn*(n-1)/2) { puts(NO); continue; }d-sum;if(!d){puts(YES);for(int i2;in;i) printf(%d ,fa[i]);puts();continue;}for(int in;i1;i--){if((i(i-1))0) continue;int cntdepth[root]-depth[i]1;if(cntd){d-cnt;depth[i]depth[root]1;fa[i]root;rooti;}else {dcnt-d;while(droot!1) rootfa[root],d--;fa[i]root; depth[i]depth[root]1;puts(YES);for(int i2;in;i) printf(%d ,fa[i]);puts();break;}} }return 0;
}
/**/