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

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

Portal
Time Limit : 1 second
Memory Limit : 512 megabytes

Problem Description

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

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

In each step you pick some integer k > 0 k > 0 k>0, take the top k 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} i=1nnnipi.

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 t (1 ≤ t ≤ 1000 1 \ le t \ le 10001t1000) — the number of test cases.

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

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

It’s guaranteed that the sum of n n n over all test cases doesn’t exceed 1 0 5 10^5 105.

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:


Topic

There is a stack of cards (A), and you need to move it to another stack (B).
You can take the top few sheets and put them in another stack each time (B).
Until finally all moved from (A) to (B).
The lower the value, the higher the weight, and ask what (B) looks like if the total value is the largest.

Topic analysis

First of all, it is not difficult to see that the big cards are placed at the bottom as far as possible.

excellent
Better than
inferior

So put the largest one at the bottom every time.
At the same time, the one located above the largest must also be moved to (B).
Repeat the operation until all the cards in (A) are moved to (B).

Such as:

4 2 5 3 6 1The largest is 6, so 6 1move to (B)
down
4 2 5 3and 6 1. Then the largest of the left unmoved (A) is 5, so 5 3move to (B)
down
4 2and 6 1 5 3. Then the largest of the unmoved (A) on the left is 4, so 4 2move to (B)
down
(A) is empty, and get (B) 6 1 5 3 4 2as the answer.


Problem solving ideas

Every time you traverse from the beginning to the end to find the maximum, it must time out. It is not difficult to find that every time you find the current maximum, you can put it at the end of the team. O O O ( ( ( n 2 n^{2} n2 ) ) )

The initial maximum value is the first number 4 44
4 2 5 3 6 1 Start from the first number and search backwards until you find 5>4, and then put it4 2at the end of the output answer.
down
5 3 6 1And4 2. Look further, 6>5, and put it5 3to the last one before the output answer.
down
6 1And5 3 4 2. Finally, put it6 1before the output answer.
down
6 1 5 3 4 2That is the final answer.

This process can be implemented with a stack.
To put 4 2it 2into the 4stack first and then into the stack.
Then put 5 3it 3into the 5stack first and then into the stack.
Finally, put 6 1it 1into the 6stack first and then into the stack.
Then the stack is

2 4 3 5 1 6

The stack order is

6 1 5 3 4 2

That is what you want.


AC code

#include <bits/stdc++.h>
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;
}

Guess you like

Origin blog.csdn.net/Tisfy/article/details/114001135