BST: binary search tree.

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=20&page=show_problem&problem=1762

题目中要求构造的BST的高度最多为H,也就是说可以比H小(之前没注意到这点)。如果有若干顺序满足高度为H的BST,取小数字排在前面的顺序,所以在构造BST的过程中,尽可能使right sub-tree填满。数字排在right sub-tree最多可以有pow(2, height-1)个,所以当前root是max(left, right-pow(2, height-1))。递归解决所有的sub-tree。

代码如下:

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <cmath>
#define SCF(a) scanf("%d", &a)
#define IN(a) cin>>a
#define FOR(i, a, b) for(int i=a;i<b;i++)
typedef long long Int;
using namespace std; void order(int start, int end, int height)
{
if (start > end)
return;
if (height == 0)
printf(" %d", start);
else
{
int n = pow(2, height - 1) - 1;
int num = end - start + 1;
if (num > n)
{
int root = end - n;
printf(" %d", root);
order(start, root - 1, height - 1);
order(root + 1, end, height - 1);
}
else
{
printf(" %d", start);
order(start+1, end, height - 1);
}
} } int main()
{
int N, H;
int t = 1;
while (SCF(N) && SCF(H))
{
if (N == 0 && H == 0)
break;
if (H < floor(log2(N) + 1))
printf("Case %d: Impossible.\n", t++);
else
{
printf("Case %d:", t++);
if (H > N)
H = N;
order(1, N, H);
printf("\n");
}
}
return 0;
}

  

UVA 10821 Constructing BST的更多相关文章

  1. UVA 1264 - Binary Search Tree(BST+计数)

    UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...

  2. uva 11020 - Efficient Solutions ——平衡BST

    链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...

  3. UVa 11020 Efficient Solutions (BST)

    题意:给按顺序给定 n 个人群,用x和y来描述,如果有没有任何一个x' < x y' <= y 或 x '<= x y' <= y,那么这个群体就是优势群体, 让你求出每放入一 ...

  4. UVA 11020 Efficient Solutions (BST,Splay树)

    题意:给n个坐标.一个坐标(x,y)若有无存在的坐标满足x1<x && y1<=y  或  x1<=x && y1<y 时,此坐标(x,y)是就 ...

  5. UVa 109 - SCUD Busters(凸包计算)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  6. Kth Smallest Element in a BST 解答

    Question Given a binary search tree, write a function kthSmallest to find the kth smallest element i ...

  7. [算法专题] BST&AVL&RB-Tree

    BST 以下BST的定义来自于Wikipedia: Binary Search Tree, is a node-based binary tree data structure which has t ...

  8. UVA 12345 Dynamic len(set(a[LR]))

    题意:询问区间唯一元素个数,单点修改. 分析: 借助Unique snowflakes, Can you answer these queries II的思想,唯一性可以借助元素上一次出现的位置来判断 ...

  9. [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

随机推荐

  1. springboot找不到主类

    在学习springboot的时候,前几天写了一个demo,正常运行,一点问题也没有.今天运行不起来了. 报错:找不到主类 解决方案: Project->Clean->选中项目,点击Clea ...

  2. nodejs中.npmrc文件的内容

    . nodejs安装后,使用npm安装模块的时候我出现了一个错误. getaddrinfo ENOTFOUND xxx 主要是这个配置文件的问题.搞不清楚.直接打开把文件内容删除变成 npmrc文件内 ...

  3. Unity3D无限奔跑者Playmaker v1.5

    这款功能丰富的移动准备包内含使您自己的各种各样的3D游戏运行的一切要素.无需编程! 功能 1 - 响应式滑动控制2 - 投掷炮弹3 - 可破坏物体4 - 能力提升道具- 金币磁铁- 护盾- 继续密钥- ...

  4. 将图片转换为Base64编码的字符串

    图片以文件的形式存在,可以在表单中使用. 也可以转换成Base64编码的字符串,从而在css.js中以字符串的形式使用图片.例如,在css中设置背景图片.在js中用ajax上传图片. <!DOC ...

  5. Chrome格式化JavaScript代码

    很多第三方插件的脚本,是压缩后的代码,甚至时动态加载的,代码只有一行. Chrome提供了格式化脚本代码的功能,方便加断点调试. 1 在Sources面板中,点击脚本名称,打开脚本源码. 2 点击左下 ...

  6. How to set the bash display to not show the vim text after exit?

    Xshell客户端在vim编辑文件保存退出,仍然显示文本内容,而不是回到shell terminal终端. 解决办法如下: User1 is using TERM=xterm, in this cas ...

  7. c#: 模态窗口最小化主窗口

    起源: 产品中,通常有些耗时操作比如视频转换.DVD刻录等,在模态窗口中执行.此时最小化它,主窗体不能跟着最小化,影响操作体验. 如何让主窗体最小化,并且可以还原呢?搜索一番,未找到满意结果,自己动手 ...

  8. Django的restframework的序列化组件之对单条数据的处理

    之前我们学习的都是处理书籍或者出版社的所有的数据的方法,下面我们来看下处理单个书籍,或者单个出版社的方法 这个时候我们就需要重新写一个类,这个类的方法,就需要有3个参数,参数1是self,参数2是re ...

  9. 第五章 Inheritance继承

    [继承] Java不支持多重继承 - 每个子类只有一个超类. 不是将成员变量声明为静态,更好的做法是将University实例化为对象,然后使用该对象访问其成员,如下所示: [抽象类] 可以包含或者不 ...

  10. swift - 启动APP 黑屏

    https://blog.csdn.net/chengkaizone/article/details/50478045