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(a2i−1,a2i)bi=min(a2i−1,a2i) , or determine that it is impossible.
Each test contains one or more test cases. The first line contains the number of test cases tt (1≤t≤1001≤t≤100 ).
The first line of each test case consists of one integer nn — the number of elements in the sequence bb (1≤n≤1001≤n≤100 ).
The second line of each test case consists of nn different integers b1,…,bnb1,…,bn — elements of the sequence bb (1≤bi≤2n1≤bi≤2n ).
It is guaranteed that the sum of nn by all test cases doesn't exceed 100100 .
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 .
5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8
1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
题意很简单。因为有要求:1.bi=min(a2i−1,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有数和它搭配,所以贪心是正确的。