UVA 10821 Constructing BST
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的更多相关文章
- UVA 1264 - Binary Search Tree(BST+计数)
UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...
- uva 11020 - Efficient Solutions ——平衡BST
链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...
- UVa 11020 Efficient Solutions (BST)
题意:给按顺序给定 n 个人群,用x和y来描述,如果有没有任何一个x' < x y' <= y 或 x '<= x y' <= y,那么这个群体就是优势群体, 让你求出每放入一 ...
- UVA 11020 Efficient Solutions (BST,Splay树)
题意:给n个坐标.一个坐标(x,y)若有无存在的坐标满足x1<x && y1<=y 或 x1<=x && y1<y 时,此坐标(x,y)是就 ...
- UVa 109 - SCUD Busters(凸包计算)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- Kth Smallest Element in a BST 解答
Question Given a binary search tree, write a function kthSmallest to find the kth smallest element i ...
- [算法专题] BST&AVL&RB-Tree
BST 以下BST的定义来自于Wikipedia: Binary Search Tree, is a node-based binary tree data structure which has t ...
- UVA 12345 Dynamic len(set(a[LR]))
题意:询问区间唯一元素个数,单点修改. 分析: 借助Unique snowflakes, Can you answer these queries II的思想,唯一性可以借助元素上一次出现的位置来判断 ...
- [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 ...
随机推荐
- STL::forward_list
forward_list(c++11): 内部是一个单链表的实现:但是为了效率的考虑,故意没有 size 这个内置函数. Constructor 六种构造方式default; fill; range; ...
- oracle 中decode函数用法
学习记录: 含义解释: decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译值1)ELSIF 条件=值2 ...
- C# 页面向controller中跳转匹配方法的时候,当controller中有两个重载方法时候,不发生跳转
在ajax中的URL跳向controller一个方法时候,controller中有两个重载的方法,ajax不发生跳转,当删除另外一个方法之后,正常跳转. 不知道,是我自己写的有问题,还是control ...
- Android模拟器故障:waiting for target deviceto come online
关闭再打开模拟器.删除再新建模拟器均无效. 解决办法:在AVD Manager中,选择立即冷启动(Cold Boot Now)模拟器.
- 24 【python入门指南】class
一.类 1.1,构造函数,析构函数 #!/bin/python class dog(): def __init__(self, age, name): self.age = age self.name ...
- 【linux】常见问题&常用命令笔记
1.重启以及关机命令: Linux centos重启命令: (1)reboot 普通重启 (2)shutdown -r now 立刻重启(root用户使用) (3)shutdown -r 10 过 ...
- 运行wmic命令异常:java.io.IOException: Cannot run program "wmic": CreateProcess error=2, ϵͳÕҲ»µ½ָ¶解决记录
之前的一篇博文获取电脑cpu序列号在一同事电脑出现上述异常 百度一下网上只有一位外国网友朋友这个问题(地址),并且还没人回复,你能靠自己了 定位了一下出错代码: Process process = R ...
- sqlserver编号
select ROW_NUMBER() OVER (ORDER BY 字段 DESC) AS rid,* from 表名
- shell中颜色的设置
linux启动后环境变量加载的顺序为:etc/profile → /etc/profile.d/*.sh → ~/.bash_profile → ~/.bashrc → [/etc/bashrc] 想 ...
- ES6对象
--------------------------------------------------------------------- 对象的扩展 let obj = {a: 1, b: 2, c ...