本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@TOC
Codeforces Round #704 (Div. 2)-B. Card Deck
传送门 Time Limit: 1 second Memory Limit: 512 megabytes
Problem Description
You have a deck of cards, and you'd like to reorder it to a new one.
Each card has a value between and equal to . All are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. stands for the bottom card, is the top card.
In each step you pick some integer , take the top 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 .
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 () — the number of test cases.
The first line of each test case contains the single integer () — the size of deck you have.
The second line contains integers (; if ) — values of card in the deck from bottom to top.
It's guaranteed that the sum of over all test cases doesn't exceed .
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 3和6 1。然后左边没有移动的(A)中最大的是5,就把5 3移动到(B)![]()
4 2和6 1 5 3。然后左边没有移动的(A)中最大的是4,就把4 2移动到(B)(A)空,得到(B)
6 1 5 3 4 2即为答案。
解题思路
每次从头遍历到尾找最大O$$($$n^{2}$$)肯定要超时。 不难发现其实每次找到当前最大,放到队尾即可。
初始最大值为第一个数
4 2 5 3 6 1从第一个数开始往后找,直到找到5>4,然后就把4 2放到输出答案的最后。![]()
5 3 6 1和4 2。再往后找,6>5,把5 3放到输出答案的最后的前一个。![]()
6 1和5 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;
}
同步发文于我的CSDN