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

The meaning of the question: give you an array b of n numbers, and ask whether there is an arrangement a of 1 to 2*n that satisfies b[i]=min(a2i-1,a2i)

Idea: First of all, from the meaning of the question, we know that there must be 1 in this sequence, and then mark every number that appears, and then find the corresponding combination of array b in turn, 1 to 2*n must appear only once

AC code:

#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;

    }
}

Guess you like

Origin blog.csdn.net/weixin_43244265/article/details/104525749