码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 1315C Restoring Permutation

时间:2020-02-26 01:36:30      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:ima   pie   nsis   view   phi   mat   for   org   elements   

You are given a sequence b1,b2,,bnb1,b2,…,bn . Find the lexicographically minimal permutation a1,a2,,a2na1,a2,…,a2n such that bi=min(a2i1,a2i)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 tt (1t1001≤t≤100 ).

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

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

It is guaranteed that the sum of nn by all test cases doesn‘t exceed 100100 .

Output

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

Otherwise, print 2n2n integers a1,,a2na1,…,a2n  — required lexicographically minimal permutation of numbers from 11 to 2n2n .

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 
题意很简单。因为有要求:1.bi=min(a2i1,a2i),2.字典序尽可能
#include <bits/stdc++.h>
using namespace std;
int b[205];
int out[205];
bool vis[205];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int i,j;
        int flag=1;
        memset(vis,0,sizeof(vis));
        for(i=1;i<=n;i++)
        {
            scanf("%d",&b[i]);
            vis[b[i]]=1;
            out[2*i-1]=b[i];
        }

        for(i=1;i<=n;i++)
        {
            int find=0;

            for(j=1;j<=2*n;j++)
            {
                if(vis[j])continue;
                if(j>out[2*i-1])
                {
                    out[2*i]=j;
                    find=1;
                    vis[j]=1;
                    break;
                }
            }

            if(!find)
            {
                 flag=0;
                 break;
            }
        }

        if(!flag)
        {
            cout<<-1<<endl;
            continue;
        }
        for(i=1;i<=2*n;i++)
        {
            cout<<out[i]<< ;
        }
        cout<<endl;
    }
    return 0;
}

 

小。所以肯定要把bi放到2i-1的位置(观察样例也不难看出)。然后就是在剩下的数里从小到大地找,找到第一个比bi大的数填到a2i的位置,没有这么一个数的话返回-1.至于正确性的话可以这么想,假设b数组里靠前的是比较小的数,那么从剩下的数里从小到大地选,能保证剩下的里较大的数留给后面更大的bi;假设b数组里靠前的是比较大的数,挑过以后肯定还能保证后面较小的bi有数和它搭配,所以贪心是正确的

Codeforces 1315C Restoring Permutation

标签:ima   pie   nsis   view   phi   mat   for   org   elements   

原文地址:https://www.cnblogs.com/lipoicyclic/p/12364866.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!