比赛 20241023 评测结果 AAAAAAAAAA
题目名称 Farmer John’s Favorite Permutation 最终得分 100
用户昵称 ┭┮﹏┭┮ 运行时间 0.860 s
代码语言 C++ 内存使用 4.29 MiB
提交时间 2024-10-23 09:24:02
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define fi first
#define in inline
#define se second
#define mp make_pair
#define pb push_back
const int N = 1e5+10;

ll read(){
	ll x = 0,f = 1;char c = getchar();
	for(;c < '0' || c > '9';c = getchar())if(c == '-')f = -1;
	for(;c >= '0' && c <= '9';c = getchar())x = (x<<1) + (x<<3) + c-'0';
	return x * f;
}

int n;
int a[N],c[N],p[N];

void solve(){
	n = read();
	memset(c,0,sizeof c);
	for(int i = 1;i < n;i++)a[i] = read(),c[a[i]]++;
	for(int i = 2;i <= n;i++)
		if(c[i] > 1)return printf("-1\n"),void();
	if(c[1] > 2 || c[1] == 0 || a[n-1] != 1)return printf("-1\n"),void();
	if(c[1] == 1){
		for(int i = n-1;i >= 1;i--)printf("%d ",a[i]);
		for(int i = 1;i <= n;i++)
			if(!c[i])printf("%d\n",i);
		return;
	}
	int a1 = 0,a2 = 0;
	for(int i = 1;i <= n;i++)
		if(!c[i]){
			if(!a1)a1 = i;
			else a2 = i;
		}
	p[n] = max(a1,a2),p[1] = min(a1,a2);
	int l = 1,r = n-1;
	p[n-1] = a[1];
//	printf("--- \n");
	for(int i = 2;i <= n-1;i++){
		if(p[r] > p[l])p[--r] = a[i];
		else p[++l] = a[i];
	}
	for(int i = 1;i <= n;i++)printf("%d ",p[i]);
	printf("\n");
}
int main(){
	freopen("permutation.in","r",stdin);
	freopen("permutation.out","w",stdout);
	int t = read();
	while(t--)solve();

	return 0;

}