CF1492B.Card Deck

普及-

通过率:0%

时间限制:1.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

Each card has a value between 11 and nn equal to pip_i. All pip_i are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. p1p_1 stands for the bottom card, pnp_n is the top card.

In each step you pick some integer k>0k \gt 0, take the top kk 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=1nnn−i⋅pi\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.

你有一副包含 nn 张牌的牌组,希望将其重排为一副新的牌组。

每张牌的值在 11 到 nn 之间,记为 pip_i。所有 pip_i 互不相同。牌组中的牌从底到顶编号,即 p1p_1 表示最底部的牌,pnp_n 表示最顶部的牌。

在每一步中,你选择某个正整数 k>0k > 0,从原牌组顶部取出最上面的 kk 张牌,并按当前顺序将它们整体放置到新牌组的顶部。重复该操作,直至原牌组为空。(参见“注释”部分以获得更清晰的理解。)

我们定义一副牌组的“顺序”为 ∑i=1nnn−i⋅pi\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}。

给定原始牌组,输出通过上述操作所能得到的、顺序最大的牌组。

输入格式

The first line contains a single integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases.

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

The second line contains nn integers p1,p2,…,pnp_1, p_2,\dots, p_n (1≤pi≤n1 \le p_i \le n; pi≠pjp_i \neq p_j if i≠ji \neq j) — values of card in the deck from bottom to top.

It's guaranteed that the sum of nn over all test cases doesn't exceed 10510^5.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)—— 你所拥有的牌堆的大小。

第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2,\dots, p_n(1≤pi≤n1 \le p_i \le n;当 i≠ji \neq j 时,pi≠pjp_i \neq p_j)—— 牌堆中从底部到顶部各张牌的数值。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

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.

对每个测试用例,输出顺序尽可能大的牌组。按从底到顶的顺序输出牌组中各张牌的数值。

如果存在多个答案,输出任意一个即可。

输入输出样例

  • 输入#1

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

    输出#1

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

说明/提示

In the first test case, one of the optimal strategies is the next one:

  1. take 11 card from the top of pp and move it to p′p': pp becomes [1,2,3][1, 2, 3], p′p' becomes [4][4];
  2. take 11 card from the top of pp: pp becomes [1,2][1, 2], p′p' becomes [4,3][4, 3];
  3. take 11 card from the top of pp: pp becomes [1][1], p′p' becomes [4,3,2][4, 3, 2];
  4. take 11 card from the top of pp: pp becomes empty, p′p' becomes [4,3,2,1][4, 3, 2, 1].

In result, p′p' has order equal to 43⋅4+42⋅3+41⋅2+40⋅14^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1 == 256+48+8+1=313256 + 48 + 8 + 1 = 313.

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

  1. take 44 cards from the top of pp and move it to p′p': pp becomes [1][1], p′p' becomes [5,2,4,3][5, 2, 4, 3];
  2. take 11 card from the top of pp and move it to p′p': pp becomes empty, p′p' becomes [5,2,4,3,1][5, 2, 4, 3, 1];

In result, p′p' has order equal to 54⋅5+53⋅2+52⋅4+51⋅3+50⋅15^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1 == 3125+250+100+15+1=34913125 + 250 + 100 + 15 + 1 = 3491.

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

  1. take 22 cards from the top of pp and move it to p′p': pp becomes [4,2,5,3][4, 2, 5, 3], p′p' becomes [6,1][6, 1];
  2. take 22 cards from the top of pp and move it to p′p': pp becomes [4,2][4, 2], p′p' becomes [6,1,5,3][6, 1, 5, 3];
  3. take 22 cards from the top of pp and move it to p′p': pp becomes empty, p′p' becomes [6,1,5,3,4,2][6, 1, 5, 3, 4, 2].

In result, p′p' has order equal to 65⋅6+64⋅1+63⋅5+62⋅3+61⋅4+60⋅26^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2 == 46656+1296+1080+108+24+2=4916646656 + 1296 + 1080 + 108 + 24 + 2 = 49166.

在第一个测试用例中,一种最优策略如下:

  1. 从 pp 的顶部取 11 张牌并移至 p′p':pp 变为 [1,2,3][1, 2, 3],p′p' 变为 [4][4];
  2. 从 pp 的顶部取 11 张牌:pp 变为 [1,2][1, 2],p′p' 变为 [4,3][4, 3];
  3. 从 pp 的顶部取 11 张牌:pp 变为 [1][1],p′p' 变为 [4,3,2][4, 3, 2];
  4. 从 pp 的顶部取 11 张牌:pp 变为空,p′p' 变为 [4,3,2,1][4, 3, 2, 1]。

最终,p′p' 的序值为 43⋅4+42⋅3+41⋅2+40⋅1=256+48+8+1=3134^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1 = 256 + 48 + 8 + 1 = 313。

在第二个测试用例中,一种最优策略如下:

  1. 从 pp 的顶部取 44 张牌并移至 p′p':pp 变为 [1][1],p′p' 变为 [5,2,4,3][5, 2, 4, 3];
  2. 从 pp 的顶部取 11 张牌并移至 p′p':pp 变为空,p′p' 变为 [5,2,4,3,1][5, 2, 4, 3, 1]。

最终,p′p' 的序值为 54⋅5+53⋅2+52⋅4+51⋅3+50⋅1=3125+250+100+15+1=34915^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1 = 3125 + 250 + 100 + 15 + 1 = 3491。

在第三个测试用例中,一种最优策略如下:

  1. 从 pp 的顶部取 22 张牌并移至 p′p':pp 变为 [4,2,5,3][4, 2, 5, 3],p′p' 变为 [6,1][6, 1];
  2. 从 pp 的顶部取 22 张牌并移至 p′p':pp 变为 [4,2][4, 2],p′p' 变为 [6,1,5,3][6, 1, 5, 3];
  3. 从 pp 的顶部取 22 张牌并移至 p′p':pp 变为空,p′p' 变为 [6,1,5,3,4,2][6, 1, 5, 3, 4, 2]。

最终,p′p' 的序值为 65⋅6+64⋅1+63⋅5+62⋅3+61⋅4+60⋅2=46656+1296+1080+108+24+2=491666^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2 = 46656 + 1296 + 1080 + 108 + 24 + 2 = 49166。

输入解题思路,AI测评打分。不知道怎么写?

首页