308276: CF1492B. Card Deck
Memory Limit:512 MB
Time Limit:1 S
Judge Style:Text Compare
Creator:
Submit:0
Solved:0
Description
Card Deck
题意翻译
你有一叠扑克,共 $n$ 张,第 $i$ 张上有一个 $1$ 到 $n$ 之间的整数 $p_i$。牌从下到上编号,即 $p_1$ 是底下那张牌的数字。所有 $p_i$ 互不相同。 现在你要重排这扑克,每次你可以从原牌堆顶上拿走 $k$ 张,按照原来的顺序放到新牌堆顶上。重复以上过程直到原牌堆空。 定义一个牌堆的值为 $\sum\limits_{i=1}^nn^{n-i}p_i$,求牌堆值最大时的新牌堆。 如果有多种方案,输出任意一种。题目描述
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.输入输出格式
输入格式
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 $ .
输出格式
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.
输入输出样例
输入样例 #1
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
输出样例 #1
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1