比赛 20241023 评测结果 AAAAAAAAAA
题目名称 Farmer John’s Favorite Permutation 最终得分 100
用户昵称 darkMoon 运行时间 1.851 s
代码语言 C++ 内存使用 4.37 MiB
提交时间 2024-10-23 08:23:25
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;
auto IN = freopen("permutation.in", "r", stdin);
auto OUT = freopen("permutation.out", "w", stdout);
auto mread = [](){int x;scanf("%lld", &x);return x;};
int t = mread();
int h[100005];
signed main(){
    while(t --){
        int n = mread();
        for(int i = 1; i < n; i ++){
            cin >> h[i];
        }
        if(h[n - 1] != 1){
            printf("-1\n");
            continue;
        }
        bool e[n + 5] = {0};
        int E = 1;
        for(int i = 1; i < n - 1; i ++){
            if(e[h[i]]){
                E = 0;
                break;
            }
            e[h[i]] = 1;
        }
        if(E == 0){
            printf("-1\n");
            continue;
        }
        int a[n + 5] = {0};
        for(int i = 1; i <= n; i ++){
            if(e[i] == 0){
                if(a[1] == 0){
                    a[1] = i;
                }
                else{
                    a[n] = i;
                    break;
                }
            }
        }
        int l = 1, r = n, ma = 1;
        // ma = 0 : the max is on the left
        // ma = 1 : the max is on the right
        for(int i = 1; i < n - 1; i ++){
            if(ma == 0){
                l ++;
                a[l] = h[i];
            }
            else{
                r --;
                a[r] = h[i];
            }
            if(a[r] > a[l]){
                ma = 1;
            }
            else{
                ma = 0;
            }
        }
        for(int i = 1; i <= n; i ++){
            printf("%lld ", a[i]);
        }
        printf("\n");
    }
    return 0;
}