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.
有点类似于栈
主要还是存储一下每个数对应的坐标
然后每次找最大值的坐标向右模拟栈的规律进行输出
#include <iostream>
using namespace std;
const int N=1000010;
int a[N],b[N];
int main() {
int t;
cin>>t;
while(t--) {
int n;
cin>>n;
int n1=n;
for(int i=1; i<=n; i++) {
cin>>a[i];
b[a[i]]=i;
}
int x=b[n];
while(n1) {
int y=0;
for(int i=x; i<=n; i++)
if(a[i]) {
cout<<a[i]<<" ";
a[i]=0;
y++;
}
n1-=y;
x=b[--n];
}
puts("");
}
}