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 ...
随机推荐
- shell重定向命令执行顺序
重定向内容介绍 一条shell命令的执行包含三个文件描述符:标准输入(键盘等) stdin 0,标准正确输出(屏幕等) stdout 1,标准错误输出(屏幕等)stderr 2 通过重定向可以指定 ...
- centos 7 下 cobbler 安装
一.cobbler 介绍: Cobbler 是一个系统启动服务(boot server),可以通过网络启动(PXE)的方式用来快速安装.重装物理服务器和虚拟机,支持安装不同的 Linux 发行版和 W ...
- [剑指offer]51-数组中的逆序对(归并排序)
题目链接 https://www.nowcoder.com/questionTerminal/96bd6684e04a44eb80e6a68efc0ec6c5 题意 在数组中的两个数字,如果前面一个数 ...
- Redis能干啥?细看11种Web应用场景[转]
下面列出11种Web应用场景,在这些场景下可以充分的利用Redis的特性,大大提高效率. 1.在主页中显示最新的项目列表. Redis使用的是常驻内存的缓存,速度非常快.LPUSH用来插入一个内容ID ...
- 使用BulkCopy报错 从 bcp 客户端收到一个对 colid 19 无效的列长度
====System.Data.SqlClient.SqlException: 从 bcp 客户端收到一个对 colid 19 无效的列长度. 从0开始数,数据库上表的第19列
- redis集群 与spring-data-redis 集成
所遇到的坑:必须使用如下的jedis 版本与spring-data-redis 版本,才能够达到集群效果 .1.7版本以前是不支持集群的 <dependency> <groupId& ...
- Java面试基础知识(1)
1.final, finally, finalize的区别 final:修饰符(关键字)如果一个类被声明为final,没有子类也不能被继承.因此一个类不能既被声明为 abstract的,又被声明为fi ...
- MyBatis代理开发(2)
一.开发步骤和程序代码 1.程序员需要编写user.xml映射文件,程序员编写mapper接口需要遵循一些开发规范,mybatis可以自动生成mapper接口实现类代理对象. 2.创建数据库配置文件 ...
- elastic5.4安装错误解决
首先,我们从官网下载:(官网:https://www.elastic.co/downloads/elasticsearch)(推荐下载deb或者rpm包,否则坑很多) 启动 (需要依赖java环境) ...
- ContenteProvider
以前只写过程序中添加背景音乐,在程序一开始就运行音乐,当程序结束后音乐也随即停止.遇到这样的功能,我们一般是通过系统提供的ContentProvider来实现的,系统对于常用的数据也给开发者提供了方便 ...