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;

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值