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
for i in range(int(input())): input() baralho = list(map(int, input().split(' '))) baralho_ordenado = len(baralho) * [0] for (indice, carta) in enumerate(baralho): baralho_ordenado[carta - 1] = indice novo_baralho = [] while baralho: indice_maior_carta = baralho_ordenado.pop() if indice_maior_carta < len(baralho): novo_baralho += baralho[indice_maior_carta:] del baralho[indice_maior_carta:] print(' '.join(map(str, novo_baralho)))
python
13
0.677704
47
33.846154
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()) A = list(map(int, input().split())) A.reverse() ans = [] temp = [] ma = -1 for i2 in range(0, n): i = n - i2 - 1 if A[i] > ma: ma = A[i] temp.reverse() for t in temp: ans.append(t) temp = [] temp.append(A[i]) temp.reverse() for t in temp: ans.append(t) ans.reverse() print(*ans)
python
13
0.525886
36
15.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 _ in range(int(input())): n = int(input()) lis = list(map(int, input().split())) look = [0 for i in range(n)] look[0] = lis[0] for i in range(1, n): look[i] = max(look[i - 1], lis[i]) j = n ans = [] for i in range(n - 1, -1, -1): if look[i] == lis[i]: ans.extend(lis[i:j]) j = i print(*ans)
python
13
0.515924
38
21.428571
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 fmax(m, n): p = [0 for i in range(n)] for i in range(len(m)): p[n - m[i]] = i return p for _ in range(int(input())): n = int(input()) m = list(map(int, input().split())) p = fmax(m, n) index = n for i in p: if index > i: print(*m[i:index], end=' ', sep=' ') index = i print()
python
13
0.518395
39
18.933333
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 i in range(int(input())): n = int(input()) deck_list = list(map(int, input().split())) max_list = [] stack = [] max = -1 for card in deck_list: if card > max: max = card max_list.append(max) for i in range(len(deck_list) - 1, -1, -1): if deck_list[i] < max_list[i]: stack.append(deck_list[i]) else: print(deck_list[i], end=' ') while len(stack) > 0: print(stack.pop(), end=' ') print()
python
15
0.57109
44
22.444444
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: n = input() sz = int(n) arr = input().split(' ') arr = [int(x) for x in arr] pr = [0] * sz temp = [] ans = [] for i in range(0, int(n)): if arr[int(n) - 1 - i] == sz: temp.append(arr[int(n) - 1 - i]) for j in range(0, len(temp)): ans.append(temp[len(temp) - 1 - j]) pr[sz - 1] = 1 temp = [] while pr[sz - 1] == 1 and sz > 0: sz = sz - 1 else: pr[arr[int(n) - 1 - i] - 1] = 1 temp.append(arr[int(n) - 1 - i]) for i in ans: print(i, end=' ') print('') t = t - 1
python
17
0.470149
39
20.44
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(): n = int(input()) p = [int(x) for x in input().split()] visited = [False] * (n + 1) res = [] for i in range(n, 0, -1): if not visited[i]: tem = [] while p and p[-1] != i: val = p.pop() visited[val] = True tem.append(val) tem.append(p.pop()) res += tem[::-1] print(*res) for _ in range(int(input())): solve()
python
13
0.512748
38
19.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
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) d = dict() for i in range(n): d[l[i]] = i ans = [] prev = n for i in range(n, 0, -1): if d[i] <= prev: ans += l[d[i]:prev] prev = d[i] print(*ans)
python
13
0.495968
36
18.076923
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
def fun(ls, var): dct = {} for (i, val) in enumerate(ls): dct[val] = i st = sorted(ls) last_pop_index = var ans = [] for i in st[::-1]: get_index = dct.get(i) if get_index < last_pop_index: for j in range(get_index, last_pop_index): ans.append(ls[j]) last_pop_index = get_index print(*ans) T = int(input()) for i in range(T): v = int(input()) ls = list(map(int, input().split())) fun(ls, v)
python
13
0.585132
45
20.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
t = int(input()) for _ in range(t): n = int(input()) p = list(map(int, input().split())) A = [0] * (n + 1) i = 0 while i < n: A[p[i]] = i i += 1 i = n Lj = n + 1 answer = '' L = n while i > 0: j = A[i] if j >= Lj: i -= 1 else: Lj = j l = j while j < L: answer += str(p[j]) + ' ' j += 1 L = l i -= 1 print(answer)
python
16
0.4
36
12.846154
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 solution(): t = int(input()) for _ in range(t): n = int(input()) deck = list(map(int, input().split())) deck.reverse() marked = (n + 1) * [False] ans = [] i = 0 for x in range(n, 0, -1): if not marked[x]: old_i = i while deck[i] != x: marked[deck[i]] = True i += 1 marked[x] = True i += 1 ans.extend(reversed(deck[old_i:i])) print(*ans) solution()
python
16
0.513648
40
19.15
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
for _ in range(int(input())): n = int(input()) a = [*map(int, input().split())] (ans, t) = ([], n) ind = [0] * n for i in range(n): ind[a[i] - 1] = i for i in ind[::-1]: if i < t: ans += a[i:t] t = i print(*ans)
python
12
0.449782
33
18.083333
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
import sys input = sys.stdin.readline def solve(): n = int(input()) p = [0] * (n + 1) a = list(map(int, input().split())) for i in range(n): p[a[i]] = i prev = n res = [] for i in range(n, 0, -1): if p[i] < prev: for j in range(p[i], prev): res.append(a[j]) prev = p[i] print(' '.join(map(str, res))) for i in range(int(input())): solve()
python
13
0.534435
36
18.105263
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 os import sys from io import BytesIO, IOBase 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() def RL(): return map(int, sys.stdin.readline().split()) def RLL(): return list(map(int, sys.stdin.readline().split())) def N(): return int(input()) def S(): return input().strip() def print_list(l): print(' '.join(map(str, l))) for _ in range(N()): n = N() a = RLL() now = 1 m = a[0] t = [] for v in a[1:]: if v > m: t.append(now) (now, m) = (1, v) else: now += 1 t.append(now) ans = [] r = n for v in t[::-1]: for i in range(r - v, r): ans.append(a[i]) r -= v print_list(ans)
python
17
0.634403
72
22.738095
84
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 = [int(x) for x in input().split()] dic = {} for j in range(n): dic[a[j]] = j ma = n temp = [] for j in range(n, 0, -1): if dic[j] < ma: for k in range(dic[j], ma): temp.append(a[k]) ma = dic[j] print(*temp) for _ in range(int(input())): solve()
python
13
0.51495
38
17.8125
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 sys n = int(input()) for _ in range(n): nn = int(input()) cards = list(map(int, sys.stdin.readline().strip().split())) max_ = cards[0] li = [max_] for i in cards[1:]: max_ = max(max_, i) li.append(max_) ali = [] ans = [] pre = None for (i, v) in enumerate(li[::-1]): i = nn - i - 1 if not (pre == None or pre == v): ans.extend(ali[::-1]) ali = [] ali.append(cards[i]) pre = v ans.extend(ali[::-1]) print(' '.join(map(str, ans)))
python
16
0.533333
61
20.136364
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
t = int(input()) r = [] for _ in range(t): n = int(input()) nums = list(input().split()) pos = [0] * n for i in range(n): pos[int(nums[i]) - 1] = i index = 0 end = n - 1 s = '' for i in range(n - 1, -1, -1): if pos[i] > end: continue for j in nums[pos[i]:end + 1]: print(j, end=' ') end = pos[i] - 1
python
12
0.487578
32
17.941176
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 PROBLEM(): for _ in range(int(input())): n = int(input()) P = list(map(int, input().split())) A = [0] * n A[0] = P[0] for i in range(1, n): A[i] = max(P[i], A[i - 1]) j = n T = [] for i in range(n - 1, -1, -1): if P[i] == A[i]: T.extend(P[i:j]) j = i print(*T) PROBLEM()
python
15
0.444805
37
18.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
import math import collections def read_list() -> list: return [int(i) for i in input().strip().split()] def read_num() -> int: return int(input().strip()) t = read_num() for _ in range(t): n = read_num() init = read_list() ans = [] cnt = n - 1 mark = [1] tmp = init[0] for i in range(1, n): if init[i] > tmp: tmp = init[i] mark.append(1) else: mark.append(0) lst = n for cnt in range(n - 1, -1, -1): if mark[cnt] == 1: ans += init[cnt:lst] lst = cnt cnt -= 1 for i in ans: print(i, end=' ') print()
python
12
0.553704
49
16.419355
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
for _ in range(int(input())): n = int(input()) p = list(map(int, input().split())) q = [] ind = [] mi = -1 for i in range(n): if p[i] > mi: ind.append(i) mi = p[i] ind.append(n) n = len(ind) for i in range(n - 1): q += p[ind[n - 2 - i]:ind[n - 1 - i]] print(' '.join([str(o) for o in q]))
python
13
0.488673
39
19.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
t = int(input()) for i in range(t): a = int(input()) l = input().split(' ') m = [] ma = 0 for j in range(a): l[j] = int(l[j]) if l[j] > l[ma]: ma = j m.append(ma) j = a - 1 while j >= 0: y = j j = m[j] for k in range(j, y + 1): print(l[k], end=' ') j -= 1 print()
python
12
0.441781
27
14.368421
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
from sys import stdin, exit from bisect import bisect_left as bl, bisect_right as br from itertools import accumulate yes = lambda : print('YES') no = lambda : print('NO') input = lambda : stdin.readline()[:-1] intput = lambda : int(input()) sinput = lambda : input().split() intsput = lambda : map(int, sinput()) def dprint(*args, **kwargs): if debugging: print(*args, **kwargs) debugging = 1 t = intput() for _ in range(t): n = intput() p = list(intsput()) indexes = {} for (i, x) in enumerate(p): indexes[x] = i ans = [] top = n - 1 target = n while top != -1: loc = indexes[target] if loc <= top: ans += p[loc:top + 1] top = loc - 1 target -= 1 print(' '.join(map(str, ans)))
python
12
0.61669
56
21.806452
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
for _ in range(int(input())): n = int(input()) p = list(map(int, input().split())) st = [] prev = n pos = {} for i in range(n): pos.setdefault(p[i], i) for i in range(n, 0, -1): if pos[i] <= prev: st += p[pos[i]:prev] prev = pos[i] print(*st)
python
13
0.521073
36
19.076923
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
def solve(P, n): moved = [False] * (n + 1) nextMax = n newDeck = [] while len(P): while moved[nextMax]: nextMax -= 1 stack = [] while len(P) and P[-1] != nextMax: stack.append(P.pop()) stack.append(P.pop()) l = len(stack) for i in range(l): newDeck.append(stack.pop()) moved[newDeck[-1]] = True return newDeck t = int(input()) for tc in range(t): n = int(input()) P = list(map(int, input().split())) result = solve(P, n) print(*result)
python
13
0.586354
36
20.318182
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 _ in range(int(input())): n = int(input()) arr = list(map(int, input().strip().split())) new = [0] * (n + 1) point = n lis = [] for x in range(n - 1, -1, -1): if arr[x] == point: lis.append(arr[x]) new[arr[x]] = 1 print(*lis[::-1], end=' ') lis = [] for y in range(point, 0, -1): if new[y] == 0: point = y break else: new[arr[x]] = 1 lis.append(arr[x])
python
15
0.4801
46
20.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
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) new = [] arr_sort = [i for i in arr] arr_sort.sort(reverse=True) memo = {arr[i]: i for i in range(n)} max_index = n indices = [] for j in range(n): i = memo[arr_sort[j]] if i < max_index: indices.append(i) max_index = i max_index = n - 1 for i in indices: j = i while j <= max_index: new.append(arr[j]) j += 1 max_index = i - 1 ans = map(str, new) print(' '.join(ans))
python
13
0.564417
38
20.26087
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 for _ in range(int(input())): n = int(input()) pi = list(map(int, input().split())) dp = [0] * n curr = n right = n val = [] for i in range(n - 1, -1, -1): if pi[i] != curr: dp[pi[i] - 1] = 1 else: dp[pi[i] - 1] = 1 val.append(pi[i:right]) right = i for ii in range(curr, -1, -1): if not dp[ii - 1]: curr = ii break ans = [] for i in val: for ii in i: ans.append(ii) print(*ans)
python
14
0.522678
37
18.291667
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()) lst = list(map(int, input().split())) a = [] p = n pos = {} for i in range(n): pos.setdefault(lst[i], i) for i in range(n, 0, -1): if pos[i] <= p: a += lst[pos[i]:p] p = pos[i] print(*a)
python
13
0.503968
38
18.384615
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
class Solution: def solution(self, nums): points = [] max_val = float('-inf') for i in range(len(nums)): if nums[i] > max_val: max_val = nums[i] points.append(i) result = [] last = len(nums) for point in points[::-1]: result.extend(nums[point:last]) last = point return result t = int(input()) for _ in range(t): n = int(input()) nums = list(map(int, input().split())) sol = Solution() print(str(sol.solution(nums))[1:-1].replace(', ', ' '))
python
13
0.590814
56
21.809524
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 sys, os, io from sys import stdin from math import log, gcd, ceil from collections import defaultdict, deque, Counter from heapq import heappush, heappop from bisect import bisect_left, bisect_right import math alphabets = list('abcdefghijklmnopqrstuvwxyz') def isPrime(x): for i in range(2, x): if i * i > x: break if x % i == 0: return False return True def ncr(n, r, p): num = den = 1 for i in range(r): num = num * (n - i) % p den = den * (i + 1) % p return num * pow(den, p - 2, p) % p def primeFactors(n): l = [] while n % 2 == 0: l.append(2) n = n / 2 for i in range(3, int(math.sqrt(n)) + 1, 2): while n % i == 0: l.append(int(i)) n = n / i if n > 2: l.append(n) return list(set(l)) def power(x, y, p): res = 1 x = x % p if x == 0: return 0 while y > 0: if y & 1 == 1: res = res * x % p y = y >> 1 x = x * x % p return res def SieveOfEratosthenes(n): prime = [True for i in range(n + 1)] p = 2 while p * p <= n: if prime[p] == True: for i in range(p * p, n + 1, p): prime[i] = False p += 1 return prime def countdig(n): c = 0 while n > 0: n //= 10 c += 1 return c def si(): return input() def prefix_sum(arr): r = [0] * (len(arr) + 1) for (i, el) in enumerate(arr): r[i + 1] = r[i] + el return r def divideCeil(n, x): if n % x == 0: return n // x return n // x + 1 def ii(): return int(input()) def li(): return list(map(int, input().split())) def ws(s): sys.stdout.write(s + '\n') def wi(n): sys.stdout.write(str(n) + '\n') def wia(a): sys.stdout.write(' '.join([str(x) for x in a]) + '\n') def power_set(L): cardinality = len(L) n = 2 ** cardinality powerset = [] for i in range(n): a = bin(i)[2:] subset = [] for j in range(len(a)): if a[-j - 1] == '1': subset.append(L[j]) powerset.append(subset) powerset_orderred = [] for k in range(cardinality + 1): for w in powerset: if len(w) == k: powerset_orderred.append(w) return powerset_orderred def fastPlrintNextLines(a): print('\n'.join(map(str, a))) def sortByFirstAndSecond(A): A = sorted(A, key=lambda x: x[0]) A = sorted(A, key=lambda x: x[1]) return list(A) if os.path.exists('input.txt'): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') else: input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline t = 1 t = int(input()) for _ in range(t): n = ii() l = li() d = defaultdict(lambda : 0) for i in range(n): d[l[i]] = i maxpre = [0] * n maxpre[0] = l[0] for i in range(1, n): maxpre[i] = max(maxpre[i - 1], l[i]) ans = [] end = n i = maxpre[-1] while 1: ans += l[i:end] if i == 0: break end = i i = d[maxpre[i - 1]] print(*ans)
python
14
0.567568
61
17.37415
147
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, n): stack = [] final_stack = [] m = float('-inf') for i in arr: if i > m: m = i final_stack.append(stack) stack = [i] else: stack.append(i) final_stack.append(stack) l = [] for i in final_stack[::-1]: l.extend(i) return l for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) print(*solve(arr, n), sep=' ')
python
13
0.562827
38
18.1
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
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) maxi = [l[0]] for i in range(1, n): maxi.append(max(l[i], maxi[i - 1])) ans = [] temp = [] l.append(l[-1]) maxi.append(maxi[-1]) for i in range(n): temp.append(l[i]) if maxi[i] != maxi[i + 1]: ans.append(temp) temp = [] ans.append(temp) for i in range(len(ans) - 1, -1, -1): print(*ans[i], end=' ') print()
python
13
0.536058
38
20.894737
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 zz = 1 sys.setrecursionlimit(10 ** 5) if zz: input = sys.stdin.readline else: sys.stdin = open('input.txt', 'r') sys.stdout = open('all.txt', 'w') di = [[-1, 0], [1, 0], [0, 1], [0, -1]] def fori(n): return [fi() for i in range(n)] def inc(d, c, x=1): d[c] = d[c] + x if c in d else x def ii(): return input().rstrip() def li(): return [int(xx) for xx in input().split()] def fli(): return [float(x) for x in input().split()] def dadd(d, p, val): if p in d: d[p].append(val) else: d[p] = [val] def gi(): return [xx for xx in input().split()] def gtc(tc, ans): print('Case #' + str(tc) + ':', ans) def cil(n, m): return n // m + int(n % m > 0) def fi(): return int(input()) def pro(a): return reduce(lambda a, b: a * b, a) def swap(a, i, j): (a[i], a[j]) = (a[j], a[i]) def prec(a, pre): for i in a: pre.append(pre[-1] + i) pre.pop(0) def si(): return list(input().rstrip()) def mi(): return map(int, input().split()) def gh(): sys.stdout.flush() def isvalid(i, j, n, m): return 0 <= i < n and 0 <= j < m def bo(i): return ord(i) - ord('a') def graph(n, m): for i in range(m): (x, y) = mi() a[x].append(y) a[y].append(x) t = fi() uu = t while t > 0: t -= 1 n = fi() a = li() mix = n vis = {} ans = [] for i in range(n - 1, -1, -1): if a[i] == mix: ans.append(a[i]) print(*ans[::-1], end=' ') ans = [] vis[mix] = 1 while mix in vis: mix -= 1 else: ans.append(a[i]) vis[a[i]] = 1 print(*ans[::-1])
python
14
0.520694
43
14.768421
95
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
entrada = int(input()) for i in range(entrada): inpt = input() size = [int(i) for i in input().split()] ord = [0] * len(size) for (i, card) in enumerate(size): ord[card - 1] = i sizeN = [] while size: i_m_C = ord.pop() if i_m_C < len(size): sizeN += size[i_m_C:] del size[i_m_C:] print(' '.join(map(str, sizeN)))
python
11
0.560241
41
22.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
t = int(input()) for case in range(t): n = int(input()) t = list(map(int, input().split())) maxi_t = [0] * n maxi = t[0] for i in range(n): maxi = max(maxi, t[i]) maxi_t[i] = maxi rep = [] split = n j = n - 1 while j >= 0: while j >= 0 and t[j] != maxi_t[split - 1]: j -= 1 rep += t[j:split] split = j j -= 1 print(*rep)
python
13
0.49711
45
17.210526
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
from math import * import threading import sys from collections import * mod = 10 ** 9 inf = 10 ** 15 yes = 'YES' no = 'NO' def npr(n, r): return factorial(n) // factorial(n - r) if n >= r else 0 def ncr(n, r): return factorial(n) // (factorial(r) * factorial(n - r)) if n >= r else 0 def lower_bound(li, num): answer = -1 start = 0 end = len(li) - 1 while start <= end: middle = (end + start) // 2 if li[middle] >= num: answer = middle end = middle - 1 else: start = middle + 1 return answer def upper_bound(li, num): answer = -1 start = 0 end = len(li) - 1 while start <= end: middle = (end + start) // 2 if li[middle] <= num: answer = middle start = middle + 1 else: end = middle - 1 return answer def abs(x): return x if x >= 0 else -x def binary_search(li, val, lb, ub): ans = -1 while lb <= ub: mid = (lb + ub) // 2 if li[mid] > val: ub = mid - 1 elif val > li[mid]: lb = mid + 1 else: ans = mid break return ans def kadane(x): sum_so_far = 0 current_sum = 0 for i in x: current_sum += i if current_sum < 0: current_sum = 0 else: sum_so_far = max(sum_so_far, current_sum) return sum_so_far def pref(li): pref_sum = [0] for i in li: pref_sum.append(pref_sum[-1] + i) return pref_sum def SieveOfEratosthenes(n): prime = [True for i in range(n + 1)] p = 2 li = [] while p * p <= n: if prime[p] == True: for i in range(p * p, n + 1, p): prime[i] = False p += 1 for p in range(2, len(prime)): if prime[p]: li.append(p) return li def primefactors(n): factors = [] while n % 2 == 0: factors.append(2) n //= 2 for i in range(3, int(sqrt(n)) + 1, 2): while n % i == 0: factors.append(i) n //= i if n > 2: factors.append(n) return factors def prod(li): ans = 1 for i in li: ans *= i return ans def dist(a, b): d = abs(a[1] - b[1]) + abs(a[2] - b[2]) return d def power_of_n(x, n): cnt = 0 while x % n == 0: cnt += 1 x //= n return cnt def ask(l, r): if l == r: return -1 print('?', l, r) sys.stdout.flush() return int(input()) import itertools sys.setrecursionlimit(300000) for _ in range(int(input()) if True else 1): n = int(input()) a = list(map(int, input().split())) d = defaultdict() for i in range(n): d[a[i]] = i ind = [] last = n for i in range(n, 0, -1): for j in range(d[i], last): print(a[j], end=' ') temp = d[i] last = temp print('')
python
13
0.564303
74
16.328571
140
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()) k = list(map(int, input().split())) lista_card = n * [0] for c in range(n): lista_card[k[c] - 1] = c saida = [] lista = k menor = n for e in range(n - 1, -1, -1): indice = lista_card[e] if indice <= menor: saida += k[indice:menor] menor = indice print(' '.join(map(str, saida)))
python
13
0.555241
36
21.0625
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
from sys import stdin, stdout nmbr = lambda : int(stdin.readline()) lst = lambda : list(map(int, stdin.readline().split())) for _ in range(nmbr()): n = nmbr() a = lst() p = 0 p1 = n mx = max(a) dp = [0] * n for i in range(n): dp[i] = max(dp[max(i - 1, 0)], a[i]) for i in range(n): if mx == a[i]: p = i break while p >= 0: for i in range(p, p1): stdout.write(str(a[i]) + ' ') p1 = p p -= 1 while p >= 0: if dp[p] == a[p]: break p -= 1 print()
python
14
0.508264
55
17.615385
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
import math T = int(input()) for t in range(T): n = int(input()) c = list(map(int, input().split())) m = c[0] k = [0] for i in range(1, n): if c[i] > m: k.append(i) m = c[i] j = k[-1] k.pop() s = c[j:] while len(k): s += c[k[-1]:j] j = k[-1] k.pop() print(' '.join(map(str, s)))
python
13
0.468647
36
14.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
from heapq import heappop, heappush, heapify t = int(input()) while t: t -= 1 n = int(input()) p = list(map(int, input().split())) ans = [] temp = [] passed = set() curr_max = n for i in range(n - 1, -1, -1): while curr_max > 0 and curr_max in passed: curr_max -= 1 if p[i] == curr_max: temp.append(p[i]) passed.add(curr_max) temp.reverse() for j in temp: ans.append(j) temp = [] else: temp.append(p[i]) passed.add(p[i]) for i in ans: print(i, end=' ') print()
python
13
0.564103
44
18.5
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
t = int(input()) while t: t = t - 1 n = int(input()) A = [int(x) for x in input().split()] Z = [] B = dict() for x in range(0, n + 1): B[x] = x i = n - 1 s = 0 while i >= 0: s = s + 1 (prev, nm) = B.popitem() if A[i] == nm: Z.extend(A[i:i + s]) s = 0 else: B[prev] = nm try: del B[A[i]] except: a = 1 i = i - 1 print(' '.join(list(map(str, Z))))
python
13
0.439276
38
14.48
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 def solve(cards): segs = [[cards[0]]] temp_max = cards[0] for i in range(len(cards)): if i == 0: continue if cards[i] <= temp_max: segs[-1].append(cards[i]) else: temp_max = cards[i] segs.append([cards[i]]) return ' '.join([str(num) for seg in segs[::-1] for num in seg]) t = int(sys.stdin.readline().strip()) ans = 0 for _ in range(t): n = sys.stdin.readline().strip() line = sys.stdin.readline().strip() cards = list(map(int, line.split())) print(solve(cards))
python
13
0.612774
65
22.857143
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 os import sys import math from io import BytesIO, IOBase input = sys.stdin.readline def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[:len(s) - 1]) def invr(): return map(int, input().split()) def dijkstra(start, distance, path, n): visited = [False for _ in range(n)] distance[start] = 0 for i in range(n): v = -1 for j in range(n): if not visited[v] and (v == -1 or distance[j] < distance[v]): v = j if distance[v] == math.inf: break visited[v] = True for edge in adj[v]: destination = edge[0] weight = edge[1] if distance[v] + weight < distance[destination]: distance[destination] = distance[v] + weight path[destination] = v def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) def lcm(a, b): return a * b // gcd(a, b) def ncr(n, r): return math.factorial(n) // (math.factorial(n - r) * math.factorial(r)) def npr(n, r): return math.factorial(n) // math.factorial(n - r) def seive(n): primes = [True] * (n + 1) ans = [] for i in range(2, n): if not primes[i]: continue j = 2 * i while j <= n: primes[j] = False j += i for p in range(2, n + 1): if primes[p]: ans += [p] return ans def factors(n): factors = [] x = 1 while x * x <= n: if n % x == 0: if n // x == x: factors.append(x) else: factors.append(x) factors.append(n // x) x += 1 return factors def main(): try: for _ in range(inp()): n = inp() a = inlt() d = {} ans = [] for i in range(n): d[a[i]] = i + 1 mx = n start = d[n] while n > 0: ans += a[start - 1:n] n = start - 1 if n <= 0: break while d[mx] > n: mx -= 1 start = d[mx] print(*ans, sep=' ') except Exception as e: print(e) 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.602883
72
20.885906
149
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 for i in range(int(input())): n = int(input()) inpList = list(map(int, input().split())) check_list = OrderedDict.fromkeys(range(1, n + 1)) last = check_list.popitem()[0] new = [] for i in reversed(range(n)): if inpList[i] == last: for j in inpList[i:n]: if j in check_list: check_list.pop(j) new.append(j) n = i if len(check_list) > 0: last = check_list.popitem()[0] new += inpList[i:n] print(' '.join([str(i) for i in new]))
python
14
0.611222
51
26.722222
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
try: t = int(input()) while t != 0: n = int(input()) arr = list(map(int, input().split())) temp = [] for i in range(n): temp.append([arr[i], i]) temp = sorted(temp, key=lambda x: -x[0]) minimum = n p = [] for i in range(n): if i != 0 and minimum > temp[i][1]: p += arr[temp[i][1]:minimum] minimum = temp[i][1] elif i == 0: p += arr[temp[i][1]:minimum] minimum = temp[i][1] print(*p) t -= 1 except EOFError: print(' ')
python
16
0.519313
42
20.181818
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
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) res = [] current = a[0] start = 0 for i in range(1, n): if a[i] > current: sth = a[start:i][::-1] start = i current = a[i] res += sth sth = a[start:][::-1] res += sth for item in res[::-1]: print(item, end=' ') print('')
python
13
0.505952
36
17.666667
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
from sys import stdin, stdout t = int(stdin.readline().strip()) outputs = [] for __ in range(t): n = int(stdin.readline().strip()) a = list(map(int, stdin.readline().strip().split())) maxm_stack = [a[0]] for i in range(1, n): maxm_stack.append(max(maxm_stack[-1], a[i])) (cur, tot, new) = (1, 0, maxm_stack[-1]) req = [] for i in range(n - 1, 0, -1): if new == maxm_stack[i - 1]: req.append(0) tot += 1 else: req.append(cur) cur = cur + tot + 1 new = maxm_stack[i - 1] tot = 0 req.append(cur) req.reverse() prev = None for i in range(n): if req[i] == 0: req[i] = prev + 1 prev = req[i] res = [''] * n for i in range(n): res[req[i] - 1] = f'{a[i]}' outputs.append(' '.join(res)) for output in outputs: stdout.write(output + '\n')
python
15
0.561133
53
22.545455
33
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, cards, nums) = (int(input()), [int(i) for i in input().split()], {}) (ans, last) = ([], n) for i in range(n): nums[cards[i]] = i for i in range(n, 0, -1): if nums[i] <= last: ans += cards[nums[i]:last] last = nums[i] print(*ans)
python
12
0.556962
73
25.333333
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
import sys import math from bisect import bisect_left from collections import defaultdict def get_lcp(s, suffix_array): s = s + '$' n = len(s) lcp = [0] * n pos = [0] * n for i in range(n - 1): pos[suffix_array[i]] = i k = 0 for i in range(n - 1): if k > 0: k -= 1 if pos[i] == n - 1: lcp[n - 1] = -1 k = 0 continue else: j = suffix_array[pos[i] + 1] while max([i + k, j + k]) < n and s[i + k] == s[j + k]: k += 1 lcp[pos[i]] = k return lcp def get_suffix_array(word): suffix_array = [('', len(word))] for position in range(len(word)): sliced = word[len(word) - position - 1:] suffix_array.append((sliced, len(word) - position - 1)) suffix_array.sort(key=lambda x: x[0]) return [item[1] for item in suffix_array] def get_ints(): return map(int, sys.stdin.readline().strip().split()) def bin_search(collection, element): i = bisect_left(collection, element) if i != len(collection) and collection[i] == element: return i else: return -1 def lcm(a, b): m = a * b while a != 0 and b != 0: if a > b: a %= b else: b %= a return m // (a + b) def main(): t = int(input()) for _ in range(t): n = int(input()) p = list(map(int, input().split())) ans = list() pref = [] for i in range(n): if i == 0: pref.append(p[i]) else: pref.append(max(pref[i - 1], p[i])) last = n - 1 for i in range(n - 1, -1, -1): if p[i] == pref[i]: ans.append((i, last)) last = i - 1 for i in range(len(ans)): for j in range(ans[i][0], ans[i][1] + 1): print(p[j], end=' ') print() main()
python
17
0.550917
58
19.802632
76
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())) p = [0] * (n + 1) for (i, x) in enumerate(a): p[x] = i b = [] e = n for x in range(n, 0, -1): if p[x] < e: b.extend(a[p[x]:e]) e = p[x] print(*b)
python
13
0.461538
36
18
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 import math import statistics def function(n, p): max1 = p[0] x = [0] ans = [] m = n for i in range(n): if p[i] > max1: max1 = p[i] x.append(i) x.reverse() for i in x: for j in range(i, m): ans.append(p[j]) m = i print(*ans) input = sys.stdin.read() data = list(map(int, input.split())) t = data[0] l = 0 for i in range(t): n = data[1 + l] p = data[l + 2:l + 2 + n] l = l + 1 + n function(n, p)
python
11
0.543779
36
14.5
28
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
R1 = lambda : list(map(int, input().split())) R2 = lambda : int(input()) t = R2() for _ in range(t): n = R2() A = R1() res = '' maxis = [] m = 0 for i in range(n): if A[i] > m: m = A[i] maxis.append(i) l = n for i in maxis[::-1]: for e in A[i:l]: res += str(e) + ' ' l = i print(res)
python
12
0.474026
45
15.210526
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
from collections import deque def main(): t = int(input()) while t: n = int(input()) L = [int(p) for p in input().split()] temp = [] best = -1 for e in L: if e > best: best = e temp.append(best) res = deque() for i in range(n - 1, -1, -1): res.appendleft(L[i]) if L[i] == temp[i]: print(*res, end=' ') res = deque() print() t -= 1 main()
python
14
0.51436
39
16.409091
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
def solution(): n = int(input()) p = [int(i) for i in input().split()] p_new = [] positions = [-1] * (n + 1) for i in range(n): positions[p[i]] = i last_pos = n for j in range(n, -1, -1): if positions[j] >= last_pos: continue p_new += p[positions[j]:last_pos] last_pos = positions[j] if last_pos == 0: break print(*p_new) for t in range(int(input())): solution()
python
11
0.56701
38
20.555556
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 os import sys from io import BytesIO, IOBase import math import itertools import bisect import heapq def main(): pass 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') def binary(n): return bin(n).replace('0b', '') def decimal(s): return int(s, 2) def pow2(n): p = 0 while n > 1: n //= 2 p += 1 return p def primeFactors(n): l = [] while n % 2 == 0: l.append(2) n = n / 2 for i in range(3, int(math.sqrt(n)) + 1, 2): while n % i == 0: l.append(i) n = n / i if n > 2: l.append(int(n)) return l def isPrime(n): if n == 1: return False else: root = int(n ** 0.5) root += 1 for i in range(2, root): if n % i == 0: return False return True def maxPrimeFactors(n): maxPrime = -1 while n % 2 == 0: maxPrime = 2 n >>= 1 for i in range(3, int(math.sqrt(n)) + 1, 2): while n % i == 0: maxPrime = i n = n / i if n > 2: maxPrime = n return int(maxPrime) def countcon(s, i): c = 0 ch = s[i] for i in range(i, len(s)): if s[i] == ch: c += 1 else: break return c def lis(arr): n = len(arr) lis = [1] * n for i in range(1, n): for j in range(0, i): if arr[i] > arr[j] and lis[i] < lis[j] + 1: lis[i] = lis[j] + 1 maximum = 0 for i in range(n): maximum = max(maximum, lis[i]) return maximum def isSubSequence(str1, str2): m = len(str1) n = len(str2) j = 0 i = 0 while j < m and i < n: if str1[j] == str2[i]: j = j + 1 i = i + 1 return j == m def maxfac(n): root = int(n ** 0.5) for i in range(2, root + 1): if n % i == 0: return n // i return n def p2(n): c = 0 while n % 2 == 0: n //= 2 c += 1 return c def seive(n): primes = [True] * (n + 1) primes[1] = primes[0] = False for i in range(2, n + 1): if primes[i]: for j in range(i + i, n + 1, i): primes[j] = False p = [] for i in range(0, n + 1): if primes[i]: p.append(i) return p def ncr(n, r, p): num = den = 1 for i in range(r): num = num * (n - i) % p den = den * (i + 1) % p return num * pow(den, p - 2, p) % p def denofactinverse(n, m): fac = 1 for i in range(1, n + 1): fac = fac * i % m return pow(fac, m - 2, m) def numofact(n, m): fac = 1 for i in range(1, n + 1): fac = fac * i % m return fac def chk(n, d): if str(d) in str(n): return True return False for xyz in range(0, int(input())): n = int(input()) l = list(map(int, input().split())) ans = [] ind = [-1] * (n + 1) maxtillnow = [] for i in range(0, n): ind[l[i]] = i if i > 0: maxtillnow.append(max(maxtillnow[-1], l[i])) else: maxtillnow.append(l[i]) prevind = n itc = n - 1 while itc != -1: tind = maxtillnow[itc] tind2 = ind[tind] ans += l[tind2:prevind] prevind = tind2 itc = prevind - 1 print(*ans)
python
17
0.582908
72
18.521327
211
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: n = int(input()) list1 = list(map(int, input().split())) maxi = [list1[0]] for i in range(1, n): maxi.append(max(maxi[-1], list1[i])) ind = n - 1 ans = [] while ind >= 0: curr = ind + 1 number = maxi[ind] while ind >= 0 and list1[ind] != number: ind -= 1 ans.extend(list1[ind:curr]) ind -= 1 print(*ans) t -= 1
python
13
0.546448
42
19.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
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) original_deck = list(map(int, input().split())) chkList = [False] * (n + 1) last_num = original_deck[0] chkList[0] = True chkList[n] = True for i in range(1, n): if last_num < original_deck[i]: chkList[i] = True last_num = original_deck[i] for i in range(n - 1, -1, -1): if chkList[i]: j = i + 1 print(original_deck[i], end=' ') while not chkList[j]: print(original_deck[j], end=' ') j += 1 print()
python
14
0.587452
48
22.909091
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 math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time sys.setrecursionlimit(10 ** 7) inf = 10 ** 10 mod = 10 ** 9 + 7 def LI(): return list(map(int, input().split())) def II(): return int(input()) def LS(): return list(input().split()) def S(): return input() def solve(): n = II() cards = LI() cards_max = [0 for _ in range(n)] cards_max[0] = cards[0] for i in range(1, n): cards_max[i] = max(cards_max[i - 1], cards[i]) res = collections.deque() l = n - 1 r = n while r > 0: if cards_max[l] == cards[l]: res.extend(cards[l:r]) l -= 1 r = l + 1 else: l -= 1 res = [str(i) for i in res] print(' '.join(list(res))) def main(): n = II() for i in range(n): solve() return 0 main()
python
12
0.575521
99
16.860465
43
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(p, n): posicoes = [0] * n for j in range(n): posicoes[p[j] - 1] = j K = [posicoes[n - 1]] for j in range(n - 2, -1, -1): if posicoes[j] < K[-1]: K.append(posicoes[j]) result = [0] * n right = n pos = 0 for left in K: for j in range(right - left): result[pos] = p[left + j] pos += 1 right = left return result t = int(input()) for i in range(t): n = int(input()) p = list(map(int, input().split())) print(' '.join(map(str, solve(p, n))))
python
13
0.546218
39
20.636364
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 _ in range(int(input())): n = int(input()) l = list(map(int, input().strip().split())) ah = [i for i in range(n)] d = dict(zip(l, ah)) lan = [] d[0] = -1 t = n a = n while t > 0: lan += l[d[t]:a] a = d[t] for i in range(t - 1, -1, -1): if d[i] < a: t = i break print(*lan)
python
15
0.463816
44
16.882353
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
import os import sys from io import BytesIO, IOBase 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') def intArr(): return map(int, input().split()) def func(arr): n = len(arr) if n == 1: return [1] l1 = [0] * n for i in range(n): l1[arr[i] - 1] = i answer = [0] * n idx = 0 ex = n curr = n while idx < n: p = l1[curr - 1] while p < ex and idx < n: k = arr[p] answer[idx] = k l1[k - 1] = -1 p += 1 idx += 1 ex = n - idx while curr > 0 and l1[curr - 1] == -1: curr -= 1 return answer def main(): for _ in range(int(input())): _ = int(input()) arr = list(intArr()) print(*func(arr)) return main()
python
17
0.626228
72
23.238095
84
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()) nums = [int(tmp) for tmp in input().split()] flags = [False] * (n + 1) cur = n - 1 max_cur = n while cur >= 0: while flags[max_cur]: max_cur -= 1 prev_cur = cur while nums[cur] != max_cur: flags[nums[cur]] = True cur -= 1 flags[nums[cur]] = True for tmp in nums[cur:prev_cur + 1]: print(tmp, end=' ') cur -= 1 print()
python
12
0.547264
45
20.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
for _ in range(int(input())): n = int(input()) D = dict() res = [] index = 0 A = [int(i) for i in input().split()] for i in A: D[i] = index index += 1 x = n j = n while D[x] != 0: if D[x] < j: res += A[D[x]:j] j = D[x] x -= 1 res += A[D[x]:j] print(*res)
python
12
0.45
38
14.555556
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 os import sys from io import BytesIO, IOBase import math from decimal import * getcontext().prec = 25 MOD = pow(10, 9) + 7 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') for _ in range(int(input())): n = int(input()) l = list(map(int, input().split(' '))) f = [0] * n for i in range(n): f[l[i] - 1] = i r = [] z = n for i in range(n - 1, -1, -1): if f[i] < z: r += l[f[i]:z] z = f[i] print(*r)
python
17
0.64053
72
26.439394
66
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, io, os, math from math import ceil, log, gcd, sqrt from itertools import permutations import operator mod = 1000000007 mod1 = 998244353 def intinp(): return int(sys.stdin.readline()) def strinp(): return sys.stdin.readline() def arrinp(): return list(map(int, sys.stdin.readline().strip().split())) def mulinp(): return map(int, sys.stdin.readline().strip().split()) def flush(): return stdout.flush() def power_two(x): return 1 << x def lcm(a, b): return a * b // gcd(a, b) def onescomp(num, d): return (1 << d) - 1 ^ num def solve(): n = intinp() t = arrinp() grp = [[t[0]]] for i in range(1, n): if grp[-1][0] > t[i]: grp[-1].append(t[i]) else: grp.append([t[i]]) grp.sort(reverse=True) for i in grp: print(*i, end=' ') print() def main(): tc = intinp() while tc: solve() tc -= 1 main()
python
15
0.618935
60
15.568627
51
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 collections as cc import math as mt input = sys.stdin.readline I = lambda : list(map(int, input().split())) for tc in range(int(input())): (n,) = I() ar = I() ans = [] fl = n te = n visi = [0] * (n + 1) visi[n] = 1 for i in range(n - 1, -1, -1): visi[ar[i]] = 1 if ar[i] == fl: ans += ar[i:te] te = i while visi[fl]: fl -= 1 ans += ar[:te] print(*ans)
python
12
0.526448
44
17.045455
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
t = int(input()) def ceil(a, b): if a % b: return a // b + 1 return a // b for _ in range(t): n = int(input()) l = list(map(int, input().split())) maxi = [0] for i in range(n): maxi.append(max(maxi[-1], l[i])) del maxi[0] j = n - 1 ans = [] while j >= 0: m = 1 j -= 1 while j >= 0 and maxi[j] == maxi[j + 1]: j -= 1 m += 1 e = j + 1 while m: ans.append(l[e]) e += 1 m -= 1 print(*ans)
python
13
0.467136
42
14.777778
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
casos = int(input()) for a in range(casos): uso = [] tamanho = int(input()) final = [0] * tamanho sequencia = list(map(int, input().split())) menor = len(sequencia) maior = len(sequencia) for b in range(tamanho): final[sequencia[b] - 1] = b for c in range(-1, -1 * len(final) - 1, -1): if final[c] < menor: menor = final[c] for d in range(menor, maior, 1): uso.append(sequencia[d]) maior = menor print(*uso)
python
13
0.608295
45
24.529412
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()) p = list(map(int, input().split())) l2 = [] a = p[0] s = [0] for i in range(1, n): if p[i] > a: a = p[i] s.append(i) s = s[::-1] ind = n for j in s: l2.extend(p[j:ind]) ind = j print(*l2)
python
13
0.474708
36
15.0625
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 operator t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) maxi = 0 new_a = [] local_max_points = [] for i in range(len(a)): if a[i] > maxi: maxi = a[i] local_max_points.append(i) last_point = len(a) for point in local_max_points[::-1]: new_a += a[point:last_point] last_point = point print(*new_a)
python
13
0.592896
37
20.529412
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
import sys from itertools import accumulate import os from io import BytesIO, IOBase from math import ceil 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') T = int(input()) for _ in range(T): n = int(input()) arr = list(map(int, input().split())) pos = {v: i for (i, v) in enumerate(arr)} ans = [] right = n for i in range(n, 0, -1): if pos[i] > right: continue ans.extend(arr[pos[i]:right]) right = pos[i] print(*ans)
python
17
0.660274
72
27.515625
64
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 from collections import defaultdict from sys import stdin input = stdin.readline T = int(input()) for _ in range(T): n = int(input()) arr = list(map(int, input().split())) l = [] for i in range(n): l.append([arr[i], i]) l.sort(reverse=True) ans = [] s = set() for i in l: ind = i[1] while ind < n: if ind in s: break else: ans.append(arr[ind]) s.add(ind) ind += 1 print(*ans)
python
14
0.588235
38
16.708333
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
t = int(input()) for _ in range(t): n = int(input()) p = list(map(int, input().split())) curMaxs = [0 for _ in range(n)] for i in range(1, n): curMaxs[i] = curMaxs[i - 1] if p[i] > p[curMaxs[i]]: curMaxs[i] = i x = n - 1 res = [] while x >= 0: res += p[curMaxs[x]:x + 1] x = curMaxs[x] - 1 print(' '.join([str(x) for x in res]))
python
13
0.517241
39
22.2
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
n = int(input()) for i in range(n): resp = '' tam = int(input()) posicoes = [0] * (tam + 1) y = list(map(int, input().strip().split()))[:tam] x = [0] x.extend(y) for i in range(1, tam + 1): posicoes[x[i]] = i a = tam b = tam + 1 while a > 0: if posicoes[a] >= b: a -= 1 else: for j in range(posicoes[a], b): resp += str(x[j]) resp += ' ' b = posicoes[a] print(resp.strip())
python
16
0.507353
50
18.428571
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 re import sys exit = sys.exit from bisect import bisect_left as bsl, bisect_right as bsr from collections import Counter, defaultdict as ddict, deque from functools import lru_cache cache = lru_cache(None) from heapq import * from itertools import * from math import inf from pprint import pprint as pp enum = enumerate ri = lambda : int(rln()) ris = lambda : list(map(int, rfs())) rln = sys.stdin.readline rl = lambda : rln().rstrip('\n') rfs = lambda : rln().split() d4 = [(0, -1), (1, 0), (0, 1), (-1, 0)] d8 = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)] t = ri() for _ in range(t): n = ri() p = ris() hp = [] for i in range(n): heappush(hp, (-p[i], i)) ans = [] k = n while hp: (_, i) = heappop(hp) if i >= k: continue for j in range(i, k): ans.append(p[j]) k = i print(*ans)
python
11
0.589499
75
22.277778
36
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()) lis = list(map(int, input().split())) index = [] maxa = 0 for i in range(len(lis)): if lis[i] > maxa: index.append(i) maxa = lis[i] index = index[::-1] l = lis[index[0]:] for i in range(1, len(index)): l.extend(lis[index[i]:index[i - 1]]) print(*l)
python
13
0.561905
38
21.5
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
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) seen = set(list(range(1, n + 1))) search = n i = n - 1 ans = [] while i >= 0: while search not in seen: search -= 1 start = i temp = [] while arr[i] != search: temp.append(arr[i]) seen.remove(arr[i]) i -= 1 temp.append(arr[i]) seen.remove(arr[i]) ans += temp[::-1] i -= 1 print(*ans)
python
13
0.535802
38
18.285714
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 int_fn(): return int(input()) def str_fn(): return input() def int_list_fn(): return [int(val) for val in input().split(' ')] def solve(n, cards): hash_arr = [0] * (n + 1) for (idx, card) in enumerate(cards): hash_arr[card] = idx i = n output = [] limit = n while i > 0: start_from = hash_arr[i] if start_from > limit: i -= 1 continue for j in range(start_from, limit): output.append(str(cards[j])) limit = start_from i -= 1 print(' '.join(output)) return for _ in range(int_fn()): n = int_fn() cards = int_list_fn() solve(n, cards) pass
python
13
0.593103
48
17.125
32
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 = list(map(int, input().split())) l1 = p.copy() l1.sort() m = n - 1 d1 = dict() for i in range(n): d1[l1[i]] = i ans = [] store = n for i in range(n - 1, -1, -1): if p[i] == l1[m]: ans += p[i:store] store = i l1[d1[p[i]]] = -1 while l1[m] == -1 and m > -1: m -= 1 else: l1[d1[p[i]]] = -1 print(*ans)
python
13
0.45974
36
17.333333
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 sys import math def read_ints(): inp = input().split() inp = [int(x) for x in inp] return inp def read_strings(): inp = input() s = [inp[i] for i in range(len(inp))] return s t = int(input()) for _ in range(t): n = int(input()) p = read_ints() elements = [1 for i in range(len(p))] ind = n - 1 i = n - 1 ans = [] while i >= 0: tmp = [] while p[i] != ind + 1 and i >= 0: elements[p[i] - 1] = 0 tmp.append(p[i]) i -= 1 if i < 0: break tmp.append(p[i]) for j in range(len(tmp) - 1, -1, -1): ans.append(tmp[j]) elements[p[i] - 1] = 0 i -= 1 ind -= 1 while elements[ind] == 0 and ind >= 0: ind -= 1 ans = [str(x) for x in ans] print(' '.join(ans))
python
12
0.526316
40
17.5
38
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()) arr = [int(k) for k in input().split()] mapping = {} for (i, num) in enumerate(arr): mapping[num] = i res = [] right = N for i in range(N, 0, -1): idx = mapping[i] if idx >= right: continue res += arr[idx:right] right = idx for x in res: (print(x, end=' '),) print()
python
11
0.549419
40
18.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
class SparseTable: def __init__(self, A, ide_ele, f): self.n = len(A) self.f = f max_k = self.n.bit_length() - 1 self.ide_ele = ide_ele self.table = [[ide_ele] * (max_k + 1) for i in range(self.n)] for i in range(self.n): self.table[i][0] = A[i] for k in range(1, max_k + 1): k2 = 1 << k - 1 k3 = (1 << k) - 1 for i in range(self.n - k3): self.table[i][k] = self.f(self.table[i][k - 1], self.table[i + k2][k - 1]) def query(self, l, r): if l >= r: return self.ide_ele d = r - l if d == 1: return self.table[l][0] k = (d - 1).bit_length() - 1 k2 = 1 << k return self.f(self.table[l][k], self.table[r - k2][k]) INF = 10 ** 18 import sys import io, os input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline t = int(input()) for _ in range(t): n = int(input()) P = list(map(int, input().split())) st = SparseTable(P, 0, max) ans = [] cur = n - 1 while cur >= 0: M = st.query(0, cur + 1) temp = [] for i in range(cur, -1, -1): temp.append(P[i]) if P[i] == M: cur = i - 1 break temp.reverse() for p in temp: ans.append(p) print(*ans)
python
16
0.535714
78
22.333333
48
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 sys import stdin from bisect import bisect_right, bisect_left from math import ceil, log input = stdin.readline def main(args): hammings = [] temp = set() for i in range(ceil(log(1000000.0, 2)) + 1): for j in range(ceil(log(1000000.0, 3)) + 1): for k in range(ceil(log(1000000.0, 5)) + 1): ans = 2 ** i * 3 ** j * 5 ** k temp.add(ans) hammings = list(temp) hammings.sort() while True: try: (m, n) = map(int, input().split(' ')) except ValueError: break s = bisect_left(hammings, m) t = bisect_right(hammings, n) print(t - s) main(sys.argv[1:])
python
15
0.622483
47
22.84
25
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
MAX = 1000000 t = [0] * (MAX + 5) a5 = 1 for i in range(9): a3 = 1 for j in range(13): a35 = a5 * a3 if a35 > MAX: break a2 = 1 for k in range(20): if a35 * a2 > MAX: break t[a35 * a2] = 1 a2 <<= 1 a3 *= 3 a5 *= 5 while 1: a = input() if a == '0': break (m, n) = map(int, a.split()) print(sum(t[m:n + 1]))
python
11
0.472141
29
13.826087
23
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
while 1: datas = list(map(int, input().split())) if datas[0] == 0: break (n, m) = (datas[0], datas[1]) cnt = 0 for i in range(n, m + 1): b = i while b % 5 == 0: b //= 5 while b % 3 == 0: b //= 3 while b % 2 == 0: b //= 2 if b == 1: cnt += 1 print(cnt)
python
13
0.443262
40
15.588235
17
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
MAX = 1000000 hamming_list = [False] * (MAX + 1) hamming_list[0] = False hamming_list[1] = True for index in range(2, MAX + 1): if index % 2 == 0: if hamming_list[index // 2]: hamming_list[index] = True elif index % 3 == 0: if hamming_list[index // 3]: hamming_list[index] = True elif index % 5 == 0: if hamming_list[index // 5]: hamming_list[index] = True while True: input_data = input() if input_data == '0': break (start, end) = [int(item) for item in input_data.split(' ')] count = sum(hamming_list[start:end + 1]) print(count)
python
11
0.621864
61
25.571429
21
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
while 1: n = list(map(int, input().split())) if n[0] == 0: break a = 0 for i in range(n[0], n[1] + 1): b = i while b % 5 == 0: b /= 5 while b % 3 == 0: b /= 3 while b % 2 == 0: b /= 2 if b == 1: a += 1 print(a)
python
13
0.4125
36
14
16
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
H = [False for i in range(1000001)] H[1] = True for i in range(20): for j in range(13): for k in range(9): if 2 ** i * 3 ** j * 5 ** k < 1000001: H[2 ** i * 3 ** j * 5 ** k] = True else: break while True: L = input() if L == '0': break (a, b) = [int(i) for i in L.split()] ans = 0 for i in range(a, b + 1): if H[i]: ans += 1 print(ans)
python
15
0.489071
41
18.263158
19
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
def hammingp(n): while n % 2 == 0: n /= 2 while n % 3 == 0: n /= 3 while n % 5 == 0: n /= 5 return n == 1 while True: s = list(map(int, input().strip().split())) if len(s) == 1: break (m, n) = s c = 0 for i in range(m, n + 1): if hammingp(i): c += 1 print(c)
python
15
0.478723
44
14.666667
18
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
isHamming = [] def judge(): global isHamming isHamming[1] = True i = 1 while True: if i * 2 > 1000000: break if isHamming[i]: isHamming[i * 2] = True i += 1 i = 1 while True: if i * 3 > 1000000: break if isHamming[i]: isHamming[i * 3] = True i += 1 i = 1 while True: if i * 5 > 1000000: break if isHamming[i]: isHamming[i * 5] = True i += 1 def init(): global isHamming for i in range(1000000 + 1): isHamming.append(False) init() judge() while True: m = [int(num) for num in input().split()] if m == [0]: break count = 0 for i in range(m[0], m[1] + 1): if isHamming[i]: count += 1 print(count)
python
11
0.574695
42
14.619048
42
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
while 1: n = list(map(int, input().split())) if n[0] == 0: break a = 0 for i in range(n[0], n[1] + 1): b = i while b % 2 == 0: b /= 2 while b % 3 == 0: b /= 3 while b % 5 == 0: b /= 5 if b == 1: a += 1 print(a)
python
13
0.4125
36
14
16
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
while True: A = list(map(int, input().split())) if A[0] == 0: break (n, m) = (A[0], A[1]) ans = 0 for i in range(n, m + 1): b = i while b % 5 == 0: b //= 5 while b % 3 == 0: b //= 3 while b % 2 == 0: b //= 2 if b == 1: ans += 1 print(ans)
python
13
0.416357
36
14.823529
17
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
twos = [2 ** i for i in range(21) if 2 ** i <= 1000000] threes = [3 ** i for i in range(21) if 2 ** i <= 1000000] fives = [5 ** i for i in range(21) if 2 ** i <= 1000000] muls = [x * y * z for x in twos for y in threes for z in fives] muls.sort() def under(n): cnt = 0 for i in muls: if i <= n: cnt += 1 else: break return cnt while True: s = input() if s == '0': break (m, n) = map(int, s.split()) print(under(n) - under(m - 1))
python
10
0.546667
63
21.5
20
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
MAX = 1000000 hamming_list = [False] * (MAX + 1) hamming_list[0] = False hamming_list[1] = True for index in range(2, MAX + 1): if index / 2 % 1 == 0: if hamming_list[index // 2]: hamming_list[index] = True elif index / 3 % 1 == 0: if hamming_list[index // 3]: hamming_list[index] = True elif index / 5 % 1 == 0: if hamming_list[index // 5]: hamming_list[index] = True while True: input_data = input() if input_data == '0': break (start, end) = [int(item) for item in input_data.split(' ')] count = sum(hamming_list[start:end + 1]) print(count)
python
11
0.614035
61
26.142857
21
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
while True: n = input() if n == '0': break (m, n) = map(int, n.split()) cnt = 0 for i in range(m, n + 1): num = i while num % 5 == 0: num /= 5 while num % 3 == 0: num /= 3 while num % 2 == 0: num /= 2 if num == 1: cnt += 1 print(cnt)
python
9
0.454545
29
14.529412
17
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
import sys from sys import stdin from bisect import bisect_right, bisect_left input = stdin.readline def main(args): hammings = [] temp = set() for i in range(21): for j in range(14): for k in range(9): ans = 2 ** i * 3 ** j * 5 ** k temp.add(ans) hammings = list(temp) hammings.sort() while True: try: (m, n) = map(int, input().split(' ')) except ValueError: break s = bisect_left(hammings, m) t = bisect_right(hammings, n) print(t - s) main(sys.argv[1:])
python
15
0.616633
44
19.541667
24
The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hamming numbers. For example * 1 * 1 x 2 x 2 = 4 * 1 x 2 x 2 x 3 x 5 x 5 = 300 Etc. are humming numbers, but 11, 13, 14 etc. are not humming numbers. All humming numbers are divisible by a power of 60 (for example, 54 is divisible by 603 = 21600), so they have long been known as convenient numbers for sexagesimal calculations such as time. In just intonation, which is one of the scales used for tuning musical instruments, the ratio of the frequencies of the sounds is a sequence of humming numbers of 24, 27, 30, 32, 36, 40, 45, 48. Create a program that takes integers m and n as inputs and outputs the number of humming numbers that are m or more and n or less. Input A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. For each dataset, two integers m and n (1 ≀ m, n ≀ 1000000, m ≀ n) are given on one line, separated by blanks. The number of datasets does not exceed 20. Output Outputs the number of humming numbers from m to n for each data set on one line. Example Input 3 8 1 27 1 86 0 Output 5 17 31
taco
class Solution: def minimum_Number(self, s): l = list(s) l.sort() for i in range(len(l)): if int(l[i]) > 0: (l[0], l[i]) = (l[i], l[0]) break n = '' for i in l: n += i return n
python
13
0.478049
31
14.769231
13
Given a number s(in string form). Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of given number. Example 1: Input: s = "846903" Output: 304689 Explanation: 304689 is the smallest number by rearranging the digits. Example 2: Input: s = "55010" Output: 10055 Explanation: 10055 is the smallest number by rearranging the digts. Your Task: You don't need to read or print anything. Your task is to complete the function minimum_number() which takes the number as input parameter and returns the smallest number than can be formed without leading zeros by rearranging the digits of the number. Expected Time Complexity: O(N * log(N)) where N is the number of digits of the given number Expected Space Complexity: O(1) Constraints: 1 <= N <= 10^{5}
taco
class Solution: def minimum_Number(self, s): d = {} for i in s: i = int(i) if i in d: d[i] += 1 else: d[i] = 1 t = list(d.keys()) t.sort() if len(t) == 1: return str(t[0]) * d[t[0]] res = str(t[1] * 10 + t[0]) (d[t[0]], d[t[1]]) = (d[t[0]] - 1, d[t[1]] - 1) for i in t: res += str(i) * d[int(i)] return res
python
13
0.437143
49
17.421053
19
Given a number s(in string form). Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of given number. Example 1: Input: s = "846903" Output: 304689 Explanation: 304689 is the smallest number by rearranging the digits. Example 2: Input: s = "55010" Output: 10055 Explanation: 10055 is the smallest number by rearranging the digts. Your Task: You don't need to read or print anything. Your task is to complete the function minimum_number() which takes the number as input parameter and returns the smallest number than can be formed without leading zeros by rearranging the digits of the number. Expected Time Complexity: O(N * log(N)) where N is the number of digits of the given number Expected Space Complexity: O(1) Constraints: 1 <= N <= 10^{5}
taco
class Solution: def minimum_Number(self, s): sort = sorted(s) s = '' i = 0 while sort[i] == '0' and i < len(sort) - 1: i += 1 if i == len(sort): for ele in sort: s += ele temp = sort[0] sort[0] = sort[i] sort[i] = temp for ele in sort: s += ele return s
python
11
0.519031
45
16
17
Given a number s(in string form). Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of given number. Example 1: Input: s = "846903" Output: 304689 Explanation: 304689 is the smallest number by rearranging the digits. Example 2: Input: s = "55010" Output: 10055 Explanation: 10055 is the smallest number by rearranging the digts. Your Task: You don't need to read or print anything. Your task is to complete the function minimum_number() which takes the number as input parameter and returns the smallest number than can be formed without leading zeros by rearranging the digits of the number. Expected Time Complexity: O(N * log(N)) where N is the number of digits of the given number Expected Space Complexity: O(1) Constraints: 1 <= N <= 10^{5}
taco
class Solution: def minimum_Number(self, s): arr = [] new = [] s = list(s) for i in s: if int(i) == 0: new.append(int(i)) else: arr.append(int(i)) if len(new) == len(s): return ''.join(s) arr.sort() new1 = [arr.pop(0)] new1.extend(new) new1.extend(arr) ans = '' for i in new1: ans += str(i) ans = int(ans) return ans
python
15
0.534247
29
15.590909
22
Given a number s(in string form). Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of given number. Example 1: Input: s = "846903" Output: 304689 Explanation: 304689 is the smallest number by rearranging the digits. Example 2: Input: s = "55010" Output: 10055 Explanation: 10055 is the smallest number by rearranging the digts. Your Task: You don't need to read or print anything. Your task is to complete the function minimum_number() which takes the number as input parameter and returns the smallest number than can be formed without leading zeros by rearranging the digits of the number. Expected Time Complexity: O(N * log(N)) where N is the number of digits of the given number Expected Space Complexity: O(1) Constraints: 1 <= N <= 10^{5}
taco