zoukankan      html  css  js  c++  java
  • hdu 3999 The order of a Tree

    题目连接

    http://acm.hdu.edu.cn/showproblem.php?pid=3999

    The order of a Tree

    Description

    As we know,the shape of a binary search tree is greatly related to the order of keys we insert. To be precisely:
    1.  insert a key k to a empty tree, then the tree become a tree with
    only one node;
    2.  insert a key k to a nonempty tree, if k is less than the root ,insert
    it to the left sub-tree;else insert k to the right sub-tree.
    We call the order of keys we insert “the order of a tree”,your task is,given a oder of a tree, find the order of a tree with the least lexicographic order that generate the same tree.Two trees are the same if and only if they have the same shape.

    Input

    There are multiple test cases in an input file. The first line of each testcase is an integer $n(n leq 100,000)$,represent the number of nodes.The second line has n intergers,k1 to kn,represent the order of a tree.To make if more simple, k1 to kn is a sequence of 1 to n.

    Output

    One line with n intergers, which are the order of a tree that generate the same tree with the least lexicographic.

    SampleInput

    4
    1 3 4 2

    SampleOutput

    1 3 2 4

    刷些水题打发时间。。

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<vector>
     7 #include<map>
     8 #include<set>
     9 using std::cin;
    10 using std::cout;
    11 using std::endl;
    12 using std::find;
    13 using std::set;
    14 using std::map;
    15 using std::pair;
    16 using std::vector;
    17 using std::multiset;
    18 using std::multimap;
    19 #define sz(c) (int)(c).size()
    20 #define all(c) (c).begin(), (c).end()
    21 #define iter(c) decltype((c).begin())
    22 #define cls(arr,val) memset(arr,val,sizeof(arr))
    23 #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    24 #define rep(i, n) for (int i = 0; i < (int)(n); i++)
    25 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
    26 #define pb(e) push_back(e)
    27 #define mp(a, b) make_pair(a, b)
    28 const int Max_N = 100100;
    29 typedef unsigned long long ull;
    30 struct Node {
    31     int v;
    32     Node *ch[2];
    33     inline void setc(int _v, Node *p) {
    34         v = _v;
    35         ch[0] = ch[1] = p;
    36     }
    37 };
    38 struct BST {
    39     Node *root, *null, *tail;
    40     Node stack[Max_N];
    41     inline void init() {
    42         tail = &stack[0];
    43         null = tail++;
    44         null->setc(0, NULL);
    45         root = null;
    46     }
    47     inline Node *newNode(int v) {
    48         Node *p = tail++;
    49         p->setc(v, null);
    50         return p;
    51     }
    52     inline void insert(Node *&x, int v) {
    53         if (x == null) { x = newNode(v); return; }
    54         insert(x->ch[v > x->v], v);
    55     }
    56     inline void dfs(vector<int> &res, Node *x) {
    57         if (x != null) {
    58             res.pb(x->v);
    59             dfs(res, x->ch[0]);
    60             dfs(res, x->ch[1]);
    61         }
    62     }
    63     inline void insert(int v) {
    64         insert(root, v);
    65     }
    66     inline void go() {
    67         vector<int> res;
    68         dfs(res, root);
    69         int n = sz(res);
    70         rep(i, n) printf("%d%c", res[i], i < n - 1 ? ' ' : '
    ');
    71     }
    72 }bst;
    73 int main() {
    74 #ifdef LOCAL
    75     freopen("in.txt", "r", stdin);
    76     freopen("out.txt", "w+", stdout);
    77 #endif
    78     int n, v;
    79     while (~scanf("%d", &n)) {
    80         bst.init();
    81         rep(i, n) scanf("%d", &v), bst.insert(v);
    82         bst.go();
    83     }
    84     return 0;
    85 }
    View Code
    By: GadyPu 博客地址:http://www.cnblogs.com/GadyPu/ 转载请说明
  • 相关阅读:
    《Erlang程序设计》 第六章 编译并运行程序
    《Erlang程序设计》第二章 入门
    《Erlang程序设计》第一章 引言
    《Erlang程序设计》第四章 异常
    animation的控件动画效果
    各种自定义动画效果
    loding等待时的一些效果
    逐渐显示的按钮和图片上下切换
    Android的系统架构
    【转】overridePendingTransition 动画切换效果
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4572796.html
Copyright © 2011-2022 走看看