HDU 3999 The order of a Tree

Source: Internet
Author: User

Topic Connection

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

The order of a treedescription

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 and then the tree become a tree with
only one node;
2. Insert a key k to a nonempty tree, if K was less than the root, insert
It to the left Sub-tree;else inserts 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 WI Th the least lexicographic order that generate the same tree. The trees is the same if and only if they has the same shape.

Input

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

Output

One line with n Intergers, which is 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

Brush some water questions to pass the time.
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 usingstd::cin;Ten usingstd::cout; One usingStd::endl; A usingStd::find; - usingSTD::Set; - usingStd::map; the usingstd::p air; - usingstd::vector; - usingStd::multiset; - usingStd::multimap; + #defineSZ (c) (int) (c). Size () - #defineAll (c) (c). Begin (), (c). End () + #defineITER (c) Decltype ((c). Begin ()) A #defineCLS (arr,val) memset (arr,val,sizeof (arr)) at #defineCpresent (c, E) (Find (All (c), (e))! = (c). End ()) - #defineRep (i, n) for (int i = 0; i < (int) (n); i++) - #defineTR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) - #definePB (E) push_back (e) - #defineMP (A, b) Make_pair (A, B) - Const intMax_n =100100; intypedef unsignedLong Longull; - structNode { to     intv; +Node *ch[2]; -InlinevoidSETC (int_v, Node *p) { thev =_v; *ch[0] = ch[1] =p; $     }Panax Notoginseng }; - structBST { theNode *root, *NULL, *tail; + Node Stack[max_n]; AInlinevoidinit () { theTail = &stack[0]; +         NULL= tail++; -         NULL-&GT;SETC (0, NULL); $Root =NULL; $     } -Inline Node *newnode (intv) { -Node *p = tail++; theP->setc (V,NULL); -         returnp;Wuyi     } theInlinevoidInsert (Node *&x,intv) { -         if(x = =NULL) {x = NewNode (v);return; } WuInsert (X->ch[v > x->v], v); -     } AboutInlinevoidDFS (vector<int> &res, Node *x) { $         if(X! =NULL) { -RES.PB (x->v); -DFS (res, x->ch[0]); -DFS (res, x->ch[1]); A         } +     } theInlinevoidInsertintv) { - Insert (root, v); $     } theInlinevoidGo () { thevector<int>Res; the Dfs (res, root); the         intn =sz (res); -Rep (i, N) printf ("%d%c", Res[i], I < n-1?' ':'\ n'); in     } the }bst; the intMain () { About #ifdef LOCAL theFreopen ("In.txt","R", stdin); theFreopen ("OUT.txt","w+", stdout); the #endif +     intN, v; -      while(~SCANF ("%d", &N)) { the bst.init ();BayiRep (i, N) scanf ("%d", &v), Bst.insert (v); the bst.go (); the     } -     return 0; -}
View Code

HDU 3999 The order of a Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.