题目链接:uva 10821 - Constructing BST
题目大意:给定节点个数以及树的高度,求一个字典序最小的插入顺序,使得生成的BST高度为H。
解题思路:根据H来确定说左右子树的节点个数,因为要求字典序尽量小,所以右子树的节点个数应该尽量多。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int N, H;
void solve (int l, int r, int h) {
if (l > r)
return;
int mid = max(r - (1<<h) + 1, l);
printf(" %d", mid);
solve(l, mid - 1, h - 1);
solve(mid + 1, r, h - 1);
}
int main () {
int cas = 0;
while (scanf("%d%d", &N, &H) == 2 && N + H) {
printf("Case %d:", ++cas);
if (N > (1<<H) - 1)
printf(" Impossible.");
else
solve(1, N, H - 1);
printf("\n");
}
return 0;
}
分享到:
相关推荐
Channel_Polarization_A_Method_for_Constructing_Capacity-Achieving_Codes_for_Symmetric_Binary-Input_Memoryless_Channels.pdf
基于自构式反馈模糊神经网络的永磁同步直线电机控制器设计,张茹,,自构式反馈模糊神经网络控制器(SCFFNNC)旨在针对永磁直线同步电动机参数不确定性导致的伺服服系统动态性能变差的问题。根据误差增加
Chapter 4 - Constructing Block Ports Chapter 5 - Constructing Connections Chapter 6 - Moving Blocks and Connections Chapter 7 - Automatic Block Placement Chapter 8 - Connection-Based Bend Points ...
HCNP-CUSN Huawei Certified Network Professional - Constructing Unifying Storage Network认证考试题库
H13-623 - Huawei Certified Network Professional - Constructing Data Protection System - HCNP-CDPS认证考试题库
HCNP-Storage-CBDS (Huawei Certified Network Professional - Constructing Big Data Solution)认证考试题库
We aim to provide more flexibility by de-constructing the radio pipeline into a framework of user controlled kernels that can be reconfigured at run-time. This architecture provides the barebones of a...
HCNP Security CISN (Huawei Certified Network Professional - Constructing Infrastructure of Security Network)认证考试题库
An Embedding-based Approach to Constructing OWL ontologies
(7)De-Constructing Accordion and Hover Effects with jQuery 8 (8)导航到页内指定位置 8 (10) Sexy Drop Down Menu w/ jQuery & CSS 9 (11) A Different Top Navigation 9 (12)Sliding Jquery Menu ...
Cheating prevention is an important cryptographic problem in secret-sharing schemes.... Then a method for constructing new 1-cheating immune secret-sharing functions from known ones is given.
Constructing Compressed Suffix Arrays with Large Alphabets 生物信息论文
NSV-GUARD: Constructing Secure Routing Paths in Software Defined Networking
Constructing identities in Indian networks
Bell多项式方法在构造非线性发展方程守恒率中的应用研究,李敏,柳长静,本文将Bell多项式方法应用于研究具有二元双线性形式的非线性发展方程的无穷多守恒率。目前,Bell多项式方法已经被应用于获得具有一元
知识图谱建立全过程,kdd会议google讲义,全面!权威!
Constructing IGA-suitable planar parameterization from complex CAD boundary by domain partition and global/local optimization
Apache Beam is a unified model for defining both batch and streaming data-parallel processing pipelines, as well as a set of language-specific SDKs for constructing pipelines and Runners for executing...
Constructing a Surrogate_代理模型.zip