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 ...
随机推荐
- python全栈 字符串,整数,bool 数据类型运用
python全栈 一.基本数据类型 1.int: 整数 2.str: 字符串, 一般不存放大量数据. 3.bool: 布尔值,用来判断. True 或者 False 4.list: 列表,用来存 ...
- Python complex() 函数
Python complex() 函数 Python 内置函数 描述 complex() 函数用于创建一个值为 real + imag * j 的复数或者转化一个字符串或数为复数.如果第一个参数为字 ...
- AD操作
加泪滴 批量添加覆铜过孔(先铺铜以后,再批量添加过孔) 开槽 在KEPP—OUT层 部分区域 不敷铜 开窗
- robotium—只有apk文件的测试
把ppt的内容总结一下,并把问题说明一下: 把基本步骤搞出来,用notepad的程序吧,直接安装apk,就当做是不知道源码就好啦! 直接用自己的手机上就行啦!! 一.重签名: 步骤: 1.直接运行re ...
- mysql 5.6 datetime 保存精确到秒
mysql中的CURRENT_TIMESTAMP和ON UPDATE CURRENT_TIMESTAMP 设置默认值 now(3) datetime 长度 3 保存精确到秒
- wk1&2 字符串
[CQ] 自增怎么样都增了,赋值不一定: int x = 2; int y = 2; int i = ++x; int j = y++; System.out.println(x); System.o ...
- 使用SqlBulkCopy批量插入数据,测试20万条用时5秒
using System;using System.Collections.Generic;using System.Linq;using System.Text; using System.Data ...
- ubuntu下安装php
一.下载PHP7的最新版源码 php7.0.9 下载地址 http://php.net/get/php-7.0.9.tar.gz/from/a/mirror 二.解压 tar -zxf /tmp/ph ...
- how2j网站前端项目——天猫前端(第一次)学习笔记8
其他页面的学习 这些页面有1.查询结果页 2.支付页面 3.支付成功页面 4.确认收货页面上 5.确认收货页面下 6.收获成功页面 7.评价页面上 8.评价页面下 9.登陆页面 10.注册页面 1.查 ...
- golang语言中os包的学习与使用(文件,目录,进程的操作)
os中一些常用函数的使用: package main; import ( "os" "fmt" "time" "strings&q ...