当前位置:   article > 正文

Codeforces Round #704 (Div. 2)B.Card Deck_b. card deck time limit per test 1 second memory l

b. card deck time limit per test 1 second memory limit per test 512 megabyte
                                                      B. Card Deck
                                              time limit per test1 second
                                           memory limit per test512 megabytes
                                                   inputstandard input
                                                  outputstandard output
  • 1
  • 2
  • 3
  • 4
  • 5

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

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

In each step you pick some integer k>0, take the top 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=1nnn−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 (1≤t≤1000) — the number of test cases.

The first line of each test case contains the single integer n (1≤n≤105) — the size of deck you have.

The second line contains n integers p1,p2,…,pn (1≤pi≤n; pi≠pj if i≠j) — values of card in the deck from bottom to top.

It’s guaranteed that the sum of n over all test cases doesn’t exceed 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.

思路

我的思路可能较为麻烦,需要先用vis数组标记每一个数在原数组中的位置,输出答案时先从n开始遍历,看n-1这个数在原数组中的位置是否在n的右边,若是,则依次输出原数组中n到n-1这两个数之间的所有大于0的数(包括这两个数),一边输出一边将已输出的数赋值为0,防止后续输出重复的数,具体细节看代码。

#include<bits/stdc++.h>
using namespace std;
 #define ll long long
 const int mod = 1e9 + 7;
int a[100010];
int vis[100010];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		memset(a,0,sizeof(a));
		int n;
		scanf("%d",&n);
		int k = 1;
		for(int i = 1; i <= n; i++)
		{
			scanf("%d",&a[i]);
			vis[a[i]] = i;
		}
		for(int i = n; i >= 1; i--)
		{
			if(vis[i] < vis[i - 1])
			{
				for(int j = vis[i]; j <= vis[i - 1];j++)
				{
					if(a[j] != 0)
					{
						printf("%d ",a[j]);
						a[j] = 0;
					}
				}
			}
			else
			{
				for(int j = vis[i]; j <= n; j++)
				{
					if(a[j] != 0)
					{
						printf("%d ",a[j]);
						a[j] = 0;
					}
				}
				n = vis[i];
			}
		}
		
		printf("\n");
	}
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/54889
推荐阅读
相关标签
  

闽ICP备14008679号