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 ? ‘ ‘ : ‘\n‘);
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 }

时间: 2024-12-23 07:33:56

hdu 3999 The order of a Tree的相关文章

hdu 3999 The order of a Tree (二叉搜索树)

1 /****************************************************************** 2 题目: The order of a Tree(hdu 3999) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=3999 4 题意: 给你一个序列建立一棵二叉搜索树 要你找出另外一个序 5 列,可以建立和原序列建立的二叉搜索树一样且这个序列 6 是字典序最小 7 算法: 二叉搜索树 8 思想: 对于一个

HDU 3999 The order of a Tree 二叉树

#include <iostream> #include <cstdlib> #include <string.h> #include <algorithm> #define ss(a) scanf("%d",&a) #define ss64(a) scanf("%I64d",&a) using namespace std; typedef struct Node { struct Node *l; s

hdu3999-The order of a Tree (二叉树的先序遍历)

http://acm.hdu.edu.cn/showproblem.php?pid=3999 The order of a Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1361    Accepted Submission(s): 695 Problem Description As we know,the shape of

HDU 4718 The LCIS on the Tree(树链剖分)

Problem Description For a sequence S1, S2, ... , SN, and a pair of integers (i, j), if 1 <= i <= j <= N and Si < Si+1 < Si+2 < ... < Sj-1 < Sj , then the sequence Si, Si+1, ... , Sj is a CIS(Continuous Increasing Subsequence). The

HDU 3999 二叉排序树

The order of a Tree Problem Description 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 no

HDU 4836 The Query on the Tree lca || 欧拉序列 || 动态树

lca的做法还是很明显的,简单粗暴, 不过不是正解,如果树是长链就会跪,直接变成O(n).. 最后跑的也挺快,出题人还是挺阳光的.. 动态树的解法也是听别人说能ac的,估计就是放在splay上剖分一下,做法还是比较复杂的,,, 来一发lca: #include <stdio.h> #include <iostream> #include <algorithm> #include <sstream> #include <stdlib.h> #inc

hdu 4670 Cube number on a tree(点分治)

Cube number on a tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1628    Accepted Submission(s): 382 Problem Description The country Tom living in is famous for traveling. Every year, man

HDU 1166 敌兵布阵 Segment Tree题解

本题是最基本的分段树操作了.或者一般叫线段树,不过好像和线段没什么关系,只是分段了. 不使用lazy标志,更新只是更新单点. 如果不使用分段树,那么更新时间效率只需要O(1),使用分段树更新效率就需要O(lgn)了. 但是不是用分段树,那么查询的时间效率是O(n),而分段树查询效率是O(lgn) 这就是amortize分摊了时间,而且lgn真的很快,数据不是非常巨大的时候,接近常数了. 故此本题需要使用分段树. #include <cstdio> class EnemyInfo { stati

HDU 1394 Minimum Inversion Number Segment Tree解法

本题有两个考点: 1 求逆序数的性质 计算逆序数的公式, 一个数arr[i]从前面放到后面,必然会有n-arr[i]-1个数比这个大,那么就有n-arr[i]-1个逆序数增加,同时因为前面少了个arr[i]数,那么就必然有arr[i]个(加上零)数比起小的数失去一个逆序数,总共失去arr[i]个逆序数,所以新的逆序数为增加了n-arr[i]-1-arr[i]个逆序数(当然有可能是减小了,视arr[i]的值而定. 2 如何求一个数列的逆序数 可以使用归并排序来求,也可以使用线段树来求.两者都是二分