欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1579E1.Permutation Minimization by Deque
Permutation Minimization by Deque
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems.
A permutation of size is given. A permutation of size is an array of size in which each integer from to occurs exactly once. For example, and are correct permutations while and are not.
Let us consider an empty deque (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements currently in the deque, adding an element to the beginning will produce the sequence , and adding same element to the end will produce .
The elements of the permutation are sequentially added to the initially empty deque, starting with and finishing with . Before adding each element to the deque, you may choose whether to add it to the beginning or the end.
For example, if we consider a permutation , one of the possible sequences of actions looks like this:
| 1. | add to the end of the deque: | deque has a sequence in it; |
|---|---|---|
| 2. | add to the beginning of the deque: | deque has a sequence in it; |
| 3. | add to the end of the deque: | deque has a sequence in it; |
| 4. | add to the end of the deque: | deque has a sequence in it; |
Find the lexicographically smallest possible sequence of elements in the deque after the entire permutation has been processed.
A sequence is lexicographically smaller than the sequence if there exists such that , , , and . In other words, if the sequences and have some (possibly empty) matching prefix, and the next element of the sequence is strictly smaller than the corresponding element of the sequence . For example, the sequence is smaller than the sequence because after the two matching elements in the start the first sequence has an element which is smaller than the corresponding element in the second sequence.
Input
The first line contains an integer () — the number of test cases.
The next lines contain descriptions of the test cases.
The first line of each test case description contains an integer () — permutation size. The second line of the description contains space-separated integers (; all are all unique) — elements of the permutation.
It is guaranteed that the sum of over all test cases does not exceed .
Output
Print lines, each line containing the answer to the corresponding test case. The answer to a test case should contain space-separated integer numbers — the elements of the lexicographically smallest permutation that is possible to find in the deque after executing the described algorithm.
Note
One of the ways to get a lexicographically smallest permutation from the permutation (the first sample test case) is described in the problem statement.
Samples
5
4
3 1 2 4
3
3 2 1
3
3 1 2
2
1 2
2
2 1
1 3 2 4
1 2 3
1 3 2
1 2
1 2
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |