Codeforces Round #704 (Div. 2) B

水题。

B. Card Deck
time limit per test1 second
memory limit per test512 megabytes
inputstandard input
outputstandard output
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.

题目链接:https://codeforces.com/problemset/problem/1492/B

思路:用结构体记录数组的数值和下标。
然后结构体从大到小排序。 依次把当前数值中最大的数的后缀全部移动。

AC代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int b[N],c[N];
int cnt;
int t,n;
struct node
{
	int x,y;
}a[N];
bool cmp(node a,node b)
{
	return a.x>b.x;
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n;
		int res,maxi;
		res=0,maxi=0;
		for(int i=1;i<=n;i++)
		{
			cin>>c[i];
			a[i].x=c[i];
			a[i].y=i;
		}
		cnt=1;
		sort(a+1,a+1+n,cmp);
		for(int i=1;i<=n;i++)
		{
	//		cout<<a[i].x<<" "<<a[i].y<<endl;
		}
		int num=n;
		for(int i=1;i<=num;i++)
		{
			int k=a[i].y;
			if(k>n) continue;
		//	printf("i===%d\n",i);
			for(int j=k;j<=n;j++)
			{
		//		cout<<j<<" ";
				b[cnt++]=c[j];
			}
			n=n-abs(n-a[i].y+1);
		//	printf("n===%d\n",n);
	//		cout<<endl;
		//	break;
		}
		for(int i=1;i<=cnt-1;i++)
		{
			printf("%d ",b[i]);
		}
		cout<<endl;		
	}
	return 0;
 } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值