HDOJ 5338 ZZX and Permutations line segment tree + tree Array
[Question]:
Add parentheses to an arrangement and ask how to maximize the Lexicographic Order of 1 to n.
From 1, greedy each number can go to three places, the first one on the right, jump to one on the left, and form a loop with yourself.
For the first one on the right, you only need to judge whether the one on the right is occupied. If it can be exchanged with the one on the right, you need to set the number on the right to 0 in the online segment tree.
Jump to one on the left. If a number jumps to one on the left, it indicates that the left is the one starting with the bracket and ending with the bracket. query the maximum value in the interval using a line segment tree, because brackets cannot overlap, you need to use a tree array to split the left boundary. if this number is to jump to a certain number on the left, + 1 will be placed on the right brackets in the tree array, indicating that there is already a complete bracket.
In the case of 2 1 3 (2) (1 3), the maximum Lexicographic Order is obtained, so a number can jump to its own
ZZX and Permutations
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission (s): 775 Accepted Submission (s): 244
Problem Description ZZX likes permutations.
ZZX knows that a permutation can be decomposed into disjoint cycles (see https://en.wikipedia.org/wiki/Permutation#Cycle_notation). For example:
145632 = (1) (35) (462) = (462) (1) (35) = (35) (1) (462) = (246) (1) (53) = (624) (1) (53 )......
Note that there are always ways to rewrite it, but they are all equivalent.
A cycle with only one element is also written in the decomposition, like (1) in the example above.
Now, we remove all the parentheses in the decomposition. So the decomposition of 145632 can be 135462,463695, 351462,246153, 624153 ......
Now you are given the decomposition of a permutation after removing all the parentheses (itself is also a permutation ). you shoshould recover the original permutation. there are always ways to recover, so you should find the one with largest lexicographic order.
Input First line contains an integer T , The number of test cases.
Then T Testcases follow. In each testcase:
First line contains an integer N , The size of the permutation.
Second line contains N Space-separated integers, the decomposition after removing parentheses.
N ≤ 105 . There are 10 testcases satisfying N ≤ 105 , 200 testcases satisfying N ≤ 1000 .
Output N Space-separated numbers in a line for each testcase.
Don't output space after the last number of a line.
Sample Input
261 4 5 6 3 221 2
Sample Output
4 6 2 5 1 32 1
Author XJZX
Source 2015 Multi-University Training Contest 4
/*************************************** * ******** Author: CKbossCreated Time:, Monday, January 1, August 03, 2015 File Name: HDOJ5338.cpp *************************************** * *********/# include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; const int maxn = 100100; int n; int nb [maxn], wz [maxn]; int sed [maxn <2]; int mx [maxn <2]; # define lrt rt <1 # define rrt rt <1 | 1 # define lson l, m, lrt # define rson m + 1, r, rrtvoid push_up (int rt) {mx [rt] = max (mx [lrt], mx [rrt]);} void push_down (int rt) {if (sed [rt]) {mx [rrt] = mx [lrt] = 0; sed [rrt] = sed [lrt] = 0; sed [rt] = 0 ;}} void build (int l, int r, int rt) {sed [rt] = mx [rt] = 0; if (l = r) {mx [rt] = nb [l]; return;} int m = (l + r)/2; build (lson ); build (rson); push_up (rt);} int query (int L, int R, int l, int r, int rt) {if (L <= l & r <= R) return mx [rt]; push_down (rt); int m = (l + r)/2; int ret = 0; if (L <= m) ret = max (ret, query (L, R, lson); if (R> m) ret = max (ret, query (L, R, rson); return ret;} void update (int L, int R, int l, int r, int rt) {if (L <= l & r <= R) {sed [rt] = 1; mx [rt] = 0; return;} push_down (rt ); int m = (l + r)/2; if (L <= m) update (L, R, lson); if (R> m) update (L, R, rson); push_up (rt);} void show (int l, int r, int rt) {printf (% d: % d <----> % d mx: % d, rt, l, r, mx [rt]); if (l = r) return; int m = (l + r)/2; show (lson ); show (rson);} int ans [maxn]; bool used [maxn]; int tree [maxn]; inline int lowbit (int x) {return x & (-x);} void add (int p, int v = 1) {for (int I = p; I
Self | right> self) {if (right> left) {ans [I] = right; used [wz [right] = true; update (wz [right], wz [right], 1, n, 1) ;}else {ans [I] = left; // link circlefor (int j = wz [left]; j