欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1492B.Card Deck
Card Deck
You have a deck of cards, and you'd like to reorder it to a new one.
Each card has a value between and equal to . All are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. stands for the bottom card, is the top card.
In each step you pick some integer , take the top 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 um\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 () — the number of test cases.
The first line of each test case contains the single integer () — the size of deck you have.
The second line contains integers (; if ) — values of card in the deck from bottom to top.
It's guaranteed that the sum of over all test cases doesn't exceed .
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.
Note
In the first test case, one of the optimal strategies is the next one:
- take card from the top of and move it to : becomes , becomes ;
- take card from the top of : becomes , becomes ;
- take card from the top of : becomes , becomes ;
- take card from the top of : becomes empty, becomes .
In result, has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ .
In the second test case, one of the optimal strategies is:
- take cards from the top of and move it to : becomes , becomes ;
- take card from the top of and move it to : becomes empty, becomes ;
In result, has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ .
In the third test case, one of the optimal strategies is:
- take cards from the top of and move it to : becomes , becomes ;
- take cards from the top of and move it to : becomes , becomes ;
- take cards from the top of and move it to : becomes empty, becomes .
In result, 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$ .
Samples
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |