code
stringlengths
0
672k
language
stringclasses
2 values
AST_depth
int64
-1
40
alphanumeric_fraction
float64
0
1
max_line_length
int64
0
672k
avg_line_length
float64
0
267k
num_lines
int64
0
4.06k
task
stringlengths
7
14k
source
stringclasses
2 values
INF = 10000000000.0 max_n = 50 max_k = 2000 def main(): (n, s, k) = map(int, input().split()) s -= 1 buf = [''] * (max_n + 1) dp = [[0 for i in range(max_n + 1)] for j in range(max_k + 1)] r = list(map(int, input().split())) c = input() answer = INF for i in range(len(c)): buf[i] = c[i] for i in range(k, -1, -1): for j in range(n): dp[i][j] = INF for j in range(n): value = abs(j - s) if k - r[j] <= 0: answer = min(answer, value) else: dp[k - r[j]][j] = value for i in range(k, 0, -1): for j in range(n): if dp[i][j] < INF: for l in range(n): if buf[j] != buf[l] and r[j] < r[l]: value = dp[i][j] + abs(j - l) if i - r[l] <= 0: answer = min(answer, value) else: dp[i - r[l]][l] = min(dp[i - r[l]][l], value) if answer == INF: print(-1) return print(answer) def __starting_point(): main() __starting_point()
python
24
0.500559
63
20.829268
41
There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values ​​— red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$). Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. -----Input----- The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) β€” number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) β€” numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. -----Output----- Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1". -----Examples----- Input 5 3 10 1 2 3 4 5 RGBRR Output 4 Input 2 1 15 5 6 RG Output -1 -----Note----- The sequence of actions of Tanya for the first example: move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. Since Tanya eats candy instantly, the required time is four seconds.
taco
(n, s, k) = map(int, input().split()) s -= 1 r = list(map(int, input().split())) INF = float('inf') c = input() dp = [[] for i in range(n)] def calc(u): if dp[u]: return dp[u] = [0] * (r[u] + 1) + [INF] * (k - r[u]) for i in range(n): if c[u] != c[i] and r[i] > r[u]: calc(i) d = abs(u - i) for j in range(r[u] + 1, k + 1): dp[u][j] = min(dp[u][j], dp[i][j - r[u]] + d) ans = INF for i in range(n): calc(i) ans = min(ans, abs(i - s) + dp[i][k]) if ans == INF: print(-1) else: print(ans)
python
17
0.472656
49
19.48
25
There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values ​​— red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$). Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. -----Input----- The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) β€” number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) β€” numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. -----Output----- Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1". -----Examples----- Input 5 3 10 1 2 3 4 5 RGBRR Output 4 Input 2 1 15 5 6 RG Output -1 -----Note----- The sequence of actions of Tanya for the first example: move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. Since Tanya eats candy instantly, the required time is four seconds.
taco
import math def solve(): (n, s, k) = map(int, input().split()) s -= 1 r = list(map(int, input().split())) c = input() inf = int(1000000000.0) dp = [[inf for j in range(n)] for i in range(k + 1)] for i in range(0, k + 1): for j in range(0, n): if i == 0 or i <= r[j]: dp[i][j] = 0 continue for K in range(0, n): if c[K] != c[j] and r[K] > r[j]: dp[i][j] = min(dp[i][j], dp[i - r[j]][K] + int(abs(K - j))) ans = min((dp[k][i] + abs(i - s) for i in range(0, n))) if ans >= inf: print(-1) return print(ans) return t = 1 while t > 0: t -= 1 solve()
python
21
0.495741
64
20.740741
27
There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values ​​— red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$). Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. -----Input----- The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) β€” number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) β€” numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. -----Output----- Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1". -----Examples----- Input 5 3 10 1 2 3 4 5 RGBRR Output 4 Input 2 1 15 5 6 RG Output -1 -----Note----- The sequence of actions of Tanya for the first example: move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. Since Tanya eats candy instantly, the required time is four seconds.
taco
INF = 100000 (n, s, k) = list(map(int, input().split())) r = list(map(int, input().split())) c = input().rstrip() dp = [[INF for j in range(k + 1)] for i in range(n)] s -= 1 for i in range(n): dp[i][k - r[i]] = abs(s - i) for j in range(k, -1, -1): for i in range(n): if dp[i][j] >= INF: continue for f in range(n): if r[f] <= r[i]: continue if c[f] == c[i]: continue new_val = max(0, j - r[f]) dp[f][new_val] = min(dp[f][new_val], dp[i][j] + abs(i - f)) ans = INF for i in range(n): ans = min(ans, dp[i][0]) if ans >= INF: ans = -1 print(ans)
python
15
0.516522
62
22
25
There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values ​​— red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$). Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. -----Input----- The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) β€” number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) β€” numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. -----Output----- Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1". -----Examples----- Input 5 3 10 1 2 3 4 5 RGBRR Output 4 Input 2 1 15 5 6 RG Output -1 -----Note----- The sequence of actions of Tanya for the first example: move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. Since Tanya eats candy instantly, the required time is four seconds.
taco
(n, s, k) = map(int, input().split()) r = list(map(int, input().split())) s -= 1 c = input() best = [[0 for i in range(n)] for j in range(k + 1)] for i in range(1, k + 1): for j in range(n): if i <= r[j]: best[i][j] = abs(j - s) else: good = float('inf') for l in range(n): if c[j] != c[l] and r[j] > r[l]: good = min(good, best[i - r[j]][l] + abs(j - l)) best[i][j] = good if min(best[-1]) == float('inf'): print(-1) else: print(min(best[-1]))
python
21
0.498943
53
23.894737
19
There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values ​​— red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$). Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. -----Input----- The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) β€” number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) β€” numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. -----Output----- Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1". -----Examples----- Input 5 3 10 1 2 3 4 5 RGBRR Output 4 Input 2 1 15 5 6 RG Output -1 -----Note----- The sequence of actions of Tanya for the first example: move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. Since Tanya eats candy instantly, the required time is four seconds.
taco
import sys sys.setrecursionlimit(1000) def rec(r, c, s, K, k, dp): if (k, s) in dp: return dp[k, s] if k <= 0: return 0 n = len(r) besttime = 10 ** 10 for i in range(n): if r[i] > r[s] and c[i] != c[s] or k == K: timetakenbelow = rec(r, c, i, K, k - r[i], dp) timetaken = timetakenbelow + abs(s - i) if timetaken < besttime: besttime = timetaken dp[k, s] = besttime return besttime def answer(n, s, K, r, c): dp = dict() k = K ans = rec(r, c, s, K, k, dp) if ans == 10 ** 10: return -1 return ans def main(): (n, s, K) = map(int, sys.stdin.readline().split()) r = tuple(map(int, sys.stdin.readline().split())) c = sys.stdin.readline().rstrip() print(answer(n, s - 1, K, r, c)) return main()
python
14
0.569672
51
20.529412
34
There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values ​​— red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$). Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. -----Input----- The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) β€” number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) β€” numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. -----Output----- Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1". -----Examples----- Input 5 3 10 1 2 3 4 5 RGBRR Output 4 Input 2 1 15 5 6 RG Output -1 -----Note----- The sequence of actions of Tanya for the first example: move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. Since Tanya eats candy instantly, the required time is four seconds.
taco
INF = 10000000000.0 (n, s, k) = map(int, input().split()) r = list(map(int, input().split())) r.append(0) col = input() mat = [] for i in range(n + 1): adj = {} for j in range(n): if i == n: adj[j] = abs(s - 1 - j) elif col[i] != col[j] and r[i] < r[j]: adj[j] = abs(i - j) mat.append(adj) mem = [{} for i in range(n + 1)] def get(s, k): if mem[s].get(k): return mem[s].get(k) if r[s] >= k: mem[s][k] = 0 else: mi = None for nei in mat[s]: ncost = get(nei, k - r[s]) if ncost is None: continue curr = ncost + mat[s][nei] if mi is None or curr < mi: mi = curr if mi is not None: mem[s][k] = mi else: mem[s][k] = INF return mem[s].get(k) ans = get(n, k) if ans is None or ans >= INF: print(-1) else: print(ans)
python
14
0.525424
40
18.175
40
There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values ​​— red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$). Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. -----Input----- The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) β€” number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) β€” numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. -----Output----- Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1". -----Examples----- Input 5 3 10 1 2 3 4 5 RGBRR Output 4 Input 2 1 15 5 6 RG Output -1 -----Note----- The sequence of actions of Tanya for the first example: move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. Since Tanya eats candy instantly, the required time is four seconds.
taco
import sys def minp(): return sys.stdin.readline().strip() dp = [None] * 50 for j in range(50): dp[j] = [None] * 2001 (n, s, k) = map(int, minp().split()) a = [None] * n i = 0 s -= 1 for j in map(int, minp().split()): a[i] = (j, i) i += 1 i = 0 for j in minp(): a[i] += ('RGB'.find(j),) i += 1 a.sort() r = 10 ** 18 zzz = 0 for i in range(n): ii = dp[i] c = a[i][0] ii[c] = abs(s - a[i][1]) for j in range(i): if a[j][2] == a[i][2] or a[j][0] == a[i][0]: continue jj = dp[j] for z in range(2001 - c): zz = jj[z] if zz != None: d = zz + abs(a[i][1] - a[j][1]) cc = z + c if ii[cc] != None: if ii[cc] > d: ii[cc] = d else: ii[cc] = d for z in range(k, 2001): if ii[z] != None: r = min(r, ii[z]) if r != 10 ** 18: print(r) else: print(-1)
python
17
0.46384
46
16.434783
46
There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values ​​— red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$). Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. -----Input----- The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) β€” number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) β€” numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. -----Output----- Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1". -----Examples----- Input 5 3 10 1 2 3 4 5 RGBRR Output 4 Input 2 1 15 5 6 RG Output -1 -----Note----- The sequence of actions of Tanya for the first example: move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. Since Tanya eats candy instantly, the required time is four seconds.
taco
inf = 10000 (n, s, k) = map(int, input().split()) a = list(map(int, input().split())) b = list(input()) for i in range(n): if b[i] == 'R': b[i] = 0 elif b[i] == 'G': b[i] = 1 else: b[i] = 2 boxes = [[a[i], b[i], i] for i in range(n)] boxes.sort() l = boxes[-1][0] * n + 1 s -= 1 dp = [[[inf, s, -1] for j in range(l)] for i in range(3)] if l < k: print(-1) return dp[0][0][0] = 0 dp[1][0][0] = 0 dp[2][0][0] = 0 for i in range(n): pos = boxes[i][2] clr = boxes[i][1] cnt = boxes[i][0] for j in range(l - cnt): for c in range(3): if c == clr: continue if dp[clr][j + cnt][0] > dp[c][j][0] + abs(dp[c][j][1] - pos) and cnt > dp[c][j][2]: dp[clr][j + cnt][0] = dp[c][j][0] + abs(dp[c][j][1] - pos) dp[clr][j + cnt][1] = pos dp[clr][j + cnt][2] = cnt ans = min(dp[0][k][0], min(dp[1][k][0], dp[2][k][0])) for i in range(k, l): ans = min(min(ans, dp[0][i][0]), min(dp[1][i][0], dp[2][i][0])) if ans < inf: print(ans) else: print(-1)
python
18
0.484536
87
22.658537
41
There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values ​​— red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$). Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. -----Input----- The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) β€” number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) β€” numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. -----Output----- Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1". -----Examples----- Input 5 3 10 1 2 3 4 5 RGBRR Output 4 Input 2 1 15 5 6 RG Output -1 -----Note----- The sequence of actions of Tanya for the first example: move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. Since Tanya eats candy instantly, the required time is four seconds.
taco
(n, s, k) = list(map(int, input().split())) amounts = list(map(int, input().split())) colors = list(input()) dp = [[-1 for j in range(k + 1)] for i in range(n)] def getAns(nth, left): if left <= 0: return 0 if dp[nth][left] >= 0: return dp[nth][left] ret = 999999999 for i in range(n): if amounts[i] <= amounts[nth] or colors[i] == colors[nth]: continue ret = min(ret, abs(nth - i) + getAns(i, left - amounts[i])) dp[nth][left] = ret return ret ans = 999999999 for i in range(n): ans = min(ans, getAns(i, k - amounts[i]) + abs(s - 1 - i)) if ans == 999999999: ans = -1 print(ans)
python
14
0.595
61
25.086957
23
There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values ​​— red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$). Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. -----Input----- The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) β€” number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) β€” numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. -----Output----- Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1". -----Examples----- Input 5 3 10 1 2 3 4 5 RGBRR Output 4 Input 2 1 15 5 6 RG Output -1 -----Note----- The sequence of actions of Tanya for the first example: move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$. Since Tanya eats candy instantly, the required time is four seconds.
taco
def sub(maxs, mins): for i in range(len(maxs)): if maxs[i] != mins[i]: if i == len(maxs) - 1: return int(maxs[i]) - int(mins[i]) if i == len(maxs) - 2: return int(maxs[i:i + 2]) - int(mins[i:i + 2]) return 10 return 0 def checkEqual(S): ans = 8 for k in range(1, len(S)): if len(S) % k != 0: continue mins = maxs = S[0:k] for s in range(0, len(S), k): maxs = max(maxs, S[s:s + k]) mins = min(mins, S[s:s + k]) ans = min(ans, sub(maxs, mins)) return ans def check12(S): maxv = 0 minv = 10 p = 0 while p < len(S): v = int(S[p]) if S[p] == '1' and p + 1 < len(S): v = 10 + int(S[p + 1]) p += 1 maxv = max(maxv, v) minv = min(minv, v) p += 1 return maxv - minv S = input() print(min(checkEqual(S), check12(S)))
python
16
0.523995
50
19.837838
37
If you visit Aizu Akabeko shrine, you will find a unique paper fortune on which a number with more than one digit is written. Each digit ranges from 1 to 9 (zero is avoided because it is considered a bad omen in this shrine). Using this string of numeric values, you can predict how many years it will take before your dream comes true. Cut up the string into more than one segment and compare their values. The difference between the largest and smallest value will give you the number of years before your wish will be fulfilled. Therefore, the result varies depending on the way you cut up the string. For example, if you are given a string 11121314 and divide it into segments, say, as 1,11,21,3,14, then the difference between the largest and smallest is 21 - 1 = 20. Another division 11,12,13,14 produces 3 (i.e. 14 - 11) years. Any random division produces a game of luck. However, you can search the minimum number of years using a program. Given a string of numerical characters, write a program to search the minimum years before your wish will be fulfilled. Input The input is given in the following format. n An integer n is given. Its number of digits is from 2 to 100,000, and each digit ranges from 1 to 9. Output Output the minimum number of years before your wish will be fulfilled. Examples Input 11121314 Output 3 Input 123125129 Output 6 Input 119138 Output 5
taco
n = input() length = len(n) ans = 10 lst = [] ind = 0 while ind < length: if n[ind] == '1' and ind + 1 <= length - 1: lst.append(int(n[ind:ind + 2])) ind += 2 else: lst.append(int(n[ind])) ind += 1 if len(lst) >= 2: ans = min(ans, max(lst) - min(lst)) divisors = [] for i in range(1, length // 2 + 1): if length % i == 0: divisors.append(i) for i in divisors: lst = [] for j in range(0, length, i): lst.append(int(n[j:j + i])) ans = min(ans, max(lst) - min(lst)) print(ans)
python
13
0.557809
44
19.541667
24
If you visit Aizu Akabeko shrine, you will find a unique paper fortune on which a number with more than one digit is written. Each digit ranges from 1 to 9 (zero is avoided because it is considered a bad omen in this shrine). Using this string of numeric values, you can predict how many years it will take before your dream comes true. Cut up the string into more than one segment and compare their values. The difference between the largest and smallest value will give you the number of years before your wish will be fulfilled. Therefore, the result varies depending on the way you cut up the string. For example, if you are given a string 11121314 and divide it into segments, say, as 1,11,21,3,14, then the difference between the largest and smallest is 21 - 1 = 20. Another division 11,12,13,14 produces 3 (i.e. 14 - 11) years. Any random division produces a game of luck. However, you can search the minimum number of years using a program. Given a string of numerical characters, write a program to search the minimum years before your wish will be fulfilled. Input The input is given in the following format. n An integer n is given. Its number of digits is from 2 to 100,000, and each digit ranges from 1 to 9. Output Output the minimum number of years before your wish will be fulfilled. Examples Input 11121314 Output 3 Input 123125129 Output 6 Input 119138 Output 5
taco
import heapq from math import sqrt import operator import sys inf_var = 0 if inf_var == 1: inf = open('input.txt', 'r') else: inf = sys.stdin input = inf.readline def read_one_int(): return int(input().rstrip('\n')) def read_list_of_ints(): res = [int(val) for val in input().rstrip('\n').split(' ')] return res def read_str(): return input().rstrip() def check_seq(deck_size, deck_cards): new_deck = [] used = [0 for i in range(deck_size)] last_used_index = deck_size - 1 prev_ind = deck_size for i in range(deck_size - 1, -1, -1): if deck_cards[i] == last_used_index + 1: new_deck += deck_cards[i:prev_ind] for j in range(i, prev_ind): used[deck_cards[j] - 1] = 1 prev_ind = i j = -1 while True: cur_ind = j + last_used_index if cur_ind < 0: last_used_index = -1 break if used[cur_ind]: j -= 1 continue else: last_used_index = cur_ind break return ' '.join(map(str, new_deck)) def main(): cnt = read_one_int() for _ in range(cnt): deck_size = read_one_int() deck_cards = read_list_of_ints() res = check_seq(deck_size, deck_cards) print(res) main()
python
14
0.605769
60
20.185185
54
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
t = int(input()) for _ in range(t): n = int(input()) p = list(map(int, input().split())) ans = [] p1 = [-1] * (n + 1) for i in range(n): p1[p[i]] = i i = n while i: while i > 0 and p1[i] == -1: i -= 1 else: if i: k = 0 for j in range(p1[i], n): ans.append(p[j]) p1[p[j]] = -1 k += 1 n -= k i -= 1 else: break print(*ans)
python
16
0.427441
36
14.791667
24
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys def get_ints(): return map(int, sys.stdin.readline().strip().split()) def get_list(): return list(map(int, sys.stdin.readline().strip().split())) def get_list_string(): return list(map(str, sys.stdin.readline().strip().split())) def get_string(): return sys.stdin.readline().strip() def get_int(): return int(sys.stdin.readline().strip()) def get_print_int(x): sys.stdout.write(str(x) + '\n') def get_print(x): sys.stdout.write(x + '\n') def get_print_int_same(x): sys.stdout.write(str(x) + ' ') def get_print_same(x): sys.stdout.write(x + ' ') from sys import maxsize def solve(): for _ in range(get_int()): n = get_int() arr = get_list() i = n - 1 j = n - 1 temp = sorted(arr) vis = [False] * n ans = [] while j >= 0: t = j tt = [] while t >= 0 and arr[t] != temp[i]: vis[arr[t] - 1] = True tt.append(arr[t]) t -= 1 vis[arr[t] - 1] = True tt.append(arr[t]) tt = tt[::-1] for k in tt: ans.append(k) j = t - 1 while i >= 0 and vis[i]: i -= 1 get_print(' '.join(map(str, ans))) solve()
python
15
0.568918
60
18.303571
56
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
from heapq import heappop, heappush import sys class MinMaxSet: def __init__(self): self.min_queue = [] self.max_queue = [] self.entries = {} def __len__(self): return len(self.entries) def add(self, val): if val not in self.entries: entry_min = [val, False] entry_max = [-val, False] heappush(self.min_queue, entry_min) heappush(self.max_queue, entry_max) self.entries[val] = (entry_min, entry_max) def delete(self, val): if val in self.entries: (entry_min, entry_max) = self.entries.pop(val) entry_min[-1] = entry_max[-1] = True def get_min(self): while self.min_queue[0][-1]: heappop(self.min_queue) return self.min_queue[0][0] def get_max(self): while self.max_queue[0][-1]: heappop(self.max_queue) return -self.max_queue[0][0] t = int(input()) while t > 0: n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) used = [0] * n pos = [0] * (n + 1) ans = list() s = MinMaxSet() for i in range(n): s.add(a[i]) pos[a[i]] = i while len(s) > 0: x = s.get_max() for j in range(pos[x], n): if used[j] > 0: break used[j] = 1 s.delete(a[j]) ans.append(a[j]) print(*ans) t -= 1
python
14
0.601681
49
20.25
56
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) w = list(map(int, input().split())) d = [0] * (n + 1) for (i, j) in enumerate(w): d[j] = i (a, x) = ([], n) for i in range(n, 0, -1): if d[i] < x: a.extend(w[d[i]:x]) x = d[i] print(' '.join(map(str, a)))
python
13
0.501629
36
20.928571
14
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def argmax(a): m = 0 res = [] for j in range(len(a)): if a[j] > m: m = a[j] res.append(j) res.reverse() return res def find(): end = int(input()) mas = list(map(int, input().split())) for j in argmax(mas): for k in range(j, end): print(mas[k], end=' ') end = j print() for i in range(int(input())): find()
python
13
0.548193
38
15.6
20
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
t = int(input()) for i in range(t): n = int(input()) p = list(map(int, input().split())) p_ord = p.copy() p_ord.sort() k = n - 1 r = list() for j in range(n - 1, -1, -1): while p_ord[k] == 0: k -= 1 maximo = p_ord[k] p_ord[p[j] - 1] = 0 if p[j] == maximo: r.extend(p[j:]) del p[j:] print(' '.join(map(str, r)))
python
13
0.485119
36
18.764706
17
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
A = [] def test_case(): n = int(input()) a = [int(i) for i in input().split()] mp = dict() for i in range(n): mp[a[i]] = i (ans, last) = ([], n) for i in range(n, 0, -1): if mp[i] <= last: ans.extend(a[mp[i]:last]) last = mp[i] A.append(ans) for _ in range(int(input())): test_case() for a in A: print(*a)
python
13
0.518405
38
17.111111
18
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
t = int(input()) while t > 0: t -= 1 n = int(input()) ar = [int(op) for op in input().split()] ans = [] y = [0 for i in range(n)] mx = n nmx = n ops = n while not nmx == 0: for i in reversed(range(ops)): if y[i] == 0: mx = i + 1 y[i] = 1 ops = i break for i in reversed(range(nmx)): if mx == ar[i]: idx = i break else: y[ar[i] - 1] = 1 for i in range(nmx - idx): ans.append(str(ar[i + idx])) nmx = idx print(' '.join(ans))
python
15
0.488565
41
16.814815
27
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) look = [0] * n maxx = arr[0] for i in range(n): maxx = max(arr[i], maxx) look[i] = maxx j = n ans = [] for i in range(n - 1, -1, -1): if look[i] == arr[i]: ans.append(arr[i:j]) j = i for i in ans: print(*i, end=' ') print()
python
13
0.510511
38
18.588235
17
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
N = int(input()) for _ in range(N): out = [] n = int(input()) l = [int(e) for e in input().split()] i = 0 for j in range(i, n): if l[j] > l[i]: out += l[i:j][::-1] i = j out += l[i:n][::-1] print(' '.join([str(e) for e in out[::-1]]))
python
13
0.452
45
19.833333
12
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
from collections import OrderedDict import heapq as hq def show(l): for i in l: print(i, end=' ') for _ in range(int(input())): n = int(input()) r = [] l = list(map(int, input().split())) t = [(j, i) for (i, j) in enumerate(l)] t.sort(reverse=True) od = OrderedDict(t) idx = n for e in l[::-1]: m = next(iter(od)) if e == m: show(l[od[e]:idx]) idx = od[e] del od[e] print()
python
13
0.56391
40
18
21
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import math for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = {} ans = [] for i in range(n): b[a[i]] = i flag = n for j in range(n, 0, -1): if b[j] <= flag: for k in range(b[j], flag): ans.append(a[k]) flag = b[j] print(*ans)
python
13
0.515789
36
18
15
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
from collections import deque t = int(input()) for _ in range(t): c = int(input()) stack = list(map(int, input().split())) ans = deque() flag = 0 greatest = stack[0] for i in range(1, c): if greatest < stack[i]: ans.extendleft(reversed(stack[flag:i])) flag = i greatest = stack[i] ans.extendleft(reversed(stack[flag:c])) print(*ans, sep=' ')
python
14
0.631579
42
23.066667
15
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
t = int(input()) while t > 0: t -= 1 n = int(input()) a = list(map(int, input().split())) ans = [] r = [0] * (n + 1) for i in range(n): r[a[i]] = i k = n for i in range(n, 0, -1): if r[i] <= k: for j in range(r[i], k): ans.append(a[j]) k = r[i] print(*ans)
python
13
0.457143
36
16.5
16
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split(' '))) ans = [] temp = [] Greater = [arr[0]] for k in range(1, n): if Greater[k - 1] > arr[k]: Greater.append(Greater[k - 1]) else: Greater.append(arr[k]) for j in range(len(arr) - 1, -1, -1): if arr[j] != Greater[j]: temp.append(arr[j]) else: temp.append(arr[j]) ans += temp[::-1] temp = [] print(' '.join(map(str, ans)))
python
13
0.536036
41
21.2
20
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
quant_testes = int(input()) for c in range(quant_testes): output = '' tam = int(input()) seq = [int(n) for n in input().split()] posicoes = [None] * tam for i in range(len(seq)): posicoes[-seq[i]] = i for pos in posicoes: if pos + 1 <= tam: output += str(seq[pos]) output += ' ' for i in range(pos + 1, tam): output += str(seq[i]) output += ' ' tam -= 1 tam -= 1 print(output)
python
14
0.553398
40
21.888889
18
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
T = int(input()) for t in range(T): n = int(input()) pi = list(map(int, input().split())) r = [] t = [0] * len(pi) t[0] = pi[0] for i in range(1, len(pi)): t[i] = max(t[i - 1], pi[i]) index = len(pi) - 1 lastIndex = len(pi) while index >= 0: while index >= 0 and pi[index] != t[index]: index -= 1 if pi[index] == t[index]: r += pi[index:lastIndex] lastIndex = index index -= 1 print(' '.join(map(str, r)))
python
13
0.531178
45
21.789474
19
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) li = list(map(int, input().split())) bigs = [0] current_max = li[0] for i in range(1, n): if li[i] > current_max: current_max = li[i] bigs.append(i) bigs = reversed(bigs) ans = [] for start in bigs: for j in range(start, n): ans.append(li[j]) n = start print(*ans)
python
13
0.601604
37
19.777778
18
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys import os.path from collections import * import math import bisect if os.path.exists('input.txt'): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') else: input = sys.stdin.readline t = int(input()) while t: t -= 1 n = int(input()) p = [int(x) for x in input().split()] arr = [0] * n maxval = 0 for i in range(n): maxval = max(p[i], maxval) arr[i] = (p[i], maxval) arr.sort(key=lambda x: x[1], reverse=True) for i in range(n): print(arr[i][0], end=' ') print()
python
11
0.615234
43
20.333333
24
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
n = int(input()) for i in range(n): m = int(input()) a = list(map(int, input().split())) maxrest = [0 for i in range(m)] for j in range(1, m): if a[j] > a[maxrest[j - 1]]: maxrest[j] = j else: maxrest[j] = maxrest[j - 1] rest = m while rest != 0: newrest = maxrest[rest - 1] for j in range(newrest, rest): print(a[j], end=' ') rest = newrest
python
13
0.558583
36
21.9375
16
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
t = int(input()) for j in range(t): ans = dict() k = int(input()) d = list(map(int, input().split())) ma = d[0] ans[0] = ma for i in range(1, k): if d[i] > ma: ma = d[i] ans[i] = ma ans = list(reversed(ans.keys())) b = [] end = len(ans) for i in range(end): p = ans[i] b += d[p:k] k = p print(*b)
python
13
0.496894
36
15.947368
19
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys input = sys.stdin.readline def solve(): n = int(input()) arr = list(map(int, input().split())) S = set() mv = n tmp = [] ans = [] for i in range(n - 1, -1, -1): tmp.append(arr[i]) S.add(arr[i]) if arr[i] == mv: while tmp: ans.append(tmp.pop()) while mv in S: mv -= 1 return ans for _ in range(int(input())): print(*solve())
python
14
0.547945
38
16.380952
21
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for s in [*open(0)][2::2]: (*l,) = map(int, s.split()) a = [] j = len(l) a = [] L = [0] for i in range(1, j): if l[L[-1]] < l[i]: L += [i] else: L += [L[-1]] while j: i = L[j - 1] a += l[i:j] j = i print(*a)
python
13
0.37069
28
13.5
16
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) li = list(map(int, input().split())) tmp = [0] * (n + 1) res = [] for i in range(n): tmp[li[i]] = i k = n for i in range(n, 0, -1): if tmp[i] <= k: for j in range(tmp[i], k): res.append(li[j]) k = tmp[i] print(*res)
python
13
0.494737
37
19.357143
14
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
from sys import stdin lst = list(map(int, stdin.read().split())) _s = 0 def inp(n=1): global _s ret = lst[_s:_s + n] _s += n return ret def inp1(): return inp()[0] t = inp1() for _ in range(t): n = inp1() c = inp(n) new = [] for i in range(n): if len(new) and c[i] < new[-1][0]: new[-1].append(c[i]) else: new.append([c[i]]) new.reverse() for i in new: for j in i: print(j, end=' ') print()
python
13
0.535714
42
14.555556
27
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) a = [0] * n t = [] for i in range(n): a[arr[i] - 1] = i f = n + 1 for i in range(len(a) - 1, -1, -1): if a[i] > f: continue t.append(arr[a[i]:f]) f = a[i] for i in range(len(t)): for j in range(len(t[i])): print(t[i][j], end=' ') print()
python
13
0.488506
38
19.470588
17
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def take_second(elem): return elem[0] q = int(input()) while q > 0: n = int(input()) a = input().split() a = [int(x) for x in a] list = [] i = 0 while i < n: if i == 0 or a[i] > a[i - 1]: list.append([a[i]]) list[-1].append(i) i += 1 list = sorted(list, key=take_second) b = [0 for i in range(n)] ans = [] i = len(list) - 1 while i >= 0: for j in range(list[i][1], n): if b[j] == 0: b[j] = 1 ans.append(a[j]) else: break i -= 1 for i in ans: print(i, end=' ') print() q = q - 1
python
13
0.493359
37
16.566667
30
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def solve(arr, size): positions = [0] * size for j in range(size): positions[arr[j] - 1] = j K = [positions[size - 1]] for j in range(size - 2, -1, -1): if positions[j] < K[-1]: K.append(positions[j]) result = [0] * size right = size pos = 0 for left in K: for j in range(right - left): result[pos] = arr[left + j] pos += 1 right = left for el in result: print(el, end=' ') print('') N = int(input()) for n in range(N): L = int(input()) A = [int(x) for x in input().split()] solve(A, L)
python
11
0.564547
38
20.625
24
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys input = sys.stdin.readline for _ in range(int(input().strip())): n = int(input().strip()) a = list(map(int, input().strip().split(' '))) asc = set(a) e = n o = [] for i in range(n, 0, -1): if i in asc: for j in range(e - 1, -1, -1): if a[j] == i: for k in a[j:e]: asc.remove(k) o += a[j:e] e = j break print(' '.join(map(str, o)))
python
16
0.497396
47
20.333333
18
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) look = [0] * n look[0] = a[0] for i in range(1, n): look[i] = max(look[i - 1], a[i]) j = n ans = [] for i in range(n - 1, -1, -1): if look[i] == a[i]: ans.extend(a[i:j]) j = i print(*ans)
python
13
0.489655
36
19.714286
14
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys input = sys.stdin.readline import math import bisect from copy import deepcopy as dc from itertools import accumulate from collections import Counter, defaultdict, deque def ceil(U, V): return (U + V - 1) // V def modf1(N, MOD): return (N - 1) % MOD + 1 inf = int(1e+18) mod = int(1000000000.0 + 7) t = int(input()) for _ in range(t): n = int(input()) p = list(map(int, input().split())) pc = list(accumulate(p, func=max)) od = [] prev = n for i in range(n - 1, -1, -1): if pc[i] == p[i]: for j in range(i, prev): od.append(p[j]) prev = i for j in range(prev): od.append(p[j]) print(*od)
python
13
0.619808
51
19.866667
30
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import os import sys from io import BytesIO, IOBase def main(): for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) p = [a[0]] for i in range(1, n): p.append(max(p[-1], a[i])) b = [0] * (n + 1) for (i, v) in enumerate(a): b[v] = i ans = [] i = n - 1 while p: j = b[p[-1]] for k in range(j, i + 1): ans.append(a[k]) p.pop() i = j - 1 print(*ans) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = 'x' in file.mode or 'r' not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b'\n') + (not b) ptr = self.buffer.tell() (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) (self.buffer.truncate(0), self.buffer.seek(0)) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode('ascii')) self.read = lambda : self.buffer.read().decode('ascii') self.readline = lambda : self.buffer.readline().decode('ascii') (sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout)) input = lambda : sys.stdin.readline().rstrip('\r\n') main()
python
17
0.627209
72
25.671429
70
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = [] c = [0 for j in range(n)] d = n e = n for j in range(n - 1, -1, -1): c[a[j] - 1] = 1 if a[j] == d: b += a[j:e] e = j while d > 0 and c[d - 1] == 1: d -= 1 b += a[:e] print(*b)
python
13
0.424658
36
17.25
16
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) k = [] d = {} for i in range(len(l)): d[l[i]] = i t = int(n) for i in range(n, 0, -1): if d[i] <= t: for j in range(d[i], t): k.append(l[j]) t = d[i] print(*k)
python
13
0.471698
36
17.928571
14
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def card(n, arr): ind = [0] * n temp = n ans = [] for i in range(n): ind[arr[i] - 1] = i for i in ind[::-1]: if i < temp: ans += arr[i:temp] temp = i return ans for i in range(int(input())): a = int(input()) lst = list(map(int, input().strip().split())) print(*card(a, lst))
python
15
0.530612
46
18.6
15
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) dic = {} result = [] for i in range(n): dic[l[i]] = i temp = n for i in range(n, 0, -1): if dic[i] < temp: result.extend(l[dic[i]:temp]) temp = dic[i] print(*result)
python
13
0.539326
36
19.538462
13
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
t = int(input()) for _ in range(t): n = int(input()) dec = list(map(int, input().split())) dic = {} for i in range(n): dic[dec[i]] = i covered_till = n new_dec = [] for i in range(n, 0, -1): if dic[i] < covered_till: new_dec += dec[dic[i]:covered_till] covered_till = dic[i] print(*new_dec)
python
13
0.563107
38
21.071429
14
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
from collections import defaultdict for _ in range(int(input())): n = int(input()) v = list(map(int, input().split())) cnt = 0 j = n - 1 i = n - 1 cur_max = n temp = [] while cnt < n: while v[i] != cur_max: temp.append(v[i]) i -= 1 temp.append(v[i]) temp.sort(reverse=True) flag = 1 for x in range(1, len(temp)): if temp[x - 1] - temp[x] > 1: cur_max = temp[x - 1] - 1 flag = 0 temp = temp[x:] break if flag: cur_max = temp[len(temp) - 1] - 1 temp = [] elif flag != 0 and len(temp) == 1: cur_max -= 1 temp = [] for k in range(i, j + 1): print(v[k], end=' ') cnt += 1 j = i - 1 i -= 1 print('')
python
14
0.504505
36
18.588235
34
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) ps = list(map(int, input().split())) dp = [ps[0]] for i in range(1, n): dp.append(max(dp[-1], ps[i])) res = [] j = n - 1 temp = [ps[-1]] for i in range(n - 2, -1, -1): if dp[i] == dp[i + 1]: temp.append(ps[i]) else: res += temp[::-1] temp = [ps[i]] res += temp[::-1] print(*res)
python
13
0.481481
37
19.647059
17
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
from collections import deque n = int(input()) for _ in range(n): c = int(input()) d = list(map(int, input().split())) answer = deque() count = 0 greatest = d[0] for i in range(1, c): if greatest < d[i]: answer.extendleft(reversed(d[count:i])) count = i greatest = d[i] answer.extendleft(reversed(d[count:c])) print(*answer, sep=' ')
python
14
0.623229
42
22.533333
15
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def cardDeck(n, array): array.reverse() stack = [] for x in range(n): if not stack: stack.append(x) else: while stack and array[x] > array[stack[-1]]: stack.pop() stack.append(x) ans = [] prev = 0 ans += array[stack[0]::-1] for x in range(1, len(stack)): ans += array[stack[x]:stack[x - 1]:-1] prev = stack[x] return ans t = int(input()) final = [] for _ in range(t): n = int(input()) l = list(map(int, input().split())) final.append(cardDeck(n, l)) for _ in range(t): for x in final[_]: print(x, end=' ') print()
python
14
0.578182
47
19.37037
27
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
from collections import Counter, deque from math import * mod = 998244353 def solve(): n = int(input()) l = list(map(int, input().split())) val = [i + 1 for i in range(n)] cur = val[-1] ans = [] x = n - 1 z = n - 1 while x >= 0: d = deque() while l[x] != cur: y = l.pop() val[y - 1] = -1 d.appendleft(y) x -= 1 y = l.pop() d.appendleft(y) ans += d x -= 1 val[y - 1] = -1 while z >= 0 and val[z] == -1: z -= 1 cur = val[z] print(*ans) t = int(input()) for _ in range(t): solve()
python
13
0.505747
38
15.83871
31
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys input = lambda : sys.stdin.readline().rstrip('\r\n') inp = lambda : list(map(int, sys.stdin.readline().rstrip('\r\n').split())) mod = 10 ** 9 + 7 Mod = 998244353 INF = float('inf') from heapq import * tc = 1 (tc,) = inp() for _ in range(tc): (n,) = inp() a = inp() vis = [False] * n h = [(-a[i], i) for i in range(n)] heapify(h) ans = [] while h: (node, ind) = heappop(h) if vis[ind] == True: continue for i in range(ind, n): if vis[i] == True: break vis[i] = True ans.append(a[i]) print(*ans)
python
15
0.558879
74
19.576923
26
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def solve(): n = int(input()) A = list(map(int, input().split())) B = [0] * (n + 1) top = n pos = n (l, r) = (0, n) ans = [] for i in range(n - 1, -1, -1): if A[i] == top: for x in range(i, r): ans.append(A[x]) B[A[x]] = 1 r = i while B[top]: top -= 1 continue return ans for i in range(int(input())): print(*solve())
python
13
0.474576
36
16.7
20
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
t = int(input()) for i in range(t): n = int(input()) p = list(map(int, input().split())) l = [i for i in range(n, 0, -1)] j = n - 1 k = 0 b = n - 1 a = [] while j >= 0: if l[k] > p[j] and l[k] != -1: l[n - p[j]] = -1 j = j - 1 elif l[k] == p[j] and l[k] != -1: a += p[j:b + 1] j = j - 1 b = j k = k + 1 elif l[k] == -1: k = k + 1 print(*a)
python
13
0.388298
36
16.904762
21
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def carddeckV2(): nbCase = int(input()) res = [] for i in range(nbCase): l_desk = int(input()) case = [int(j) for j in input().split(' ')] M = case[0] last = 0 p_prime = [] for i in range(len(case)): if case[i] > M: for j in reversed(range(last, i)): p_prime.append(case[j]) last = i M = case[i] for j in reversed(range(last, len(case))): p_prime.append(case[j]) res.append(p_prime) for r in res: s = '' for i in reversed(range(len(r))): s += str(r[i]) s += ' ' print(s) carddeckV2()
python
15
0.55102
45
20.56
25
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def solve(nums): h = {num: i for (i, num) in enumerate(nums)} m = [h[str(num)] for num in range(len(nums), 0, -1)] result = [] i_max = len(nums) for i in m: if i < i_max: result += nums[i:i_max] i_max = i print(' '.join(map(str, result))) t = int(input().strip()) while t: t -= 1 input() solve(input().split())
python
11
0.557927
53
20.866667
15
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
t = int(input()) for _ in range(t): n = int(input()) p = list(map(int, input().split())) d = {} for i in range(n): d[p[i]] = i out = [] pv = n for i in range(n, 0, -1): if d[i] <= pv: out += p[d[i]:pv] pv = d[i] print(*out)
python
13
0.466942
36
16.285714
14
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys, functools, collections, bisect, math input = sys.stdin.readline import heapq t = int(input()) for _ in range(t): n = int(input().strip()) arr = list(map(int, input().strip().split())) maxarr = [(arr[0], 0)] for i in range(1, n): if arr[i] > maxarr[-1][0]: maxarr.append((arr[i], i)) else: maxarr.append(maxarr[-1]) curr = n - 1 ans = [] while curr > -1: for i in range(maxarr[curr][1], curr + 1): ans.append(arr[i]) curr = maxarr[curr][1] - 1 print(' '.join((str(i) for i in ans)))
python
15
0.593449
48
24.95
20
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def process(cards): maxes = [[cards[0]]] n = len(cards) for i in range(1, n): if cards[i] > maxes[-1][0]: maxes.append([cards[i]]) else: maxes[-1].append(cards[i]) answer = [] m = len(maxes) for i in range(m): x = maxes[m - 1 - i] for y in x: answer.append(y) return answer T = int(input()) for I in range(T): n = int(input()) cards = [int(x) for x in input().split()] cards = process(cards) cards = ' '.join(map(str, cards)) print(cards)
python
13
0.581197
42
20.272727
22
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys def debug(*args): print(*args, file=sys.stderr) def read_str(): return sys.stdin.readline().strip() def read_int(): return int(sys.stdin.readline().strip()) def read_ints(): return map(int, sys.stdin.readline().strip().split()) def read_str_split(): return list(sys.stdin.readline().strip()) def read_int_list(): return list(map(int, sys.stdin.readline().strip().split())) def Main(): t = read_int() for _ in range(t): n = read_int() p = read_int_list() note = [0] * n for (i, x) in enumerate(p): note[~-x] = i last = n ans = [] for i in range(n - 1, -1, -1): if note[i] < last: for j in range(note[i], last): ans.append(p[j]) last = note[i] print(*ans) Main()
python
15
0.598338
60
18.513514
37
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import math for i in range(int(input())): n = int(input()) l = list(map(int, input().split())) m = [l[0]] k = [] c = l[0] for i in range(1, n): if l[i] > c: k.append(m) m = [l[i]] c = l[i] else: m.append(l[i]) k.append(m) for i in range(len(k) - 1, -1, -1): print(*k[i], end=' ') print()
python
13
0.48254
36
16.5
18
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def cards(arr, l): s = [0] maxsof = arr[0] for i in range(1, l): if maxsof < arr[i]: maxsof = arr[i] s += [i] s = s[::-1] upto = l t = [] for i in s: for j in range(i, upto): t += [arr[j]] upto = i print(*t) n = int(input()) while n > 0: l = int(input()) c = [int(i) for i in input().split()] cards(c, l) n -= 1
python
11
0.483776
38
15.142857
21
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) li = list(map(int, input().split())) b = {} li1 = [] for i in range(n): b[li[i]] = i ck = n for i in range(n, 0, -1): if b[i] <= ck: for j in range(b[i], ck): li1.append(li[j]) ck = b[i] print(*li1)
python
13
0.492537
37
18.142857
14
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
T = int(input()) for _ in range(T): N = int(input()) A = list(map(int, input().split())) ans = list() Position = {i: None for i in range(1, N + 1)} for (i, elt) in enumerate(A): Position[elt] = i todo = N last = N while todo >= 1: i = Position[todo] ans.extend(A[i:last]) last = i while Position[todo] >= N - len(ans): todo -= 1 if todo < 1: break print(' '.join(map(str, ans)))
python
13
0.556373
46
20.473684
19
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) dic = dict() ans = [] maxi = n for i in range(n): dic[arr[i]] = i for j in range(n, 0, -1): if dic[j] <= maxi: ans += arr[dic[j]:maxi] maxi = dic[j] print(*ans)
python
13
0.526515
38
19.307692
13
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
_t = int(input()) for t in range(_t): n = int(input()) a = list(map(int, input().split())) pos = [0] * (n + 1) for i in range(n): pos[a[i]] = i res = [] lastpos = n for i in range(n, 0, -1): p = pos[i] if p >= lastpos: continue res += a[p:lastpos] lastpos = p if lastpos == 0: break print(' '.join(map(str, res)))
python
13
0.517647
36
17.888889
18
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for i in range(int(input())): n = int(input()) l = list(map(int, input().split())) support = [] mx = 0 mx1 = n n1 = n visited = set() for i in range(len(l) - 1, -1, -1): if l[i] == n1: support += l[i:mx1] mx1 = i visited.add(l[i]) while n1 in visited: n1 -= 1 print(*support)
python
13
0.53
36
17.75
16
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
test = int(input()) for _ in range(test): nn = int(input()) l = list(map(int, input().split())) mark = [0] * (nn + 1) i = nn - 1 j = nn ans = [] while i >= 0 and j >= 1: k = i while l[k] != j: k -= 1 for index in range(k, i + 1): ans.append(l[index]) mark[l[index]] = 1 i = k - 1 while mark[j] == 1: j -= 1 for i in ans: print(i, end=' ') print()
python
13
0.478947
36
17.095238
21
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def debug(): for i in range(10): pass def mmm(a, b, m): res = 0 a %= m while b: if b & 1: res = (res + a) % m a = 2 * a % m b >>= 1 return res def pw(a, b, c): ans = 1 a = a % c if a == 0: return 0 while b > 0: if b & 1: ans = ans * a % c b = b >> 1 a = a * a % c return ans try: for _ in range(int(input())): n = int(input()) arr = [int(i) for i in input().split()] arr2 = sorted(arr, reverse=True) mp = {} for (i, j) in enumerate(arr): mp[j] = i ans = [] for i in arr2: idx = mp[i] ptr = idx while ptr < n and arr[ptr] != 0: ans.append(arr[ptr]) arr[ptr] = 0 ptr += 1 print(*ans) except EOFError as e: print(e)
python
13
0.489115
41
14.659091
44
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
__version__ = '0.2' __date__ = '2021-03-06' import sys def solve(n, p): answer = [] pos = [0] * (n + 1) for i in range(n): pos[p[i]] = i cur = n for largest in range(n, 0, -1): if pos[largest] >= cur: continue i = pos[largest] answer.extend(p[i:cur]) cur = i return answer def main(argv=None): t = int(input()) for _ in range(t): n = int(input()) p = list(map(int, input().split())) print(' '.join(map(str, solve(n, p)))) return 0 STATUS = main() sys.exit(STATUS)
python
15
0.562753
40
17.296296
27
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
from pprint import pprint import sys input = sys.stdin.readline def do(): from heapq import heappop, heappush, heapify n = int(input()) odat = list(map(int, input().split())) dat = [] for i in range(n): dat.append((-odat[i], i)) heapify(dat) res = [] totteru = n while len(dat) > 0: (curVal, curInd) = heappop(dat) if totteru <= curInd: continue for i in range(curInd, totteru): res.append(odat[i]) totteru = curInd print(' '.join(list(map(str, res)))) q = int(input()) for _ in range(q): do()
python
13
0.632184
45
19.88
25
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = [0] * (n + 1) ans = [] for i in range(n): b[a[i]] = i ck = n for i in range(n, 0, -1): if b[i] <= ck: for j in range(b[i], ck): ans.append(a[j]) ck = b[i] print(*ans)
python
13
0.478261
36
18.714286
14
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
from sys import stdin, stdout input = stdin.readline inf = int(1e+20) for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) prev = n rem = set() m = n ans = [] for i in range(n - 1, -1, -1): if arr[i] == m: ans += arr[i:prev] for d in arr[i:prev]: rem.add(d) prev = i while m in rem: m -= 1 print(' '.join(map(str, ans)))
python
13
0.543081
38
19.157895
19
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def segfunc(x, y): return max(x, y) ide_ele = -10 ** 18 class SegTree: def __init__(self, init_val, segfunc, ide_ele): n = len(init_val) self.segfunc = segfunc self.ide_ele = ide_ele self.num = 1 << (n - 1).bit_length() self.tree = [ide_ele] * 2 * self.num for i in range(n): self.tree[self.num + i] = init_val[i] for i in range(self.num - 1, 0, -1): self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1]) def update(self, k, x): k += self.num self.tree[k] = x while k > 1: self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1]) k >>= 1 def query(self, l, r): res = self.ide_ele l += self.num r += self.num while l < r: if l & 1: res = self.segfunc(res, self.tree[l]) l += 1 if r & 1: res = self.segfunc(res, self.tree[r - 1]) l >>= 1 r >>= 1 return res import sys input = sys.stdin.readline import math import copy t = int(input()) for f in range(t): n = int(input()) p = list(map(int, input().split())) ans = [] a = copy.copy(p) a.reverse() last = n seg = SegTree(p, segfunc, ide_ele) flg = seg.query(0, last) for i in range(n): if a[i] == flg: for j in range(n - i - 1, last): ans.append(p[j]) last = n - i - 1 flg = seg.query(0, last) print(*ans)
python
15
0.56088
70
20.948276
58
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) p = list(map(int, input().split())) q = [(pi, i) for (i, pi) in enumerate(p)] q.sort() (pi, i) = q.pop() np = p[i:] while q: (npi, ni) = q.pop() if ni < i: np += p[ni:i] i = ni print(*np)
python
13
0.522337
42
18.4
15
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) a = [*map(int, input().split())] ans = [] j = n final = [0] * n for i in range(n): final[a[i] - 1] = i for i in range(n - 1, -1, -1): if final[i] < j: ans += a[final[i]:j] j = final[i] print(*ans)
python
12
0.484733
33
19.153846
13
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) li = list(map(int, input().split())) index = [0] * n for i in range(n): index[li[i] - 1] = i temp = n ans = [] for ind in reversed(index): if ind < temp: ans += li[ind:temp] temp = ind print(*ans)
python
13
0.545802
37
19.153846
13
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys input = lambda : sys.stdin.readline().rstrip('\r\n') for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = [] d = dict() t = 0 ck = [] for (i, v) in enumerate(a): d[v] = i t = max(t, v) ck.append(t) l = n while len(b) != n: mv = ck[l - 1] for i in range(d[mv], l): b.append(a[i]) l = d[mv] print(*b)
python
13
0.512195
52
17.45
20
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import os, sys, heapq as h, time from io import BytesIO, IOBase from types import GeneratorType from bisect import bisect_left, bisect_right from collections import defaultdict as dd, deque as dq, Counter as dc import math, string BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os self._fd = file.fileno() self.buffer = BytesIO() self.writable = 'x' in file.mode or 'r' not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b'\n') + (not b) ptr = self.buffer.tell() (self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: self.os.write(self._fd, self.buffer.getvalue()) (self.buffer.truncate(0), self.buffer.seek(0)) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode('ascii')) self.read = lambda : self.buffer.read().decode('ascii') self.readline = lambda : self.buffer.readline().decode('ascii') (sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout)) input = lambda : sys.stdin.readline().rstrip('\r\n') def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input() def listStr(): return list(input()) def getMat(n): return [getInts() for _ in range(n)] def isInt(s): return '0' <= s[0] <= '9' MOD = 10 ** 9 + 7 class SortedList: def __init__(self, iterable=[], _load=200): values = sorted(iterable) self._len = _len = len(values) self._load = _load self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)] self._list_lens = [len(_list) for _list in _lists] self._mins = [_list[0] for _list in _lists] self._fen_tree = [] self._rebuild = True def _fen_build(self): self._fen_tree[:] = self._list_lens _fen_tree = self._fen_tree for i in range(len(_fen_tree)): if i | i + 1 < len(_fen_tree): _fen_tree[i | i + 1] += _fen_tree[i] self._rebuild = False def _fen_update(self, index, value): if not self._rebuild: _fen_tree = self._fen_tree while index < len(_fen_tree): _fen_tree[index] += value index |= index + 1 def _fen_query(self, end): if self._rebuild: self._fen_build() _fen_tree = self._fen_tree x = 0 while end: x += _fen_tree[end - 1] end &= end - 1 return x def _fen_findkth(self, k): _list_lens = self._list_lens if k < _list_lens[0]: return (0, k) if k >= self._len - _list_lens[-1]: return (len(_list_lens) - 1, k + _list_lens[-1] - self._len) if self._rebuild: self._fen_build() _fen_tree = self._fen_tree idx = -1 for d in reversed(range(len(_fen_tree).bit_length())): right_idx = idx + (1 << d) if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]: idx = right_idx k -= _fen_tree[idx] return (idx + 1, k) def _delete(self, pos, idx): _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len -= 1 self._fen_update(pos, -1) del _lists[pos][idx] _list_lens[pos] -= 1 if _list_lens[pos]: _mins[pos] = _lists[pos][0] else: del _lists[pos] del _list_lens[pos] del _mins[pos] self._rebuild = True def _loc_left(self, value): if not self._len: return (0, 0) _lists = self._lists _mins = self._mins (lo, pos) = (-1, len(_lists) - 1) while lo + 1 < pos: mi = lo + pos >> 1 if value <= _mins[mi]: pos = mi else: lo = mi if pos and value <= _lists[pos - 1][-1]: pos -= 1 _list = _lists[pos] (lo, idx) = (-1, len(_list)) while lo + 1 < idx: mi = lo + idx >> 1 if value <= _list[mi]: idx = mi else: lo = mi return (pos, idx) def _loc_right(self, value): if not self._len: return (0, 0) _lists = self._lists _mins = self._mins (pos, hi) = (0, len(_lists)) while pos + 1 < hi: mi = pos + hi >> 1 if value < _mins[mi]: hi = mi else: pos = mi _list = _lists[pos] (lo, idx) = (-1, len(_list)) while lo + 1 < idx: mi = lo + idx >> 1 if value < _list[mi]: idx = mi else: lo = mi return (pos, idx) def add(self, value): _load = self._load _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len += 1 if _lists: (pos, idx) = self._loc_right(value) self._fen_update(pos, 1) _list = _lists[pos] _list.insert(idx, value) _list_lens[pos] += 1 _mins[pos] = _list[0] if _load + _load < len(_list): _lists.insert(pos + 1, _list[_load:]) _list_lens.insert(pos + 1, len(_list) - _load) _mins.insert(pos + 1, _list[_load]) _list_lens[pos] = _load del _list[_load:] self._rebuild = True else: _lists.append([value]) _mins.append(value) _list_lens.append(1) self._rebuild = True def discard(self, value): _lists = self._lists if _lists: (pos, idx) = self._loc_right(value) if idx and _lists[pos][idx - 1] == value: self._delete(pos, idx - 1) def remove(self, value): _len = self._len self.discard(value) if _len == self._len: raise ValueError('{0!r} not in list'.format(value)) def pop(self, index=-1): (pos, idx) = self._fen_findkth(self._len + index if index < 0 else index) value = self._lists[pos][idx] self._delete(pos, idx) return value def bisect_left(self, value): (pos, idx) = self._loc_left(value) return self._fen_query(pos) + idx def bisect_right(self, value): (pos, idx) = self._loc_right(value) return self._fen_query(pos) + idx def count(self, value): return self.bisect_right(value) - self.bisect_left(value) def __len__(self): return self._len def __getitem__(self, index): (pos, idx) = self._fen_findkth(self._len + index if index < 0 else index) return self._lists[pos][idx] def __delitem__(self, index): (pos, idx) = self._fen_findkth(self._len + index if index < 0 else index) self._delete(pos, idx) def __contains__(self, value): _lists = self._lists if _lists: (pos, idx) = self._loc_left(value) return idx < len(_lists[pos]) and _lists[pos][idx] == value return False def __iter__(self): return (value for _list in self._lists for value in _list) def __reversed__(self): return (value for _list in reversed(self._lists) for value in reversed(_list)) def __repr__(self): return 'SortedList({0})'.format(list(self)) def solve(): N = getInt() A = getInts() ans = [] B = SortedList(A) while B: idx = len(A) - 1 m = B[-1] while A[idx] != m: idx -= 1 for j in range(idx, len(A)): ans.append(A[j]) B.remove(A[j]) for j in range(idx, len(A)): A.pop() print(*ans) return for _ in range(getInt()): solve()
python
17
0.602555
80
23.674576
295
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
from sys import * input = lambda : stdin.readline() int_arr = lambda : list(map(int, stdin.readline().strip().split())) for _ in range(int(input())): n = int(input()) arr = int_arr() ind = [0] * n for i in range(n): ind[n - arr[i]] = i last = n res = [] for i in range(n): if ind[i] <= last: res += arr[ind[i]:last] last = ind[i] print(*res)
python
14
0.566667
67
21.5
16
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import itertools for _ in range(int(input())): n = int(input()) p = list(map(int, input().split(' '))) cands = [] new_deck = [] for pi in p: if not cands: cands.append(pi) ord_cands = pi elif pi < cands[0]: cands.append(pi) else: new_deck.append(cands) cands = [pi] new_deck.append(cands) print(*itertools.chain.from_iterable(new_deck[::-1]))
python
13
0.61186
54
20.823529
17
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
from collections import deque def solve(arr): sorted_arr = sorted([(val, idx) for (idx, val) in enumerate(arr)], reverse=True) N = len(arr) curr_idx = N - 1 output = [] i = 0 while curr_idx >= 0: (val, new_idx) = sorted_arr[i] if curr_idx < new_idx: i += 1 continue for j in range(new_idx, curr_idx + 1): output.append(str(arr[j])) i += 1 curr_idx = new_idx - 1 print(' '.join(output)) return t = int(input()) while t: t -= 1 n = int(input()) arr = [int(val) for val in input().split(' ')] solve(arr)
python
13
0.590994
81
20.32
25
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import sys from collections import * import math import bisect def input(): return sys.stdin.readline() for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = [] c = -1 c1 = [] for i in range(n): if a[i] > c: b.append(i) c = a[i] for i in range(len(b) - 1, -1, -1): if len(c1) == 0: c1.extend(a[b[i]:]) else: c1.extend(a[b[i]:b[i + 1]]) print(*c1)
python
15
0.551471
36
16.73913
23
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
from collections import defaultdict t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) a.reverse() check = n ans = [] tmp = [] d = defaultdict(int) for i in range(n): d[a[i]] += 1 if a[i] == check: tmp.append(a[i]) tmp.reverse() ans.append(tmp) tmp = [] while True: if d[check] == 1: check -= 1 else: break if check == 1: break else: tmp.append(a[i]) ans2 = [] for ary in ans: for i in ary: ans2.append(i) print(*ans2)
python
13
0.540076
36
15.903226
31
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
mod = 1000000007 eps = 10 ** (-9) def main(): import sys input = sys.stdin.readline for _ in range(int(input())): N = int(input()) P = list(map(int, input().split())) ma = [0] * N ma[0] = P[0] for i in range(1, N): ma[i] = max(ma[i - 1], P[i]) ans = [] tmp = [] m = N for i in range(N - 1, -1, -1): p = P[i] tmp.append(p) if p == m: tmp.reverse() ans.extend(tmp) tmp = [] m = ma[i - 1] print(*ans) main()
python
15
0.4814
37
16.576923
26
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] (ind, ce, ans) = ([0] * n, n, []) for i in range(n): ind[n - a[i]] = i for i in ind: if i < ce: ans += a[i:ce] ce = i print(*ans)
python
11
0.47619
38
20
11
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) p = [int(x) for x in input().split()] pref = [0] * n pref[0] = p[0] for i in range(1, n): pref[i] = max(p[i], pref[i - 1]) pp = [0] * n i = n - 1 x = n prev = n start = 0 while i >= 0: while i >= 0 and p[i] != pref[i]: i -= 1 for j in range(max(0, i), prev): pp[start] = p[j] start += 1 prev = i i -= 1 print(*pp)
python
11
0.471939
38
17.666667
21
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
import copy t = int(input()) np = [] ans = [] for _ in range(t): np.append([]) n = int(input()) np[-1].append(n) p = list(map(int, input().split())) np[-1].append(p) for i in range(t): n = np[i][0] p = np[i][1] ans.append([]) index = [0 for _ in range(n)] for j in range(n): index[p[j] - 1] = j biggest_index = n for j in index[::-1]: if j < biggest_index: ans[-1] += p[j:biggest_index] biggest_index = j def print_list(lst): for i in range(len(lst)): if i != len(lst) - 1: print(lst[i], end=' ') else: print(lst[i]) for elem in ans: print_list(elem)
python
13
0.554608
36
17.903226
31
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
t = int(input()) for cs in range(t): n = int(input()) p = [int(s) for s in input().split()] ans = 0 pp = [] mp = [True] * (n + 1) currmax = n ii = n for i in range(n - 1, -1, -1): mp[p[i]] = False if p[i] == currmax: pp += p[i:ii] ii = i while mp[currmax] == False: currmax -= 1 print(*pp)
python
11
0.490506
38
17.588235
17
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
t = int(input()) for _ in range(t): n = int(input()) p = list(map(int, input().split())) max = 0 l = [] for (i, v) in enumerate(p): if max < v: l.append(i) max = v ans = [] f = n for i in l[::-1]: ans += p[i:f] f = i print(*ans, sep=' ')
python
13
0.471042
36
15.1875
16
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
T = int(input()) for t in range(T): N = int(input()) P = list(map(int, input().split())) D = [-1 for n in range(N + 1)] S = [-1 for n in range(N + 1)] for x in range(N): D[P[x]] = x s = N R = [] stop = N while s > 0: if S[s] == -1: start = D[s] for x in range(start, stop): R.append(P[x]) S[P[x]] = 1 stop = start s = s - 1 print(*R)
python
13
0.476839
36
17.35
20
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) maxi = [] curr = -1 pos = -1 for i in range(n): if arr[i] > curr: curr = arr[i] pos = i maxi.append([curr, pos]) ans = [] pos = n - 1 while pos >= 0: t = maxi[pos][1] for i in range(t, pos + 1): ans.append(arr[i]) pos = t - 1 for i in ans: print(i, end=' ') print()
python
13
0.511568
38
16.681818
22
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) (q, z, r) = ([0] * n, n, []) for i in range(n): q[n - a[i]] = i for i in q: if i < z: r += a[i:z] z = i print(*r)
python
13
0.446009
36
18.363636
11
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
def main(): t = int(input()) for _ in range(t): n = int(input()) p = list(map(int, input().split())) taken = [0] * (len(p) + 1) n_max = n last_p = n deck = [] for i in range(n): if p[n - i - 1] == n_max: for j in range(n - i - 1, last_p): deck.append(p[j]) last_p = n - i - 1 for j in range(n_max - 1, 0, -1): if taken[j] == 0: n_max = j break taken[p[n - i - 1]] = 1 s = '' for d in deck: s += str(d) + ' ' print(s) main()
python
15
0.443763
38
19.375
24
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) ind = [0] v = l[0] for i in range(1, n): if l[i] > v: v = l[i] ind.append(i) ind = ind[::-1] en = n ans = [] for i in ind: for j in range(i, en): ans.append(l[j]) en = i print(*ans)
python
13
0.496552
36
16.058824
17
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco
for _ in range(int(input())): n = int(input()) seq = list(map(int, input().split())) locmax = [0] ans = [] for i in range(1, n): if seq[i] > seq[locmax[-1]]: locmax.append(i) locmax = [n] + locmax[::-1] for el in range(len(locmax) - 1): print(*seq[locmax[el + 1]:locmax[el]], end=' ') print()
python
13
0.555195
49
24.666667
12
You have a deck of $n$ cards, and you'd like to reorder it to a new one. Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card. In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.) Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$. Given the original deck, output the deck with maximum possible order you can make using the operation above. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) β€” the size of deck you have. The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) β€” values of card in the deck from bottom to top. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$. -----Output----- For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top. If there are multiple answers, print any of them. -----Examples----- Input 4 4 1 2 3 4 5 1 5 2 4 3 6 4 2 5 3 6 1 1 1 Output 4 3 2 1 5 2 4 3 1 6 1 5 3 4 2 1 -----Note----- In the first test case, one of the optimal strategies is the next one: take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$; take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$; take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$; take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$. In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$. In the second test case, one of the optimal strategies is: take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$; take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$; In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$. In the third test case, one of the optimal strategies is: take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$; take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$. In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$.
taco