# Codeforces Round #704 (Div. 2)-B. Card Deck-题解

### 目录

• Codeforces Round #704 (Div. 2)-B. Card Deck
• Problem Description
• Input
• Output
• Sample Input
• Sample Onput
• Note
• 题目大意
• 题目分析
• 解题思路
• AC代码

## Codeforces Round #704 (Div. 2)-B. Card Deck

Time Limit: 1 second
Memory Limit: 512 megabytes

### Problem Description

You have a deck of n n cards, and you’d like to reorder it to a new one.

Each card has a value between 1 1 and n n equal to p i p_i . All p i p_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p 1 p_1 stands for the bottom card, p n p_n is the top card.

In each step you pick some integer k > 0 k > 0 , take the top k 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 ∑ i = 1 n n n − i ⋅ p i \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 t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 ) — the number of test cases.

The first line of each test case contains the single integer n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 ) — the size of deck you have.

The second line contains n n integers p 1 , p 2 , … , p n p_1, p_2,\dots, p_n ( 1 ≤ p i ≤ n 1 \le p_i \le n ; p i ≠ p j p_i \neq p_j if i ≠ j i \neq j ) — values of card in the deck from bottom to top.

It’s guaranteed that the sum of n n over all test cases doesn’t exceed 1 0 5 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.

### Sample Input

4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1


### Sample Onput

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:

In the second test case, one of the optimal strategies is:

In the third test case, one of the optimal strategies is:

# 题目分析

4 2 5 3 6 1中最大的是6，就把6 1移动到（B）

4 2 5 36 1。然后左边没有移动的（A）中最大的是5，就把5 3移动到（B）

4 26 1 5 3。然后左边没有移动的（A）中最大的是4，就把4 2移动到（B）

（A）空，得到（B）6 1 5 3 4 2即为答案。

# 解题思路

4 2 5 3 6 1从第一个数开始往后找，直到找到5>4，然后就把4 2放到输出答案的最后。

5 3 6 14 2。再往后找，6>5，把5 3放到输出答案的最后的前一个。

6 15 3 4 2。最后把6 1放到输出答案的前一个。

6 1 5 3 4 2即为最终答案。

2 4 3 5 1 6

6 1 5 3 4 2

# AC代码

#include
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int a[100010];
void push(stack<int> &s, int l, int r) //从l到r入栈
{

for (int i = r; i >= l; i--) //小的先入栈（从右往左）
s.push(a[i]);
}
/*打印栈*/
void prt(stack<int> &s)
{

while (s.size()) //非空时出栈
{

printf("%d ", s.top());
s.pop();
}
puts(""); //换行
}
int main()
{

int N;
cin >> N;
while (N--) //N组数据
{

stack<int> s;
int n;
cd(n); //scanf("%d",&n);
for (int i = 0; i < n; i++)
cd(a[i]); //scanf("%d",&a[i]);
a[n] = n + 1; //最后设置一个最大的数，保证前面所有都比它小
int M = a[0], last = 0;
for (int i = 0; i <= n; i++)
{

if (a[i] > M) //找到一个更大的数
{

push(s, last, i - 1); //入栈并更新
last = i;
M = a[i];
}
}
prt(s); //打印
}
return 0;
}