zoukankan      html  css  js  c++  java
  • Codeforces 1315C Restoring Permutation

    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有数和它搭配,所以贪心是正确的
  • 相关阅读:
    使用FileReader在浏览器读取预览文件(image和txt)
    移动端Vue图片获取,压缩,预览组件-upload-img(H5+获取照片,压缩,预览)
    文件(图片)转base64
    Vue单页面应用打包app处理返回按钮
    hbuilder/hbuilderx 无法检测到模拟器
    不启动AndroidStudio直接启动其模拟器
    ES6,箭头函数 (=>)注意点
    ES6,扩展运算符
    strcmp使用注意
    android11 gc5035前置摄像头当作后置使用左右镜像问题
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/12364866.html
Copyright © 2011-2022 走看看