Codeforces Round #623 (Div. 2)C. Restoring Permutation

You are given a sequence b1,b2,…,bn. Find the lexicographically minimal permutation a1,a2,…,a2n such that bi=min(a2i−1,a2i), or determine that it is impossible.

Input
Each test contains one or more test cases. The first line contains the number of test cases t (1≤t≤100).

The first line of each test case consists of one integer n — the number of elements in the sequence b (1≤n≤100).

The second line of each test case consists of n different integers b1,…,bn — elements of the sequence b (1≤bi≤2n).

It is guaranteed that the sum of n by all test cases doesn’t exceed 100.

Output
For each test case, if there is no appropriate permutation, print one number −1.

Otherwise, print 2n integers a1,…,a2n — required lexicographically minimal permutation of numbers from 1 to 2n.

Example
input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10

题意:给你n个数的数组b,问是否存在1到2*n的排列a满足b[i]=min(a2i-1,a2i)

思路:首先由题意可知这个数列里必须有1,然后把每个出现的数都做标记,然后对数组b依次找对应的组合,1到2*n必须只出现一次

AC代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int a[100010],b[100010],v[100010],c[100010];
int main()
{
    
    
    int t;
    cin>>t;
    while(t--)
    {
    
    
        int n,f=0;
        cin>>n;
        memset(v,0,sizeof(v));
        for(int i=1;i<=n;i++)
            {
    
    
                cin>>a[i];
                v[a[i]]=1;
                if(v[a[i]]>1)f=1;
            }
            if(v[1]==0)f=1;                                                                                 
        int tot=1;
        for(int i=1;i<=n;i++)
        {
    
    
            c[tot++]=a[i];
            while(v[a[i]])
                {
    
    
                    a[i]++;
                    if(a[i]>2*n)
                        f=1;
                }
            c[tot++]=a[i];
            v[a[i]]=1;
        }
        if(f)
        {
    
    
            cout<<-1<<endl;
            continue;
        }
        for(int i=1;i<=2*n;i++)
            cout<<c[i]<<" ";
        cout<<endl;

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43244265/article/details/104525749