显示代码纯文本
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 2e5 + 10;
int T;
int n;
pair<int, int> a[MAXN];
int main() {
freopen("Lineup.in", "r", stdin);
freopen("Lineup.out", "w", stdout);
cin >> T;
while (T--) {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i].first;
a[i].second = i;
}
sort(a + 1, a + n + 1, [](auto x, auto y) {
if (x.first != y.first) return x.first > y.first;
return x.second < y.second;
});
int prepre = 0, pre = 0, flag = 0;
for (int i = 1; i <= n; ++i) {
int val = a[i].first, id = a[i].second;
if (id > pre) {
prepre = pre, pre = id;
cout << val << " ";
} else if (!flag && prepre < id) {
pre = id, flag = 1;
cout << val << " ";
}
}
cout << endl;
}
return 0;
}