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 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 1000 1t1000) — 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^5 1n105) — 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 ≤ p i ≤ n 1 \le p_i \le n 1pin; 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:


题目大意

有一摞牌(A),要把它移成另一摞(B)。
每次可以拿走最上面的几张放到另一摞(B)。
直到最终全部由(A)移到(B)。
越往下权值越高,问总值最大的话(B)长什么样。

题目分析

首先不难看出牌大的尽量放到最下面。

Codeforces Round #704 (Div. 2)-B. Card Deck-题解_第1张图片
优于
Codeforces Round #704 (Div. 2)-B. Card Deck-题解_第2张图片

所以每次就把最大的放到最下面。
同时位于最大的上面的也必须连带地移到(B)去。
重复操作直到(A)中牌全部移动到(B)。

如:

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即为答案。


解题思路

每次从头遍历到尾找最大 O O O ( ( ( n 2 n^{2} n2 ) ) )肯定要超时。
不难发现其实每次找到当前最大,放到队尾即可。

初始最大值为第一个数 4 4 4
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即为最终答案。

这个过程可以用栈来实现。
要放4 2,就先2入栈后4入栈。
然后放5 3,就先3入栈后5入栈。
最后放6 1,就先1入栈后6入栈。
然后栈为

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;
}

你可能感兴趣的:(CodeForces,题解,题解)