目录

    • 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=1∑n​nn−i⋅pi​.

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 1≤t≤1000) — 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 1≤n≤105) — 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 1≤pi​≤n; 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)长什么样。

题目分析

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


优于

所以每次就把最大的放到最下面。
同时位于最大的上面的也必须连带地移到(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 <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;
}

Codeforces Round #704 (Div. 2)-B. Card Deck-题解相关推荐

  1. Codeforces Round #704 (Div. 2)-A. Three swimmers-题解

    目录 Codeforces Round #704 (Div. 2)-A. Three swimmers Problem Description Input Output Sample Input Sa ...

  2. Codeforces Round #704 (Div. 2)-C. Maximum width-题解

    目录 Codeforces Round #704 (Div. 2)-C. Maximum width Problem Description Input Output Sample Input Sam ...

  3. Codeforces Round #807 (Div. 2)A~E个人题解

    Dashboard - Codeforces Round #807 (Div. 2) - Codeforces A. Mark the Photographer 题意: 有个人,每个人的身高设为,现在 ...

  4. Codeforces Round #706 (Div. 2)-A. Split it!-题解

    目录 Codeforces Round #706 (Div. 2)-A. Split it! Problem Description Input Output Sample Input Sample ...

  5. Codeforces Round #704 (Div. 2)(A ~ E)5题全 超高质量题解【每日亿题2 / 23】

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A.Three swimmers B.Card Deck C.Maximum width D.G ...

  6. Codeforces Round #742 (Div. 2) B、C 题解

    Codeforces Round 742 B. MEXor Mixup 题意 有一个数组,输入两个数a,b,a代表这个数组之外的最小非负整数,b代表这个数组的异或值,问你该数组的最小长度. 思路 首先 ...

  7. Codeforces Round #704 (Div. 2) A-E题解

    A Three swimmers 题意 三个人每人游一个来回时间分别是a.b.c,那么在 a.b.c的倍数时间点上 三个人均会在左边的点,题目问你p时刻来 还要等多久最快遇到三个人 1e18 除法判断 ...

  8. Codeforces Round #704 (Div. 2) E. Almost Fault-Tolerant Database 思维

    传送门 题意: 给nnn个长度为mmm的数组,要求构造一个长度为mmm的数组,使得这个数组与前面nnn个数组同一位置最多两个元素不同. 思路: 我们为了方便构造,可以先把要构造的数组看成nnn个数组的 ...

  9. Codeforces Round #704 (Div. 2) D. Genius‘s Gambit 构造 + 细节

    传送门 题意: 给a,b,ka,b,ka,b,k,要求用aaa个000和bbb个111组成二进制xxx和yyy,并且x−yx-yx−y恰好有kkk个111,并且xxx和yyy不含前导零. 思路: 首先 ...

最新文章

  1. X-007 FriendlyARM tiny4412 u-boot移植之内存初始化
  2. linux的备份和恢复命令,Linux基本命令——备份与恢复文档
  3. BML CodeLab重磅更新:在Windows上可原生Linux AI开发
  4. 简述ospf的工作原理_简述洛氏硬度计的工作原理及应用领域
  5. codeforces1552 D. Array Differentiation(思维+暴力)
  6. python函数的作用域_python学习第五篇 函数 变量作用域
  7. android 打开系统相册_这5款常用Android手机自动化测试工具你要收藏
  8. NUMA全称 Non-Uniform Memory Access,译为“非一致性内存访问”,积极NUMA内存策略
  9. 上海阅文集团android面试题,2018年阅文集团PHP工程师面试题分享
  10. 为初学者介绍10个最常被问到的Javascript问题
  11. c语言万年历程序及注释,c语言万年历程序.doc
  12. Tomcat控制台弱密码漏洞
  13. Linux如何配置DNS服务器
  14. Code Complete阅读笔记(二)
  15. 最新谷歌GOOGLE搜索命令大全
  16. 使用百度统计对网站进行流量分析和统计
  17. 如何制作Chrome扩展?<详细教程>
  18. SVG矢量图中矢量路径的获取
  19. 抖音吸粉_抖音上热门快速吸粉的6个方法,新手建议收藏看看
  20. 根据AD账号直接单点登录到第三方系统

热门文章

  1. Linux下静态库与动态库的引用关系深入分析
  2. 厦门羽燕食品有限公司受邀参加2022世界滋补产业生态大会暨品牌展示会
  3. 求100的阶乘有多少个约数?
  4. Android基础GridView、ListView、Edittext属性大全
  5. db2 修改字段类型 长度
  6. 01-Postman断言-常用断言
  7. 智能车增量式PID算法
  8. speedoffice表格中如何随意拖动表格
  9. 星辰大海,需要门票;诗和远方,需要路费
  10. VS 常见问题之一:error C3867: “CRect::Width”:  函数调用缺少参数列表;请使用“CRect::Width”创建指向成员的指针