uva 10821 - Constructing BST(BSF)

题目链接: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;
}
时间: 2024-07-30 17:47:05

uva 10821 - Constructing BST(BSF)的相关文章

UVA 10821 - Constructing BST(贪心构造)

UVA 10821 - Constructing BST 题目链接 题意:有1 - n的数字,要构造一棵高度不超过h的BST,并且要字典序最小的,输出序列 思路:贪心构造,既然字典序最小,那么每个子树的根都要尽量小,那么也就是右子树尽量填满,按照这个策略去dfs构造即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n, h; void dfs

优质题表(机密版)

转载请注明出处:http://www.cnblogs.com/dashuzhilin/p/4556803.html 思维题: poj 1528 poj 1597 poj 2538 poj 2608 poj 2612 poj 2361 poj 2339 poj 2664 uva 10894 uva 10921   uva 10922   uva 10929 uva 10931   uva 10800   uva 10878 uva 10976   uva 10323   uva 201 poj 2

uva 1264 - Binary Search Tree(BST)

题目链接:uva 1264 - Binary Search Tree 题目大意:给定一个插入顺序,要求输出有多少种插入顺序,使得生成的BST一样. 解题思路:组合数学+BST的性质,起始左右两个子树的节点之间是没有影响的.所以逐层递推上去即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; const int max

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

UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树而言,仅仅要保证左边和右边顺序对就能够了,所以种数为C(左右结点总数,左结点),然后依据乘法原理乘上左右子树的情况就可以 代码: #include <cstdio> #include <cstring> typedef long long ll; const int MAXNODE =

UVA - 11020 - Efficient Solutions (multiset实现BST)

Efficient Solutions 题目传送:Efficient Solutions AC代码: #include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string>

Kth Smallest Element in a BST 解答

Question Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up What if the BST is modified (insert/delete operations) often and

向原文学习 &nbsp; 已添加字母说明 BST 排序二叉树

     /***************************    运行环境  http://www.anycodes.cn/zh/    原文件  http://www.cnblogs.com/hanxi/archive/2012/08/18/2645929.html    带注释的C++类版本 BST 二叉搜索树    ***************************/    #ifndef BTREE_H_ #define BTREE_H_   #include <cstdli

[算法专题] BST&amp;AVL

BST 以下BST的定义来自于Wikipedia: Binary Search Tree, is a node-based binary tree data structure which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d