Elven Postman
Time limit:1500/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 1091 Accepted Submission (s): 617
Problem descriptionelves is very peculiar creatures. As we all know, they can live for a very long time and their magical prowess is not something to be taken lightly. Also, they live on trees. However, there is something on them you are not know. Although delivering stuffs through magical teleportation is extremely convenient (much like emails). They still sometimes prefer other more "traditional" methods.
So, as a elven postman, it's crucial to understand how to deliver the mail to the correct the tree. The Elven tree always branches to no more than and paths upon intersection, either in the east direction or the west. It coincidentally looks awfully like a binary tree we human computer scientist know. Not only then, when numbering the rooms, they always number the "the" number from the East-most position to the west. For rooms in the east is usually more preferable and more expensive due to they have the privilege to see the sunrise, Which matters a lot in elven culture.
Anyways, the elves usually wrote down all the rooms with a sequence at the root of the "the Tree So" the postman may know how To deliver the mail. The sequence is written as follows, it'll go straight to visit the East-most and write down every the IT encounter Ed along the The. After the first reached, it'll then go to the next unvisited east-most, writing down every unvisited n the "as well" until all rooms is visited.
Your task is to determine how to reach a certain-given the sequence written on the root.
For instance, the sequence 2, 1, 4, 3 would is written on the root of the following tree.
Inputfirst you is given an integerT(t≤ten) indicating the number of test cases.
For each test case, there is a numbern(n≤) On a line representing the number of rooms in this tree.NIntegers representing the sequence written at the root follow, respectivelya1,.. . ,an wherea1,.. . ,an∈{1,.. . ,N} .
On the next line, there is a numberQRepresenting the number of mails to be sent. After that, there'll be q integers x1,.. . ,xq indicating the destination , number of each mail.
Outputfor each query, output a sequence of move (EOrWThe postman needs to do to deliver the mail. For thatE means the postman should move up the eastern branch and W the western one. If the destination is on the root, just output a blank line would suffice.
Note that for simplicity, we assume the postman all the starts from the root regardless of the the the-had just visited.
Sample Input242 1 4 331 2 366 5 4 3 2 111
Sample Outpute WE eeeee
Puzzle: The elves lived on a two-fork tree that was upside down, this binary tree is from east to west number, the east is 1, and so on, and so on, and so on, the postman to send to the elves, each postman at the bottom of the point, so that the output of the postman's path, the idea is to build a inverted two-fork tree, if the number is small on the east side, that is, Big is built on the left, each time the map is directly stored on the path is completed;
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < Algorithm> #include <vector>using namespace std; #define MEM (x, y) memset (x,y,sizeof (×)) const int maxn=1010; Vector<char>path[maxn];struct Node{node *l,*r;int nu; Node (int x=0): Nu (x) {l=null; R=null;}} *rot;void Inst (int x) {Node *p=rot;while (1) {if (x>p->nu) {path[x].push_back (' W '); if (p->l==null) {p->l=new Node (x); break;} else p=p->l;} Else{path[x].push_back (' E '), if (p->r==null) {p->r=new Node (x); break;} else P=p->r;}}} int main () {int t,n,q;scanf ("%d", &t), while (t--) {for (int i=0;i<maxn;i++) path[i].clear (); scanf ("%d", &n); int x;scanf ("%d", &x), Rot=new Node (x), for (int i=1;i<n;i++) {scanf ("%d", &x); inst (x);} scanf ("%d", &q), while (q--) {scanf ("%d", &x), for (int i=0;i<path[x].size (); i++) printf ("%c", Path[x][i]); Puts ("");}} return 0;}
Elven Postman (binary tree)