Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int mpow(int base, int exp);
void solve() {
long long int n;
cin >> n;
vector<long long int> v(n);
vector<long long int> cnt(2 * n + 1, 0);
for (long long int i = 0; i < (n); i++) {
cin >> v[i];
cnt[v[i]]++;
}
vector<long long int> out;
for (long long int i = 0; i < (n); i++) {
out.push_back(v[i]);
bool flag = true;
for (long long int j = v[i] + 1; j <= 2 * n; j++) {
if (cnt[j] == 0) {
out.push_back(j);
cnt[j]++;
flag = false;
break;
}
}
if (flag) {
cout << -1 << '\n';
return;
}
}
for (auto i : out) cout << i << " ";
cout << '\n';
;
}
int main() {
int t;
cin >> t;
while (t--) solve();
return 0;
}
int mpow(int base, int exp) {
base %= 1000000007;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((long long int)result * base) % 1000000007;
base = ((long long int)base * base) % 1000000007;
exp >>= 1;
}
return result;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | t = int(input())
for _ in range(t):
n = int(input())
l = list(map(int,input().split()))
done = l[::]
result = []
for i in range(n):
val = l[i]
flag = 0
for j in range(val+1,2*n+1):
if(j not in done):
done.append(j)
result.extend([val,j])
flag = 1
break
if(flag!=1):
break
if(flag==0):
print(-1)
else:
print(*result,sep=' ') | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | t = int(input())
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
b = [None]*(2*n)
c = set(a)
d = set(range(1, 2*n+1))
for i in range(len(a)):
x = a[i]+1
while x in c:
x += 1
else:
q = 2*i
w = 2*i+1
b[q] = a[i]
b[w] = x
c.add(x)
count = 1
if c != d:
print('-1')
else:
if count == 1 or 1:
for i in range(len(b)):
print(b[i], end=' ')
print()
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | for _ in range(int(input())):
n = int(input())
b = list(map(int, input().split()))
a = [-1] * 2 * n
for x in range(1, 2 * n + 1):
if x in b:
i = 2 * b.index(x)
a[i] = x
else:
i = -1
for j in range(0, 2 * n, 2):
if a[j] != -1 and a[j] < x and \
a[j + 1] == -1:
i = j
break
if i != -1:
a[i + 1] = x
if any(map((-1).__eq__, a)):
print(-1)
else:
print(*a)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
int main() {
int i, j, k, t, n, l, largest_element;
scanf("%d", &t);
int result[t][200];
int largest_element_array[t];
for (i = 0; i < t; i++) {
int count = 0;
scanf("%d", &n);
largest_element = 2 * n;
largest_element_array[i] = largest_element;
int array_given[n];
for (j = 0; j < n; j++) {
scanf("%d", &array_given[j]);
}
int smallest_element = 100000000;
for (j = 0; j < n; j++) {
if (array_given[j] < smallest_element) {
smallest_element = array_given[j];
}
}
for (j = 0; j < n; j++) {
if (array_given[j] >= largest_element) {
result[i][0] = -1;
result[i][1] = 0;
count += 1;
break;
}
}
int n_r_elements = 0;
if (!count) {
int array_remaining[largest_element];
for (k = smallest_element + 1; k <= largest_element; k++) {
int exists = 0;
for (l = 0; l < n; l++) {
if (k == array_given[l]) {
exists = 1;
break;
}
}
if (!exists) {
array_remaining[n_r_elements] = k;
n_r_elements += 1;
}
}
if (n_r_elements < n) {
result[i][0] = -1;
result[i][1] = 0;
count += 1;
} else {
int array[n];
for (k = 0; k < n; k++) {
array[k] = 1;
}
for (k = 0; k < n; k++) {
result[i][2 * k] = array_given[k];
}
for (k = 0; k < n; k++) {
count = 0;
result[i][2 * k + 1] = 0;
for (l = 0; l < n; l++) {
if (array[l] && (array_remaining[l] > array_given[k])) {
result[i][2 * k + 1] = array_remaining[l];
count += 1;
}
if (count) {
array[l] = 0;
break;
}
}
}
for (k = 0; k < n; k++) {
if (result[i][2 * k + 1] == 0) {
result[i][0] = -1;
result[i][1] = 0;
}
}
}
}
}
for (i = 0; i < t; i++) {
for (j = 0; (result[i][j] && (j < largest_element_array[i])); j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | t = int(input())
for i in range(t):
n = int(input())
b = list(map(int, input().split()))
a = [0 for i in range(n+n)]
vis = [0 for i in range(n+n+1)]
for i in range(n):
a[2*i] = b[i]
vis[b[i]] = 1
ans = 1
for i in range(n):
ok = 0
for j in range(b[i]+1, n+n+1):
if vis[j] == 1:
continue
a[2*i+1] = j
vis[j] = 1
ok = 1;
break
if ok == 0:
ans = 0
break
if ans == 0:
print(-1)
else:
for i in range(n+n):
print(a[i], end = ' ')
print() | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int f(map<int, int> &mp, int b[], int n, int l) {
bool x = 0;
for (int j = l + 1; j <= 2 * n; j++) {
if (!mp[j]) {
mp[j] = 1;
x = 1;
return j;
}
}
return 0;
}
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
int a[n + 3], b[2 * n + 5];
map<int, int> mp;
for (int i = 1; i <= n; i++) cin >> a[i], mp[a[i]]++;
bool y = 0;
int x = 1;
for (int i = 1; i <= 2 * n; i += 2, x++) {
int k = f(mp, b, n, a[x]);
if (!k) {
y = 1;
break;
}
b[i + 1] = k;
b[i] = a[x];
}
if (y) {
cout << "-1\n";
continue;
}
x = 1;
for (int i = 1; i <= 2 * n; i += 2, x++) {
if (a[x] == min(b[i], b[i + 1])) continue;
cout << "-1\n";
y = 1;
break;
}
if (!y) {
for (int i = 1; i <= 2 * n; i++) cout << b[i] << " \n"[i == 2 * n];
}
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n, p = 0;
cin >> n;
int a[210], b[105], maxm = 0;
for (int i = 1; i <= n; i++) cin >> b[i];
bool g[210];
memset(g, false, sizeof(g));
for (int i = 1; i <= 2 * n; i += 2) a[i] = b[(i + 1) / 2], g[a[i]] = true;
for (int i = 2; i <= 2 * n; i += 2) {
for (int j = 1; j <= 200; j++)
if (!g[j] && j > a[i - 1]) {
a[i] = j;
g[j] = true;
max(j, maxm) == j ? maxm = j : 0;
break;
}
}
for (int i = 1; i <= maxm; i++) {
if (!g[i]) {
p = 1;
}
}
if (p == 1) {
cout << -1 << endl;
} else {
for (int i = 1; i <= 2 * n; i++) cout << a[i] << " ";
cout << endl;
}
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int INF = 1e18;
void solve() {
long long int n;
cin >> n;
long long int a[n], b[2 * n];
for (long long int i = 0; i < n; i++) cin >> a[i];
set<long long int> s;
for (long long int i = 1; i < 2 * n + 1; i++) s.insert(i);
long long int ind = 0;
for (long long int i = 0; i < n; i++) {
s.erase(s.find(a[i]));
}
for (long long int i = 0; i < 2 * n; i++) {
b[i] = a[ind];
auto it = s.lower_bound(a[ind]);
if (it == s.end()) {
cout << -1 << "\n";
return;
}
b[i + 1] = *it;
s.erase(it);
i++;
ind++;
}
for (long long int i = 0; i < 2 * n; i++) cout << b[i] << " ";
cout << "\n";
return;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
long long int t = 1;
cin >> t;
for (long long int i = 1; i < t + 1; i++) {
solve();
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | /* / οΎοΎβ β β β β β β β β β β β γ
/ )\β β β β β β β β β β β β Y
(β β | ( Ν‘Β° ΝΚ Ν‘Β°οΌβ β(β γ
(β οΎβ Y βγ½-γ __οΌ
| _β qγ| γq |/
(β γΌ '_δΊΊ`γΌ οΎ
β |\ οΏ£ _δΊΊ'彑οΎ
β )\β β qβ β /
β β (\β #β /
β /β β β /α½£====================D-
/β β β /β \ \β β \
( (β )β β β β ) ).β )
(β β )β β β β β ( | /
|β /β β β β β β | /
[_] β β β β β [___] */
// Main Code at the Bottom
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;
public class Main {
//Fast IO class
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
boolean env=System.getProperty("ONLINE_JUDGE") != null;
if(!env) {
try {
br=new BufferedReader(new FileReader("src\\input.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
else br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static long MOD=1000000000+7;
//Euclidean Algorithm
static long gcd(long A,long B){
return (B==0)?A:gcd(B,A%B);
}
//Modular Exponentiation
static long fastExpo(long x,long n){
if(n==0) return 1;
if((n&1)==0) return fastExpo((x*x)%MOD,n/2)%MOD;
return ((x%MOD)*fastExpo((x*x)%MOD,(n-1)/2))%MOD;
}
//Modular Inverse
static long inverse(long x) {
return fastExpo(x,MOD-2);
}
//Prime Number Algorithm
static boolean isPrime(long n){
if(n<=1) return false;
if(n<=3) return true;
if(n%2==0 || n%3==0) return false;
for(int i=5;i*i<=n;i+=6) if(n%i==0 || n%(i+2)==0) return false;
return true;
}
//Reverse an array
static void reverse(int arr[],int l,int r){
while(l<r) {
int tmp=arr[l];
arr[l++]=arr[r];
arr[r--]=tmp;
}
}
//Print array
static void print1d(int arr[]) {
out.println(Arrays.toString(arr));
}
static void print2d(int arr[][]) {
for(int a[]: arr) out.println(Arrays.toString(a));
}
// Pair
static class pair{
int x,y;
pair(int a,int b){
this.x=a;
this.y=b;
}
public boolean equals(Object obj) {
if(obj == null || obj.getClass()!= this.getClass()) return false;
pair p = (pair) obj;
return (this.x==p.x && this.y==p.y);
}
public int hashCode() {
return Objects.hash(x,y);
}
}
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
//Main function(The main code starts from here)
public static void main (String[] args) throws java.lang.Exception {
int test;
//test=1;
test=sc.nextInt();
while(test-->0){
int n=sc.nextInt(),b[]=new int[n],a[]=new int[2*n],visited[]=new int[2*n+1];
for(int i=0;i<n;i++) b[i]=sc.nextInt();
for(int i=0;i<n;i++) {
a[2*i]=b[i];
visited[b[i]]=1;
}
for(int i=1;i<=2*n;i++) {
if(visited[i]==1) continue;
for(int j=0;j<2*n;j++) if(a[j]==0 && a[j-1]<i) {
a[j]=i;
break;
}
visited[i]=1;
}
//print1d(a);
Arrays.fill(visited, 0);
for(int i=0;i<2*n;i++) {
if(a[i]!=0)visited[a[i]]=1;
}
int sum=0;
for(int i: visited) sum+=i;
//out.println(sum);
if(sum!=2*n) out.println(-1);
else {
for(int x: a) out.print(x+" ");
out.println();
}
}
out.flush();
out.close();
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | from sys import stdin, stdout
from collections import Counter, defaultdict
from itertools import permutations, combinations
raw_input = stdin.readline
pr = stdout.write
mod=10**9+7
def ni():
return int(raw_input())
def li():
return map(int,raw_input().split())
def pn(n):
stdout.write(str(n)+'\n')
def pa(arr):
pr(' '.join(map(str,arr))+'\n')
# fast read function for total integer input
def inp():
# this function returns whole input of
# space/line seperated integers
# Use Ctrl+D to flush stdin.
return map(int,stdin.read().split())
range = xrange # not for python 3.0+
for t in range(ni()):
n=ni()
l=li()
d=Counter(l)
arr=[]
for i in range(1,2*n+1):
if not d[i]:
arr.append(i)
f=0
ans=[]
for i in range(n):
f1=0
for j in arr:
if j>l[i]:
f1=1
break
if not f1:
f=1
break
ans.append(l[i])
ans.append(j)
arr.remove(j)
if f:
pn(-1)
else:
pa(ans)
| PYTHON |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | for t in range(int(input())):
n = int(input())
b = list(map(int,input().split()))
a = [0]*(2*n+1)
res = []
f = True
for i in b:
a[i] = 1
for i in b:
# print("bv",i)
res.append(i)
val = 0
# print("A",a)
for j in range(i,2*n+1):
if a[j] != 1:
val = j
break
# print("f",val)
if val == 0:
f = False
break
a[val] = 1
res.append(val)
if f:
print(*res)
else:
print(-1)
# print(res) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | for _ in range(int(input())):
n=int(input())
b=list(map(int,input().split()))
d={}
for i in range(1,2*n+1):
d[i]=1
for i in b:
d[i]=0
a=[]
for i in b:
a.append(i)
z=i
for j in range(z+1,2*n+1):
if d[j]:
z=j
d[j]=0
break
if z!=i:
a.append(z)
else:
break
if len(a)==2*n:
print(' '.join(map(str,a)))
else:
print(-1) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | from sys import stdin
input = stdin.readline
def mp():return map(int,input().split())
def it():return int(input())
for _ in range(it()):
n=it()
l=list(mp())
v=[]
flag=0
for i in range(n):
v.append(l[i])
k=l[i]
while k in v or k in l:
k+=1
if k>2*n:
# print(-1)
flag=1
break
v.append(k)
if flag==0:
print(*v)
else:
print(-1)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import sys
#input=sys.stdin.readline
t=int(input())
while t:
t-=1
n=int(input())
b=list(map(int,input().split()))
l=[i for i in range(1,2*n+1) if i not in b]
ans=[]
k=0
for i in range(n):
c=0
ans.append(b[i])
for j in range(n):
if l[j]>b[i] and l[j] not in ans:
c=1
break
if c==1:
ans.append(l[j])
else:
k=1
print(-1)
break
if k==1:
continue
else:
print(*ans)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
vector<int> solve(int N, const vector<int>& B) {
vector<int> pos(2 * N + 1, -1);
for (int i = 0, _n = (N); i < _n; ++i) {
int x = B[i];
pos[x] = i;
}
set<int> missing;
for (int n = (1), _b = (2 * N); n <= _b; ++n) {
if (pos[n] < 0) missing.insert(n);
}
vector<int> A(2 * N);
for (int i = 0, _n = (N); i < _n; ++i) {
int x = B[i];
auto it = missing.upper_bound(x);
if (it == missing.end()) return {};
A[i * 2] = x;
A[i * 2 + 1] = *it;
missing.erase(it);
}
return A;
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int TC;
cin >> TC;
for (int tc = (1), _b = (TC); tc <= _b; ++tc) {
int N;
cin >> N;
vector<int> B(N);
for (int i = 0, _n = (N); i < _n; ++i) cin >> B[i];
vector<int> A = solve(N, B);
if (A.empty())
cout << "-1\n";
else {
for (int x : A) cout << x << ' ';
cout << '\n';
}
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 |
import java.util.HashSet;
import java.util.Scanner;
public class B16 {
static int max = 11;
public static void main(String[] args) {
Scanner nik = new Scanner(System.in);
int t = nik.nextInt();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < t; i++) {
}
A: while (t-- > 0) {
int n = nik.nextInt();
int b[] = new int[n + 1];
HashSet<Integer> hs = new HashSet<>();
for (int i = 1; i <= n; i++) {
b[i] = nik.nextInt();
hs.add(b[i]);
}
int a[] = new int[2 * n + 1];
int val = 1;
for (int i = 1; i <= n; i++) {
int v = b[i];
a[2 * i - 1] = v;
val = v + 1;
while (hs.contains(val))
val++;
if (val > 2 * n) {
sb.append(-1 + "\n");
continue A;
}
hs.add(val);
a[2 * i] = val;
}
for (int i = 1; i < a.length; i++)
sb.append(a[i] + " ");
sb.append("\n");
}
System.out.print(sb);
}
}
| JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
istream::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q;
cin >> q;
for (; q > 0; q--) {
int n;
cin >> n;
int b[n];
bool mk[n + n + 10];
for (int i = 0; i <= n + n; i++) mk[i] = 0;
for (int i = 0; i < n; i++) {
cin >> b[i];
mk[b[i]] = 1;
}
int a[n + n];
bool gd = 1;
for (int i = 0; i < n; i++) {
int nm = -1;
for (int j = b[i]; j <= n + n; j++)
if (!mk[j]) {
nm = j;
break;
}
if (nm == -1) {
gd = 0;
cout << -1 << endl;
break;
}
mk[nm] = 1;
a[(i + 1) * 2 - 1] = nm;
a[(i + 1) * 2 - 2] = b[i];
}
if (!gd) continue;
for (int i = 0; i < n + n; i++) cout << a[i] << " ";
cout << endl;
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | for _ in range(int(input())):
n=int(input())
b=[int(x) for x in input().split()]
a=[]
for i in range(n):
a.append(b[i])
k=b[i]
while(k in a or k in b):k+=1
if(k>2*n):print(-1);break
a.append(k)
else:
print(*a) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long INF = 1 << 28;
const double pi = acos(-1.0);
int fx[] = {1, -1, 0, 0};
int fy[] = {0, 0, 1, -1};
int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
int knight[8][2] = {1, 2, 1, -2, 2, 1, 2, -1, -1, 2, -1, -2, -2, 1, -2, -1};
const long double EPS = 1e-7;
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
bool cmp(int a, int b) { return a < b; }
int on(int mask, int st) { return mask | (1 << st); }
int off(int mask, int st) { return mask & (~(1 << st)); }
const int mx = 600100;
int mod = 998244353;
int vis[400];
pair<int, int> arr[mx];
int ans[1000];
int main() {
int t;
scanf("%d", &t);
while (t--) {
memset(ans, 0, sizeof(ans));
memset(vis, 0, sizeof(vis));
int n;
int x;
scanf("%d", &n);
int f = 1;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
vis[x]++;
ans[(i * 2) - 1] = x;
}
int l = 1;
int r = 1;
while (r <= 2 * n) {
if (vis[r] == 0) {
int flag = 0;
for (int i = 1; i <= n; i++) {
if (ans[(i * 2) - 1] < r && ans[i * 2] == 0) {
flag = 1;
ans[i * 2] = r;
break;
}
}
if (flag == 0) {
f = 0;
break;
}
}
r++;
}
if (f == 0)
cout << -1 << endl;
else {
for (int i = 1; i <= 2 * n; i++) printf("%d ", ans[i]);
cout << endl;
}
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long power(long long a, long long b, long long m = mod) {
long long x = 1;
while (b) {
if (b & 1) {
x = 1ll * x * a % m;
}
a = 1ll * a * a % m;
b /= 2;
}
return x;
}
const int N = 1e5 + 9;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
vector<int> a(n + 1), taken(2 * n + 1), b(2 * n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
b[2 * i - 1] = a[i];
taken[a[i]] = 1;
}
set<int> ss;
for (int i = 1; i <= 2 * n; i++) {
if (taken[i] == 0) ss.insert(i);
}
bool can = 1;
for (int i = 0; i < n; i++) {
auto it = ss.upper_bound(b[2 * i + 1]);
if (it == ss.end()) {
can = 0;
break;
}
b[2 * i + 2] = *it;
ss.erase(it);
}
if (can) {
for (int i = 1; i <= 2 * n; i++) {
cout << b[i] << " ";
}
cout << "\n";
} else {
cout << "-1\n";
}
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.util.*;
import java.io.*;
public class c {
static boolean debug = false;
public static void main(String[] args) {
FS in = new FS(System.in);
PrintWriter out = new PrintWriter(System.out);
int numT = in.nextInt();
for(int t=0; t<numT; t++) {
int n = in.nextInt();
int[] b = new int[n];
int[] bvalLocs = new int[2*n+1];
boolean[] used = new boolean[2*n+1];
Arrays.fill(bvalLocs, -1);
for(int i=0; i<n; i++) {
b[i] = in.nextInt();
bvalLocs[b[i]] = i;
used[b[i]] = true;
}
if(debug) System.out.println(Arrays.toString(used));
// Impossible cases =
// a[i] = max(b[2*i-1], b[2])
// If not enough numbers (used up already)
// If 1 isn't there lol.
// for(int bind=n-1; bind>=0; bind--) {
// int aind = bind*2;
// }
int[] a = new int[n*2];
int aind = 0;
boolean impossible = false;
// for(int bval=1; bval<=2*n; bval++) {
for(int bind=0; bind<n; bind++) {
// Search for it
// int bind = bvalLocs[bval];
// if(bind == -1) continue;
int bval = b[bind];
if(debug) System.out.printf("bval=%d bind=%d\n", bval, bind);
// So now we have the correct thing.
// Greedily place the next highest number.
aind = bind*2;
a[aind] = bval; // This is the min of the two, will always be first
aind++;
int nextUse = -1;
for(int i=bval; i<=2*n; i++) {
if(!used[i]) {
// This is the next value we place.
nextUse = i;
break;
}
}
if(debug) System.out.printf("Placing [%d]=%d\n", aind, nextUse);
// If we can't place a next one, impossible
if(nextUse == -1) {
impossible = true;
break;
}
a[aind] = nextUse;
aind++;
used[nextUse] = true;
}
if(impossible) {
out.println("-1");
} else {
for(int i=0; i<2*n; i++) {
out.print(a[i] + " ");
}
out.println();
}
out.flush();
}
}
static class FS {
BufferedReader in;
StringTokenizer token;
public FS(InputStream stream) {
in = new BufferedReader(new InputStreamReader(stream));
}
public String next() {
if(token == null || !token.hasMoreElements()) {
try{
token = new StringTokenizer(in.readLine());
} catch (Exception e) {}
return next();
}
return token.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
/*
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
*/ | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
const long long MX = 1e18, N = 1e4 + 10;
const double PI = 3.141592653589793238;
using namespace std;
long long n, m, ans[N], a[N], t;
map<int, int> mp;
int main() {
cin >> t;
while (t--) {
cin >> n;
bool yes = true;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
++mp[a[i]];
}
for (int i = 1; i <= n; ++i) {
bool ok = false;
ans[i * 2 - 1] = a[i];
for (int j = a[i] + 1; j <= n * 2; ++j) {
if (!mp[j]) {
ans[i * 2] = j;
mp[j]++;
ok = true;
break;
}
}
if (!ok) yes = false;
}
if (yes) {
for (int i = 1; i <= n * 2; i++) cout << ans[i] << ' ';
} else
cout << -1;
cout << '\n';
mp.clear();
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | t = int(input())
while t:
t += -1
n = int(input())
l = list(map(int, input().split()))
check = [0] * ((2 * n) + 1)
ans = [0] * (2 * n)
for i in range(0, 2 * n, 2):
ans[i] = l[i // 2]
check[ans[i]] = 1
# print(ans)
c = 1
for i in range(0, 2 * n, 2):
chintu = 0
for j in range(ans[i] + 1, 2 * n + 1):
if check[j] == 0:
chintu = 1
check[j] = 1
ans[i + 1] = j
break
if chintu == 0:
c = 0
break
if c == 0: print(-1)
else: print(*ans) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | from bisect import bisect_right
import sys
input = sys.stdin.readline
for ti in range(int(input().strip())):
n = int(input().strip())
b = [int(x) for x in input().strip().split()]
n = list(range(1, 2*n+1))
ans = []
for bi in b:
n.remove(bi)
for bi in b:
ai = bisect_right(n, bi)
if ai==len(n):
print('-1')
break
ans.append(bi)
ans.append(n[ai])
n.pop(ai)
else:
print(*ans)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 |
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Round623C {
public static void solve() {
int t = s.nextInt();
outer:while(t-- >0) {
int n = s.nextInt();
int[] arr = new int[n];
boolean[] used = new boolean[2 * n + 1];
for(int i = 0; i < n; i++) {
arr[i] = s.nextInt();
used[arr[i]] = true;
}
int[] ans = new int[2 * n + 1];
for(int i = 1; i <= n; i++) {
int current = arr[i-1] ;
int min = -1;
for(int j = current+1; j <= 2 * n; j++) {
if(!used[j]) {
min = j;
break;
}
}
used[current] = true;
if(min == -1) {
out.println(-1);
continue outer;
}else {
used[min] = true;
ans[2 * i - 1] = current;
ans[2 * i ] = min;
}
}
for(int i = 1; i <= 2 * n; i++) {
out.print(ans[i]+" ");
}
out.println();
}
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
s = new FastReader();
solve();
out.close();
}
public static FastReader s;
public static PrintWriter out;
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | # cook your dish here
n=int(input())
while(n):
n=n-1
a=int(input())
b=list(map(int,input().split()))
c=[True for x in range(2*a+1)]
for x in b:
c[x]=False
ans=[]
get=True
for i in range(a):
flag=False
k=b[i]
while(k<2*a+1):
if c[k]==True:
flag=True
c[k]=False
ans.append(b[i])
ans.append(k)
break
else:
k+=1
if flag==False:
get=False
break
if get:
for x in ans:
print(x,end=" ")
print("\n",end="")
else:
print("-1")
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e2 + 10;
bool mark[maxn];
int ans[maxn];
int main() {
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
bool flag = true;
memset(mark, false, sizeof(mark));
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> ans[2 * i];
if (mark[ans[2 * i]] == true) {
flag = false;
} else
mark[ans[2 * i]] = true;
}
if (flag == false) {
cout << -1 << endl;
continue;
}
for (int i = 1; i < 2 * n; i += 2) {
bool ok = false;
for (int k = ans[i - 1] + 1; k <= 2 * n; k++)
if (mark[k] == false) {
ans[i] = k;
mark[k] = true;
ok = true;
break;
}
if (ok == false) {
flag = false;
break;
}
}
if (flag == false)
cout << -1 << endl;
else {
for (int i = 0; i < 2 * n; i++) cout << ans[i] << ' ';
cout << endl;
}
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import sys
from collections import Counter
def solve(N, B):
counts = Counter(B)
if any(v > 1 for v in counts.values()):
return -1
# Want smallest permutation that is element-wise greater than B
C = []
for x in range(1, 2 * N + 1):
if x not in counts:
C.append(x)
# Sorted B and C must be pairable
if any(b > c for b, c in zip(sorted(B), C)):
return -1
def gen(index=0):
if index == N:
yield C
return
for c, i in sorted(zip(C[index:N], range(index, N))):
if B[index] < c:
C[index], C[i] = C[i], C[index]
yield from gen(index + 1)
C[index], C[i] = C[i], C[index]
for sol in gen():
A = []
for b, c in zip(B, C):
A.append(str(b))
A.append(str(c))
return " ".join(A)
return -1
if __name__ == "__main__":
input = sys.stdin.readline
T = int(input())
for t in range(T):
N, = map(int, input().split())
B = [int(x) for x in input().split()]
ans = solve(N, B)
print(ans)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int Search(vector<long long int> v1, long long int x1) {
long long int j;
for (j = 0; j < v1.size(); j++) {
if (x1 == v1[j]) {
return j;
} else {
continue;
}
}
return -1;
}
int Search2(long long int v[], long long int x, long long int n) {
long long int i;
for (i = 0; i < n; i++) {
if (x == v[i]) {
return i;
} else {
continue;
}
}
return -1;
}
int main() {
long long int e, r, g, pts, y, t, d, q, x, n, h, m, w, l, k,
cnt = 500, countA = 0, sum, sumt, countB = 0, countC = 0, a1, a2,
countD = 0;
long long int min = 1000;
vector<long long int> v3;
vector<long long int> v2;
set<long long int> s;
cin >> a2;
while (a2--) {
cin >> n;
for (h = 0; h < n; h++) {
cin >> sum;
v2.push_back(sum);
}
e = *max_element(v2.begin(), v2.end());
r = *min_element(v2.begin(), v2.end());
if (e >= 2 * n || r > 1) {
cout << -1;
} else {
v3.push_back(0);
for (k = 0; k < n; k++) {
t = v2[k];
v3.push_back(t);
for (r = 1; r <= (2 * n) - t; r++) {
if (Search(v3, t + r) == -1 && Search(v2, t + r) == -1) {
break;
} else
continue;
}
v3.push_back(t + r);
}
g = *max_element(v3.begin(), v3.end());
if (g == 2 * n) {
for (q = 1; q <= 2 * n; q++) {
cout << v3[q] << " ";
}
} else {
cout << -1;
}
}
cout << "\n";
v3.clear();
v2.clear();
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int b, c = 0, sum = 0, i = 0, s, j, n, d = 0, q = 0, e, f, t = 0, x,
h, l = 0, k = 0, x1, x2, y1, y2, m;
cin >> t;
vector<long long int> v;
vector<long long int> a;
vector<long long int> z;
map<long long int, long long int> mp;
for (j = 0; j < t; j++) {
cin >> n;
v.clear();
a.clear();
z.clear();
mp.clear();
for (i = 0; i < n; i++) {
cin >> x;
v.push_back(x);
z.push_back(x);
}
sort(v.begin(), v.end());
c = 0;
s = 1;
for (k = c + 1; k < 2 * n + 1; k++) {
for (i = 0; i < n; i++) {
l = 1;
if (v[i] == k) {
l = 0;
break;
}
}
if (l != 0) {
c = k;
a.push_back(c);
}
}
if (v[0] != 1) {
cout << "-1" << endl;
continue;
}
for (f = 0; f < n; f++) {
if (v[f] > a[f]) {
cout << "-1" << endl;
s = 0;
break;
}
}
if (s != 0) {
for (f = 0; f < n; f++) {
for (i = 0; i < a.size(); i++) {
if (a[i] > z[f]) {
cout << z[f] << " " << a[i] << " ";
a.erase(a.begin() + i);
break;
}
}
}
}
cout << endl;
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long inf = 1e18;
const long long dx[8] = {-1, 0, 1, 0, -1, 1, 1, -1},
dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const long long kdx[8] = {-2, -1, 1, 2, 2, 1, -1, -2},
kdy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
void solve() {
long long n;
cin >> n;
long long f = 0, f2 = 0;
vector<long long> v(n);
set<long long> s;
vector<long long> nots;
for (long long i = 0; i <= n - 1; ++i) {
cin >> v[i];
if (v[i] == 2 * n) {
f = 1;
}
if (v[i] == 1) {
f2 = 1;
}
s.insert(v[i]);
}
if (f == 1) {
cout << "-1\n";
return;
}
if (f2 == 0) {
cout << "-1\n";
return;
}
for (long long i = 1; i <= 2 * n; ++i)
if (s.count(i) == 0) nots.push_back(i);
map<long long, long long> make_pair;
for (auto x : v) {
auto idx = upper_bound(nots.begin(), nots.end(), x) - nots.begin();
if (idx >= (long long)(nots.size())) {
cout << "-1\n";
return;
}
make_pair[x] = nots[idx];
nots.erase(nots.begin() + idx);
}
for (auto x : v) {
cout << x << ' ' << make_pair[x] << ' ';
}
cout << '\n';
}
int32_t main() {
clock_t start = clock();
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
cin >> t;
for (long long i = 1; i <= t; ++i) {
solve();
}
double duration = (clock() - start) / (double)CLOCKS_PER_SEC;
cerr << duration << 's' << "\n";
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
bool compare(string &s1, string &s2) { return s1.size() < s2.size(); }
string lower(string second) {
transform(second.begin(), second.end(), second.begin(), ::tolower);
return second;
}
string upper(string second) {
transform(second.begin(), second.end(), second.begin(), ::toupper);
return second;
}
map<int, int> mp;
set<int> myset;
vector<int> v, v1, v2;
long long n, m, i, j, k, h, t, x = 1, y = 1, z = 1, d = 0;
int ans = 0, sum = 0, cnt = 0, p = 0, mx = 0, mn = 0;
int a, b, l, r;
string second, st, str;
int main() {
cin >> t;
while (t--) {
cin >> n;
for (i = 1; i <= 2 * n; i++) mp[i];
int a[n + 5];
for (i = 1; i <= n; i++) {
cin >> a[i];
mp[a[i]]++;
}
for (i = 1; i <= n; i++) {
x = a[i];
v.push_back(x);
for (auto it : mp) {
if (it.second == 0 and it.first > x) {
mp[it.first]++;
v.push_back(it.first);
break;
}
}
}
if (v.size() == 2 * n) {
for (auto it : v) cout << it << ' ';
} else {
cout << -1;
}
cout << endl;
v.clear();
mp.clear();
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.util.*;
public class RestoringPermutation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int b[] = new int[n];
int a[] = new int[n*2];
int r[] = new int[n*2];
boolean bool = false;
for(int i=0;i<n;i++){
a[2*i] = 2*i + 1;
a[2*i+1] = 2*i + 2;
b[i] = sc.nextInt();
}
for(int i=0;i<n;i++){
a[b[i] - 1] = 0;
}
for(int i=0;i<n;i++){
r[2*i] = b[i];
bool = false;
for(int j=0;j<2*n;j++){
if(a[j] > 0 && a[j] > b[i]){
r[2*i+1] = a[j];
a[j] = 0;
bool = true;
break;
}
}
if(!bool){
System.out.println(-1);
break;
}
}
if(bool){
for(int i=0;i<2*n;i++){
System.out.print(r[i] + " ");
}
System.out.println();
}
}
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | t = int(input())
for i in range(t):
n = int(input())
b = list(map(int,input().split()))
l = [i+1 for i in range(2*n)]
q = sorted(b)
ans = True
ansList = []
count = 0
if q[0]!=1:
print(-1)
continue
for i in range(1,2*n-1):
if i not in q and i+1 not in q:
if count == 0:
ans = False
break
else:
count -= 1
if i in q and i+1 in q:
count+=1
if ans == False:
print(-1)
else:
for i in b:
ansList.append(i)
i+=1
while i<2*n+1:
if i not in ansList and i not in q:
ansList.append(i)
break
i+=1
print(*ansList)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | for _ in range(int(input())):
l = int(input())
arr = [int(h) for h in input().strip().split(' ')]
newarr = []
flag=True
for i in range(2*l):
if i%2==0:
newarr.append(arr[i//2])
else:
num = newarr[-1]
while num<=2*l:
if (num in newarr) or (num in arr):
num=num+1
else:
newarr.append(num)
break
if newarr[-1]==arr[(i-1)//2]:
print(-1)
flag=False
break
if flag:
s=''
for i in range(len(newarr)):
s=s+str(newarr[i])+' '
print(s)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.io.*;
import java.util.*;
public class C implements Runnable {
boolean judge = false;
FastReader scn;
PrintWriter out;
String INPUT = "";
void solve() {
int t = scn.nextInt();
here: while(t-- > 0) {
int n = scn.nextInt();
int[] b = scn.nextIntArray(n);
int[] a = new int[2 * n];
TreeSet<Integer> set = new TreeSet<Integer>();
for(int i = 0; i < 2 * n; i++) {
set.add(i + 1);
}
for(int i = 0; i < n; i++) {
a[2 * i] = b[i];
set.remove(b[i]);
}
for(int i = 1; i < 2 * n; i += 2) {
if(set.higher(a[i - 1]) == null) {
out.println(-1);
continue here;
}
a[i] = set.higher(a[i - 1]);
set.remove(a[i]);
}
if(!set.isEmpty()) {
out.println(-1);
continue here;
}
for(int v : a) {
out.print(v + " ");
}
out.println();
}
}
public void run() {
long time = System.currentTimeMillis();
boolean oj = System.getProperty("ONLINE_JUDGE") != null || judge;
out = new PrintWriter(System.out);
scn = new FastReader(oj);
solve();
out.flush();
if (!oj) {
System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
}
}
public static void main(String[] args) {
new Thread(null, new C(), "Main", 1 << 28).start();
}
class FastReader {
InputStream is;
public FastReader(boolean onlineJudge) {
is = onlineJudge ? System.in : new ByteArrayInputStream(INPUT.getBytes());
}
byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return (char) skip();
}
String next() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
String nextLine() {
int b = skip();
StringBuilder sb = new StringBuilder();
while ((!isSpaceChar(b) || b == ' ')) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
char[] next(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
int nextInt() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
long nextLong() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
char[][] nextMatrix(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = next(m);
return map;
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
int[][] next2DInt(int n, int m) {
int[][] arr = new int[n][];
for (int i = 0; i < n; i++) {
arr[i] = nextIntArray(m);
}
return arr;
}
long[][] next2DLong(int n, int m) {
long[][] arr = new long[n][];
for (int i = 0; i < n; i++) {
arr[i] = nextLongArray(m);
}
return arr;
}
int[] shuffle(int[] arr) {
Random r = new Random();
for (int i = 1, j; i < arr.length; i++) {
j = r.nextInt(i);
int c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
return arr;
}
long[] shuffle(long[] arr) {
Random r = new Random();
for (int i = 1, j; i < arr.length; i++) {
j = r.nextInt(i);
long c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
return arr;
}
int[] uniq(int[] arr) {
arr = scn.shuffle(arr);
Arrays.sort(arr);
int[] rv = new int[arr.length];
int pos = 0;
rv[pos++] = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[i - 1]) {
rv[pos++] = arr[i];
}
}
return Arrays.copyOf(rv, pos);
}
long[] uniq(long[] arr) {
arr = scn.shuffle(arr);
Arrays.sort(arr);
long[] rv = new long[arr.length];
int pos = 0;
rv[pos++] = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[i - 1]) {
rv[pos++] = arr[i];
}
}
return Arrays.copyOf(rv, pos);
}
int[] reverse(int[] arr) {
int l = 0, r = arr.length - 1;
while (l < r) {
arr[l] = arr[l] ^ arr[r];
arr[r] = arr[l] ^ arr[r];
arr[l] = arr[l] ^ arr[r];
l++;
r--;
}
return arr;
}
long[] reverse(long[] arr) {
int l = 0, r = arr.length - 1;
while (l < r) {
arr[l] = arr[l] ^ arr[r];
arr[r] = arr[l] ^ arr[r];
arr[l] = arr[l] ^ arr[r];
l++;
r--;
}
return arr;
}
int[] compress(int[] arr) {
int n = arr.length;
int[] rv = Arrays.copyOf(arr, n);
rv = uniq(rv);
for (int i = 0; i < n; i++) {
arr[i] = Arrays.binarySearch(rv, arr[i]);
}
return arr;
}
long[] compress(long[] arr) {
int n = arr.length;
long[] rv = Arrays.copyOf(arr, n);
rv = uniq(rv);
for (int i = 0; i < n; i++) {
arr[i] = Arrays.binarySearch(rv, arr[i]);
}
return arr;
}
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | def solve(m, b):
d=dict()
y=dict()
for i in range(m):
d[b[i]]=0
for k in d.keys():
x=k+1
while x in d.keys() or x in y.keys():
x+=1
if (x>2*m):
return -1
d[k]=x
y[x]=k
for i in range(m):
print(b[i], d[b[i]], end=" ")
print()
return 1
for _ in range(int(input())):
m=int(input())
b=list(map(int, input().split()))
if (solve(m, b)==-1):
print(-1)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | T=int(input())
for i in range(T):
N=int(input())
l=[0]*(N*2+1)
f=0
c=0
b=[int(x) for x in input().split()]
for j in range(0,len(b)):
c=c+1
l[c]=b[j]
c=c+1
if b.count(b[j]+1)==1 or l.count(b[j]+1)==1:
m=b[j]+1
while(True):
m=m+1
if b.count(m)==0 and l.count(m)==0:
break
if m==(2*N):
f=1
break
if f==1:
break
else:
l[c]=m
else:
l[c]=b[j]+1
for j in range(1,2*N+1):
if l.count(j)==0:
f=1
break
if f==1:
print(-1)
else:
l.pop(0)
print(*l)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | I=input
for _ in[0]*int(I()):
I();b=*map(int,I().split()),;s={*range(2*len(b)+1)}-{*b};a=[];i=1
try:
for x in b:y=min(s-{*range(x)});s-={y};a+=x,y;i+=2
except:a=-1,
print(*a) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.util.*;
import java.io.*;
public class Solution{
static long mod=998244353 ;
static boolean f=true;
static class pair implements Comparable<pair>{
int a,b,c;
pair(int x,int y,int z){
a=x;b=y;c=z;
}
public int compareTo(pair t){
if(c<t.c) return 1;
else if(c==t.c)
{ if(a<t.a) return -1;
else if(a>t.a) return 1;
else return 0;
}
else return -1;
}
}
// public static ArrayList<pair> bfs(String[] a,int r,int c){
// ArrayList<pair> ans=new ArrayList<>();
// Queue<pair> q=new LinkedList<>();
// int[][] dxy={{-1,0,1,0},{0,-1,0,1}};
// q.add(new pair(r,c));
// HashSet<String> h=new HashSet<>();
// while(!q.isEmpty()){
// pair f=q.poll();
// ans.add(f);h.add(f.a+" "+f.b);
// for(int i=0;i<4;i++){
// int dx=f.a+dxy[0][i];
// int dy=f.b+dxy[1][i];
// if(dx<0||dy<0||dx>=a.length||dy>=a.length||h.contains(dx+" "+dy)||
// a[dx].charAt(dy)=='1')
// continue;
// q.add(new pair(dx,dy));
// h.add(dx+" "+dy);
// }
// }
// return ans;
// }
/*
1 1 1 1 1
3 3 3
2 3 3 2 1 3
1
2 2 2 2
4 4 4 1 4
*/
public static int pow(int a, int n) {
long ans = 1;
long base = a;
while (n != 0) {
if ((n & 1) == 1) {
ans *= base;
ans %= 1000000007;
}
base = (base * base) % 1000000007;
n >>= 1;
}
return (int) ans % 1000000007;
}
public static int find(int x){
int i=1;int f=0;
while(x>0){
if((x&1)==0)f=i;
x>>=1;i++;
}
return f;
}
public static boolean good(int x){
if((((x+1)&x))==0)
return true;
return false;
}
public static long f(long n){
long val=2;
long preval=0;int i=1;
while(n>=val){
i++;
preval=val;
val=3*i+val-1;
}
return preval;
}
public static boolean bfs(ArrayList<Integer>[] a,int n,int[] b){
Queue<Integer> p=new LinkedList<>();
boolean v[]=new boolean[n];
v[0]=true;int k=0;
if(b[0]!=0)return false;
p.add(0);int c=1;
HashSet<Integer> h=new HashSet<Integer>();
h.add(0);
while(!p.isEmpty()){
int f=p.poll();k++;
if(h.contains(f))h.remove(f);
else return false;
v[f]=true;
c--;
for(int i:a[f]){
if(!v[i]){
p.add(i);v[i]=true;}
}
if(c==0){
c=p.size();
if(h.size()!=0)return false;
for(int j=k;j<k+p.size();j++)
h.add(b[j]);
}
}
return true;
}
public static int mi(int[] a,int k,int[] dp,int n){
int max=0;
for(int i=1;i*i<=k;i++){
// System.out.println("fact "+i);
if(k%i==0&&(a[i-1]<a[k-1])){
// System.out.println(i+" "+dp[i-1]+" "+a[i-1]+" ");
max=Math.max(dp[i-1],max);}
if(k%i==0&&(a[k/i-1]<a[k-1])){
// System.out.println(i+" "+dp[i-1]+" "+a[i-1]+" ");
max=Math.max(dp[k/i-1],max);}
}
return max;
}
public static long dfs(ArrayList<Integer> a[],int n,boolean[] v,boolean s[]
,long c[],int x){
v[n]=true;
if(n==x)
s[n]=true;
c[n]=1;
for(int i=0;i<a[n].size();i++){
if(!v[a[n].get(i)]){
c[n]+= dfs(a,a[n].get(i),v,s,c,x);
s[n]|=s[a[n].get(i)];
}
}
return c[n];
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
StringBuilder st=new StringBuilder();
while(t-- > 0) {
int n=s.nextInt();
HashSet<Integer> h=new HashSet<>();
int[]a=new int[n];
for(int i=0;i<n;i++){
a[i]=s.nextInt();
h.add(a[i]);
}
StringBuilder ans=new StringBuilder();
boolean f=true;
for(int i=0;i<n;i++){
int k=1;
while(a[i]+k<=2*n&&h.contains(a[i]+k))k++;
if(a[i]+k>2*n){
f=false;break;
}
h.add(a[i]+k);
ans.append(a[i]+" "+(a[i]+k)+" ");
}
if(!f)
st.append("-1\n");
else
st.append(ans+"\n");
}
System.out.println(st);
/*
2
1
3
1
6
6 23 21
1 1000 0
2 1 2
0
40
41664916690999888
1
1 2
2 1 3
3 1 2 4
2 4 1 3 5
3 4 1 5 2 6
6 45 46
2 2 1
3 4 2
4 4 3
*/
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | // package com.company;
import java.util.*;
import java.lang.*;
import java.io.*;
//****Use Integer Wrapper Class for Arrays.sort()****
public class BV3 {
public static void main(String[] Args){
FastReader scan=new FastReader();
int t=scan.nextInt();
while(t-->0){
int n=scan.nextInt();
int[] arr=new int[n];
Set<Integer> nu=new HashSet<>();
for(int i=1;i<=2*n;i++){
nu.add(i);
}
for (int i = 0; i <n ; i++) {
arr[i]=scan.nextInt();
nu.remove(arr[i]);
}
int[] tkn=new int[n];
int[] ans=new int[2*n];
boolean flag=true;
for(int i=0;i<n;i++){
ans[2*i]=arr[i];
int nxt=500;
for(Integer j:nu){
if(j>ans[2*i]&&j<nxt){
nxt=j;
}
}
if(nxt==500){
flag=false;
break;
}
else{
nu.remove(nxt);
ans[2*i+1]=nxt;
}
}
if(!flag){
System.out.println(-1);
}
else{
for(int i=0;i<2*n;i++){
System.out.print(ans[i]+" ");
}
System.out.println();
}
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | cases=int(input())
final=[]
for _ in range(cases):
n=int(input())
mm=[]
flag=0
c=list(map(int,input().split()))
v=list(set(list(range(1,2*len(c)+1)))-set(c))
mm=[]
for i in range(len(v)):
r=c[i]
try:
q=min([v[i] for i in range(len(v)) if v[i]>r])
mm.append(q)
v.remove(q)
except:
continue
gg=[]
for i in range(len(mm)):
gg.append(c[i])
gg.append(mm[i])
if len(gg)==2*len(c):
print(*gg)
else:
print(-1)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin >> t;
while (t--) {
long long vis[205] = {0};
long long n, c = 0;
cin >> n;
long long a[n];
vector<long long> b;
for (long long i = 0; i < n; i++) {
cin >> a[i];
vis[a[i]] = 1;
}
for (long long i = 0; i < n; i++) {
b.push_back(a[i]);
for (long long j = a[i] + 1; j <= 2 * n; j++) {
if (!vis[j]) {
b.push_back(j);
vis[j] = 1;
break;
}
}
}
if (b.size() == 2 * n) {
for (long long i = 0; i < 2 * n; i++) cout << b[i] << " ";
} else
cout << "-1";
cout << "\n";
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.util.Scanner;
public class C {
public static void main(String[] args) {
new C();
}
public C() {
Scanner sc = new Scanner(System.in);
int amountT = sc.nextInt();
for (int task = 0; task < amountT; task++) {
int n = sc.nextInt();
int[] b = new int[n];
boolean[] done = new boolean[2 * n + 1];
boolean poss = true;
for (int i = 0; i < n; i++) {
int val = sc.nextInt();
b[i] = val;
if (done[val]) {
poss = false;
}
done[val] = true;
}
int[] a = new int[n];
for (int index = 0; index < n; index++) {
boolean found = false;
for (int i = b[index] + 1; i <= 2*n; i++) {
if (!done[i]) {
if (i > b[index]) {
found = true;
a[index] = i;
done[i] = true;
break;
}
}
}
if (!found) {
poss = false;
break;
}
}
if (poss) {
String s = "";
for (int i = 0; i < n; i++) {
if (i != 0) {
s += " ";
}
s += b[i] + " " + a[i];
}
System.out.println(s);
} else {
System.out.println(-1);
}
}
sc.close();
}
}
| JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | from bisect import bisect_left
def main():
for _ in range(int(input())):
n, res = int(input()), []
l = list(map(int, input().split()))
r = sorted(set(range(1, n * 2 + 1)) - set(l))
try:
for a in l:
res += a, r.pop(bisect_left(r, a + 1))
print(*res)
except IndexError:
print(-1)
if __name__ == '__main__':
main()
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int b[]=new int[n];
Set<Integer> set=new HashSet<>();
for(int i=0;i<n;i++)set.add(b[i]=sc.nextInt());
int a[]=new int[2*n];
int last=1;
for(int i=0;i<n;i++){
a[2*i]=b[i];
int next=0;
for(int j=b[i]+1;j<=2*n;j++){
if(!set.contains(j)){
set.add(j);
next=j;
break;
}
}
a[2*i+1]=next;
}
if(set.size()<2*n)
System.out.println(-1);
else{
for(int i:a)System.out.print(i+" ");
System.out.println();
}
}
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import javax.swing.JInternalFrame;
public class Main
{
public static void main(String[] args) throws IOException
{
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
in.nextToken();
int num = (int)in.nval;
for(int i = 0; i < num; i++)
{
in.nextToken();
int num1 = (int)in.nval;
int[] getLine = new int[num1];
int[] numLine = new int[210];
for(int j = 0; j < num1; j++) { in.nextToken(); int num2 = (int)in.nval; numLine[num2]++; getLine[j] = num2; }
int haveYES = 1;
int numHave = 0;
for(int j = 1; j < num1 * 2; j++)
{
if(numLine[j] > 1) { haveYES = 0; break; }
if(numLine[j] == 1) numHave++;
if(j % 2 == 1)
if(numHave < j / 2 + 1) { haveYES = 0; break; }
}
if(haveYES == 0) { out.println("-1"); continue; }
for(int j = 0; j < num1; j++)
{
out.print(getLine[j] + " ");
for(int k = getLine[j] + 1; ; k++)
{
if(numLine[k] == 0)
{
out.print(k + " ");
numLine[k] = 1;
break;
}
}
}
out.println();
}
out.flush();
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.io.*;
import java.util.*;
public class Q1 {
// Q3
public static void main(String[] args) {
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
while (t-- > 0) {
int N = in.nextInt();
HashSet<Integer> set = new HashSet<>();
int ans[] = new int[2 * N + 1];
for (int j = 0; j < 2*N; j += 2) {
ans[j + 1] = in.nextInt();
set.add(ans[j+1]);
}
ArrayList<Integer> count = new ArrayList<>();
for (int i = 0; i < 2 * N; i++) {
if (!set.contains(i + 1))
count.add(i + 1);
}
int flag = 1;
for (int i = 0, j = 0; i < N; i++, j += 2) {
int val = ans[j + 1];
int temp = 1000,ind=0;
for (int k = 0; k < count.size(); k++) {
if (count.get(k) > val && count.get(k) < temp) {
temp = count.get(k);
ind=k;
}
}
if (temp == 1000) {
flag = -1;
break;
}
ans[j + 2] = temp;
count.remove(ind);
}
if (flag == -1)
out.println(-1);
else {
for (int i = 1; i <= 2 * N; i++)
out.print(ans[i] + " ");
out.println();
}
}
out.close();
}
//Q2
// public static void main(String[] args) {
// InputReader in = new InputReader();
// PrintWriter out = new PrintWriter(System.out);
// int t = in.nextInt();
// while (t-- > 0) {
// int a =in.nextInt(),b=in.nextInt(),c=in.nextInt()+1,d=in.nextInt()+1;
// int max=Math.max(Math.max((a)*(b-d),(a)*(d-1)),Math.max((b)*(a-c),(b)*(c-1)));
// out.println(max);
//
// }
// out.close();
// }
// ternary Search Question
// public static void main(String[] args) {
//
// InputReader in = new InputReader();
// PrintWriter out = new PrintWriter(System.out);
// int t=in.nextInt();
// while(t-->0) {
//
// int N = in.nextInt(),max=-1,diff=0;
// int arr[] = new int[N];
// for(int i=0;i<N;i++){
// arr[i]=in.nextInt();
// max=Math.max(max,arr[i]);
// }
// if(max==-1){
// out.println(0+" "+1);
// continue;
// }
//
//
//
// for(int i =1;i<N;i++){
// int a =arr[i]==-1?max:arr[i],b =arr[i-1]==-1?max:arr[i-1],d=Math.abs(a-b);
// diff=Math.max(diff,d);
// }
// System.out.println(max+" diff:-"+diff);
//
// int l=0,r=max;
// while(l<=r){
// int mid=(l+r)/2;
// int di=0;
// for(int i =1;i<N;i++){
// int a =arr[i]==-1?mid:arr[i],b =arr[i-1]==-1?mid:arr[i-1],d=Math.abs(a-b);
// di=Math.max(di,d);
// }
// System.out.println(mid+" diff:-"+di);
// if(di<diff){
// r=mid-1;
// max=mid; diff=di;
// }else{
// l=mid+1;
// }
// }
//
// out.println(diff+" "+max);
//
// }
// out.close();
//
//
// }
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public int[] shuffle(int[] arr) {
Random r = new Random();
for (int i = 1, j; i < arr.length; i++) {
j = r.nextInt(i);
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
return arr;
}
public InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public char nextChar() {
return next().charAt(0);
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
// public static void main(String[] args) {
//
// InputReader in = new InputReader();
// PrintWriter out = new PrintWriter(System.out);
// int t =in.nextInt();
//
// while(t-->0){
// int N=in.nextInt();
// int arr[]=new int[N];
// for(int i=0 ;i<N;i++)
// arr[i]=in.nextInt();
//
// ArrayList<Integer> set1=new ArrayList<>(),set2=new ArrayList<>(),set3=new ArrayList<>();
// double xval=in.nextInt(),x=in.nextInt(),yval=in.nextInt(),y=in.nextInt();
// long k=in.nextLong();
// long ans =0;
//
// if(xval<yval){
// double temp=xval;
// xval=yval;
// yval=temp;
//
// temp=x;
// x=y;
// y=temp;
// }
// xval=xval/100; yval=yval/100;
//
// for(int i =0;i<N;i++){
// if((i+1)%x==0 && (i+1)%y==0)
// set1.add(i);
// else if ((i+1)%x==0 )
// set2.add(i);
// else if((i+1)%y==0)
// set3.add(i);
// }
// arr=in.shuffle(arr);
//
// int a =0,b=0,c=0,add=0;
//
// for(int i=N-1;i>=0;i--){
//
// int temp=0;
//
// if(a==set1.size() && b==set2.size() && c==set3.size())
// continue;
// else if (a==set1.size() && b==set2.size() ){
// ans += (yval) * arr[i];
// c++;
// temp=11;
// }else if(a==set1.size()){
// if (set2.get(b) <set3.get(c)) {
// ans += (xval) * arr[i];
// b++;
// } else {
// ans += (yval) * arr[i];
// c++;
// }
// }else{
// if (set1.get(a) < set2.get(b) && set1.get(a) < set3.get(c)) {
// ans += (xval + yval) * arr[i];
// a++;
// } else if (set2.get(b)<set3.get(c)) {
// ans += (xval) * arr[i];
// b++;
// } else {
// ans += (yval) * arr[i];
// c--;
// }
// }
//
//
//
// if(ans>=k){
// break;
// }
//
// }
//
// if(ans<k)
// out.println(-1);
// else
// out.println(add);
//
//
//
// }
//
// out.close();
// }
//
//import java.io.*;
//import java.util.*;
//
//
//public class Q3 {
//
//
// // tunnel wala Question tourist
//
//// public static void main(String[] args) {
////
//// InputReader in = new InputReader();
//// PrintWriter out = new PrintWriter(System.out);
////
//// int N=in.nextInt();
//// int arr1[]=new int [N],arr2[]=new int[N];
//// int ans =0;
////
//// for(int i =0;i<N;i++){
//// arr1[i]=in.nextInt();
//// }
////
//// HashMap<Integer,Integer>map=new HashMap<>();
////
//// for(int j=0;j<N;j++){
//// int num=in.nextInt();
//// arr2[j]=num;
//// map.put(num,N-j);
//// }
//// int a[]=new int [N+1];
////
//// boolean flag=false;
//// for(int i =0;i<N;i++) {
//// int num = arr1[i];
//// int val=map.get(num);
//// if(val>(N-i))
//// ans++;
//// else if(val==N-i && !flag){
//// ans++;
////
//// }
//// a[arr1[i]]++;
//// a[arr2[N-i-1]]++;
////
//// if(a[arr1[i]]!=2 || a[arr2[N-i-1]]!=2)
//// flag=false;
//// else
//// flag=true;
////
//// }
//// out.println(ans);
////
//// out.close();
////
////
//// }
//
// static class InputReader {
// public BufferedReader reader;
// public StringTokenizer tokenizer;
//
//
// public int[] shuffle(int[] arr) {
// Random r = new Random();
// for (int i = 1, j; i < arr.length; i++) {
// j = r.nextInt(i);
// arr[i] = arr[i] ^ arr[j];
// arr[j] = arr[i] ^ arr[j];
// arr[i] = arr[i] ^ arr[j];
// }
// return arr;
// }
//
// public InputReader() {
// reader = new BufferedReader(new InputStreamReader(System.in), 32768);
// tokenizer = null;
// }
//
// public InputReader(InputStream stream) {
// reader = new BufferedReader(new InputStreamReader(System.in), 32768);
// tokenizer = null;
// }
//
// public String next() {
// while (tokenizer == null || !tokenizer.hasMoreTokens()) {
// try {
// tokenizer = new StringTokenizer(reader.readLine());
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// }
// return tokenizer.nextToken();
// }
//
// public char nextChar() {
// return next().charAt(0);
// }
//
// public int nextInt() {
// return Integer.parseInt(next());
// }
//
// public long nextLong() {
// return Long.parseLong(next());
// }
//
// public double nextDouble() {
// return Double.parseDouble(next());
// }
//
//
// }
//
//} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | T = int(input())
while T!=0:
T-=1
n = int(input())
arr = [int(x) for x in input().split()]
st = set()
for i in range(1,2*n+1):
st.add(i)
for i in arr:
st.discard(i)
ans = []
for i in arr:
ans.append(i)
for j in sorted(st):
if j>i:
ans.append(j)
st.discard(j)
break
if len(ans) == 2*n:
for i in ans:
print(i, end=" ")
print()
else:
print(-1)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import atexit
import io
import sys
import math
from collections import defaultdict,Counter
# _INPUT_LINES = sys.stdin.read().splitlines()
# input = iter(_INPUT_LINES).__next__
# _OUTPUT_BUFFER = io.StringIO()
# sys.stdout = _OUTPUT_BUFFER
# @atexit.register
# def write():
# sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
# sys.stdout=open("CP2/output.txt",'w')
# sys.stdin=open("CP2/input.txt",'r')
# m=pow(10,9)+7
t=int(input())
for i in range(t):
n=int(input())
b=list(map(int,input().split()))
visit=[0]*(2*n+1)
for j in b:
visit[j]=1
l=[2]*n
flag=0
for j in range(n):
if b[j]+1>2*n:
flag=1
break
if visit[b[j]+1]==0:
l[j]=b[j]+1
visit[b[j]+1]=1
else:
p=b[j]+2
while p<=2*n and visit[p]==1:
p+=1
if p>2*n:
flag=1
break
l[j]=p
visit[p]=1
if visit.count(0)!=1 or flag==1:
print(-1)
continue
for j in range(n):
print(b[j],l[j],end=' ')
print() | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.util.Scanner;
public class C_623 {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for (int test = 0; test < t; test++){
int n = input.nextInt();
int[] b = new int[n + 1];
boolean[] taken = new boolean[n * 2 + 1];
for (int i = 1; i <= n; i++){
b[i] = input.nextInt();
taken[b[i]] = true;
}
String out = "";
boolean no = false;
for (int i = 1; i <= n; i++){
out += b[i] + " ";
int j = b[i] + 1;
while (j <= n * 2 && taken[j]){
j++;
}
if (j > n * 2){
no = true;
break;
}
taken[j] = true;
out += j + " ";
}
//System.out.println(out);
if(no){
System.out.println(-1);
}else{
System.out.println(out.substring(0, out.length() - 1));
}
}
}
}
| JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | # Legends Always Come Up with Solution
# Author: Manvir Singh
import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict,deque,Counter
from bisect import *
from math import sqrt,pi,ceil
import math
from itertools import permutations
from copy import deepcopy
def main():
for t in range(int(input())):
n=int(input())
b=list(map(int,input().split()))
f=0
c=set(b)
d=[]
for i in range(1,2*n+1):
if i not in c:
d.append(i)
a=[]
for i in range(n):
a.append(b[i])
x=bisect_left(d,b[i])
if x==len(d):
f=1
break
a.append(d[x])
d.remove(d[x])
if f:
print(-1)
else:
print(*a)
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
if __name__ == "__main__":
main() | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long double PI = acos(-1);
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
const int mod1 = 998244353;
const long long INF = 2e18;
template <class T>
inline void amin(T& x, const T& y) {
if (y < x) x = y;
}
template <class T>
inline void amax(T& x, const T& y) {
if (x < y) x = y;
}
int read() {
long long s = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
s = s * 10 + c - '0';
c = getchar();
}
return s * f;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt;
tt = read();
while (tt--) {
int n;
n = read();
bool visited[210];
memset(visited, 0, sizeof(visited));
int a[105];
vector<int> v;
vector<int> t;
bool flag = false;
for (int i = 1; i <= n; i++) {
a[i] = read();
t.push_back(a[i]);
visited[a[i]] = true;
}
sort(t.begin(), t.end());
for (int i = 1; i <= 2 * n; i++) {
if (!visited[i]) {
v.push_back(i);
visited[i] = true;
}
}
sort(v.begin(), v.end());
for (int i = 0; i < n; i++) {
if (t[i] > v[i]) {
flag = true;
}
}
if (flag) {
cout << "-1\n";
continue;
} else {
for (int i = 1; i <= n; i++) {
cout << a[i] << " ";
for (int j = 0; j < n; j++) {
if (v[j] > a[i] && visited[v[j]]) {
cout << v[j] << " ";
visited[v[j]] = false;
break;
}
}
}
}
cout << "\n";
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int t, n, b[105], a[205], vis[205], ret;
int main() {
int i;
scanf("%d", &t);
while (t--) {
memset(vis, 0, sizeof(vis));
ret = 0;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &b[i]);
vis[b[i]] = 1;
a[i * 2 - 1] = b[i];
}
int j;
for (i = 1; i <= n; i++) {
ret = 0;
for (j = a[2 * i - 1] + 1; j <= 2 * n; j++) {
if (!vis[j]) {
vis[j] = 1;
a[i * 2] = j;
ret = 1;
break;
}
}
if (!ret) break;
}
if (ret) {
for (i = 1; i < 2 * n; i++) printf("%d ", a[i]);
printf("%d\n", a[i]);
} else
printf("-1\n");
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
used = set(a)
ans = []
for i in range(n):
ans.append(a[i])
for j in range(a[i] + 1, n << 1 | 1):
if j not in used:
ans.append(j)
used.add(j)
break
else:
print(-1)
break
else:
print(*ans)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, k;
bool vis[510];
struct node {
int x, y;
} a[110];
int main() {
cin >> k;
while (k--) {
memset(vis, 0, sizeof(vis));
cin >> n;
int ma = 2 * n;
for (int i = 1; i <= n; i++) {
cin >> a[i].x;
vis[a[i].x] = true;
}
int now, t = 1;
for (int i = 1; i <= n; i++) {
now = a[i].x;
while (vis[now] == true || now <= a[i].x) {
++now;
}
a[i].y = now;
if (now > ma) t = 0;
vis[now] = true;
}
if (t == 0) {
cout << -1 << endl;
continue;
}
for (int i = 1; i <= n; i++) cout << a[i].x << " " << a[i].y << " ";
cout << endl;
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> b(n);
vector<int> ans;
map<int, bool> m;
for (int i = 0; i < n; i++) {
cin >> b[i];
m[b[i]] = 1;
}
int i;
for (i = 0; i < n; i++) {
ans.push_back(b[i]);
int temp = b[i] + 1;
while (m[temp]) {
temp++;
if (temp > 2 * n) break;
}
if (temp > 2 * n) break;
m[temp] = 1;
ans.push_back(temp);
}
if (i != n)
cout << -1 << endl;
else {
for (auto j : ans) cout << j << " ";
cout << endl;
}
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int cinn() {
int x;
scanf("%d", &x);
return x;
}
long long cinl() {
long long x;
scanf("%lld", &x);
return x;
}
void coutt(int x) { printf("%d ", x); }
vector<string> split(const string &s, char c) {
vector<string> v;
stringstream ss(s);
string x;
while (getline(ss, x, c)) v.emplace_back(x);
return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
cout << it->substr((*it)[0] == ' ', it->length()) << " = " << a << " ";
err(++it, args...);
}
const int mod = 1e9 + 7;
const int inf = (int)2e9 + 5;
const long long int Inf = (long long int)1e18 + 5;
const int N = 1e7 + 5;
const int nn = 3e5 + 5;
int siv[2000];
int solve() {
int n;
cin >> n;
int a[106];
for (int i = 0; i < n; i++) {
cin >> a[i];
siv[a[i]] = 1;
}
bool f = true;
std::vector<int> v;
for (int i = 0; i < n; i++) {
for (int j = 1; j <= 2 * n; j++) {
if (a[i] < j && siv[j] == 0) {
siv[j] = 1;
v.push_back(a[i]);
v.push_back(j);
break;
}
}
}
for (int i = 1; i <= 2 * n; i++) {
if (siv[i] == 0) {
f = false;
}
}
if (f == false) {
cout << -1 << endl;
} else {
for (auto i : v) {
cout << i << " ";
}
printf("\n");
;
}
for (int i = 0; i <= 2 * n; i++) {
siv[i] = 0;
}
v.clear();
f = true;
return 0;
}
int main() {
int test = 1, tc = 0;
scanf("%d", &test);
while (test--) solve();
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 |
"""
NTC here
"""
import sys
inp= sys.stdin.readline
input = lambda : inp().strip()
# flush= sys.stdout.flush
# import threading
# sys.setrecursionlimit(10**6)
# threading.stack_size(2**26)
def iin(): return int(input())
def lin(): return list(map(int, input().split()))
def main():
T = iin()
while T:
T-=1
n = iin()
a = lin()
l = 2*n+1
done = [0]*(l)
ans = []
for i in a:
done[i]=1
for i in a:
ans.append(i)
for j in range(i+1, l):
if done[j]==0:
done[j]=1
ans.append(j)
break
else:
print(-1)
break
else:
print(*ans)
main()
#threading.Thread(target=main).start()
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | N = int(input())
for _ in range(N):
n = int(input())
b = list(map(int, input().split()))
a = [-1] * 2*n
pos = list(filter(lambda x: x not in b, range(1, 2*n + 1)))
f = True
for i, el in enumerate(b):
a[2*i] = el
st, end = 0, len(pos)
new_idx = (st + end) // 2
idx = None
min_pos = 2*n + 1
while new_idx != idx:
idx = new_idx
cur = pos[idx]
if cur < el:
st = idx
else:
min_pos = min(min_pos, cur)
end = idx
new_idx = (st + end) // 2
try:
pos.remove(min_pos)
a[2*i + 1] = min_pos
except Exception:
f = False
break
if f:
print(*a)
else:
print(-1)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | def main():
for _ in range(int(input())):
n = int(input())
a = list(map(int,input().split()))
t = 0
ans = []
used = [0] * (2 * n + 1)
for i in range(n):
used[a[i]] += 1
if used[a[i]] == 2:
t = 1
ans = -1
break
if t:
print(-1)
continue
var = []
for i in range(1, 2 * n + 1):
if not used[i]:
var.append(i)
ans = []
used = [0] * (2 * n + 1)
for i in range(n):
cur = a[i]
add = -1
for j in range(n):
if var[j] > cur and not used[var[j]]:
add = var[j]
used[var[j]] = 1
break
if add == -1:
t = 1
break
ans.append(cur)
ans.append(var[j])
if t:
print(-1)
continue
print(*ans)
main() | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | def main(n, b):
#print("DEBUT")
tab = [0]*(2*n)
dispo = [True]*(2*n + 1)
for i in range(n):
tab[2*i] = b[i]
dispo[b[i]] = False
for i in range(n):
j = b[i]
test = True
while j <= (2*n) and test:
if not dispo[j]:
j += 1
else:
test = False
dispo[j] = False
tab[2*i + 1] = j
test = True
for i in range(1, 2*n+1):
if dispo[i]:
test = False
#print("SOLUCE")
if not test:
print(-1)
else:
mot = ""
for x in tab:
mot += str(x) + " "
print(mot)
#print("END")
t = int(input())
for _ in range(t):
n = int(input())
b = list(map(int, input().split()))
main(n, b) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | t=int(input())
for _ in range(t):
n=int(input())
b=list(map(int,input().split()))
a=[]
dic={}
for i in b:
dic[i]=1
for i in b:
a.append(i)
x=i+1
while(True):
if x in dic:
x+=1
else:
break
a.append(x)
dic[x]=1
f=True
for i in range(1,2*n+1):
if i in dic:
continue
else:
f=False
break
if(f):
print(*a)
else:
print(-1) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | for _ in range(int(input())):
n = int(input())
B = list(map(int, input().split()))
A = [0]*(2*n)
A[::2] = B
X = set(range(1, 2*n+1)) - set(B)
for i in range(n):
x = B[i] + 1
while x <= 2*n:
if x in X:
A[2*i+1] = x
X.remove(x)
break
x += 1
else:
print(-1)
break
else:
print(*A) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 |
def func():
n_case = int(input())
for i in range(n_case):
n = int(input())
b = list(map(int,input().split()))
res = {*(range(2*n+1))}-{*b}
a =[]
try:
for i in b :
minn = min(res - {*range(i)})
res-={minn}
a +=i,minn
except:
a = [-1]
print(*a)
if __name__ == "__main__":
func() | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | n = int(input())
for i in range(n):
length = int(input())
b = [int(i) for i in input().split()]
c = list()
final = list()
pool = list()
var = False
for j in range(1, 2*length + 1):
if j not in b:
pool.append(j)
for j in range(len(b)):
var = False
for k in range(len(pool)):
if pool[k] > b[j]:
var = pool[k]
c.append(var)
pool.remove(var)
var = True
break
if not var:
break
if not var:
print(-1)
continue
else:
for j in range(len(b)):
final.append(b[j])
final.append(c[j])
print(' '.join(map(str, final)))
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | testCases = int(input())
for i1 in range(testCases):
length = int(input())
taken = [False]*length*2
b = list(map(int, input().split()))
a = [0]*2*length
for i2 in range(length):
taken[b[i2] - 1] = True
a[i2*2] = b[i2]
if taken.index(True) > 0:
print(-1)
else:
for i2 in range(length):
for i3 in range(b[i2] , 2*length, 1):
if not taken[i3]:
taken[i3] = True
a[(i2*2) + 1] = (i3 + 1)
break
if 0 in a:
print(-1)
else:
for i2 in a:
print(i2,end=' ')
print("") | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | def solution(n: int, b: list):
ret = [0] * (2 * n)
for i in range(n):
ret[2*i] = b[i]
for j in range(b[i] + 1, 2 * n + 1):
if not (j in b):
ret[2 * i + 1] = j
b.append(j)
break
if (0 in ret):
print(-1)
return
else:
print(' '.join(map(str, ret)))
t = int(input())
for i in range(t):
n = int(input())
b = list(map(int, input().split()))
solution(n, b)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
using namespace std;
const long long MOD = 1e+9 + 7;
const long long INF = LLONG_MAX;
const int INFi = INT_MAX;
const long long N = 3e+5 + 8;
long long a[N], b[N];
long long check[N];
long long tc = 1;
void solve(long long tc) {
long long n;
cin >> n;
for (long long i = (1); i <= n; i++) cin >> a[i];
;
for (long long i = (1); i <= 2 * n; i++) {
check[i] = 0;
}
for (long long i = (1); i <= n; i++) {
b[2 * i - 1] = a[i];
check[a[i]] = 1;
}
for (long long i = (1); i <= n; i++) {
long long k = b[2 * i - 1] + 1;
while (k <= 200 && check[k] == 1) {
k++;
}
b[2 * i] = k;
check[k] = 1;
}
int flag = 1;
for (long long i = (1); i <= 2 * n; i++) {
if (check[i] == 0) {
flag = 0;
break;
}
}
if (flag) {
for (long long i = (1); i <= 2 * n; i++) cout << b[i] << " ";
cout << endl;
;
} else {
cout << -1 << " ";
cout << endl;
};
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout << fixed;
cout << setprecision(10);
;
cin >> tc;
for (long long i = (1); i <= tc; i++) {
solve(i);
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import bisect
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
# sys.setrecursionlimit(200000)
# mod = 10**9+7
# mod = 987
def lcm(a,b):
return (a*b)//math.gcd(a,b)
def sort_dict(key_value):
return sorted(key_value.items(), key = lambda kv:(kv[1], kv[0]))
def list_input():
return list(map(int,input().split()))
def num_input():
return map(int,input().split())
def string_list():
return list(input())
def solve():
n = int(input())
arr = list_input()
num = {}
for i in range(1,2*n+1):
num[i] = 1
ans = []
maxx = 0
for i in arr:
maxx = max(maxx,i)
del num[i]
if maxx == 2*n:
print(-1)
return
num = list(num.keys())
for i in arr:
ans.append(i)
temp = bisect.bisect_left(num,i)
if temp == len(num):
print(-1)
return
ans.append(num[temp])
num.pop(temp)
for i in ans:
print(i,end=' ')
print()
t = int(input())
# t = 1
for _ in range(t):
solve()
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.io.*;
import java.util.*;
public class C {
public static void main(String[] args) {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Task solver = new Task();
int t = scan.nextInt();
for(int tt = 1; tt <= t; tt++) solver.solve(tt, scan, out);
out.close();
}
static class Task {
public void solve(int testNumber, FastReader scan, PrintWriter out) {
int n = scan.nextInt();
Item[] a = new Item[n];
int[] res = new int[2 * n];
for(int i = 0; i < n; i++) a[i] = new Item(i, scan.nextInt());
TreeSet<Integer> have = new TreeSet<>();
for(int i = 1; i <= 2 * n; i++) have.add(i);
for(int i = 0; i < n; i++) {
if(!have.contains(a[i].val)) {
out.println(-1);
return;
}
have.remove(a[i].val);
}
Arrays.sort(a);
for(int i = 0; i < n; i++) {
Integer next = have.last();
if(next == null || next < a[i].val) {
out.println(-1);
return;
}
have.remove(next);
a[i].other = next;
}
Arrays.sort(a, new Comparator<Item>() {
@Override
public int compare(Item o1, Item o2) {
return Integer.compare(o1.index, o2.index);
}
});
for(int k = 0; k < 500; k++) {
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j < n; j++) {
if (a[j].other < a[i].other && a[i].other > a[j].val && a[j].other > a[i].val) {
int temp = a[i].other;
a[i].other = a[j].other;
a[j].other = temp;
}
}
}
}
for(int i = 0; i < n; i++) {
res[a[i].index * 2] = a[i].val;
res[a[i].index * 2 + 1] = a[i].other;
}
for(int i : res) out.printf("%d ", i);
out.println();
}
static class Item implements Comparable<Item> {
int index, val;
int other;
public Item(int a, int b) {
index = a;
val = b;
other = -1;
}
@Override
public int compareTo(Item o) {
return Integer.compare(o.val, val);
}
}
}
static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import sys
t = int(sys.stdin.readline())
for _ in range(t):
n = int(sys.stdin.readline())
sq = []
bq = [int(b) for b in sys.stdin.readline().split()]
aq = [True] * (2*n+1)
for b in bq:
aq[b] = False
for b in bq:
sq.append(b)
j = b+1
while j <= 2*n:
if aq[j]:
aq[j] = False
sq.append(j)
break
j+=1
else:
print(-1)
break
else:
sys.stdout.write(str(sq[0]))
for s in sq[1:]:
sys.stdout.write(" ")
sys.stdout.write(str(s))
sys.stdout.write("\n")
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
map<int, int> m1;
int a[101];
int b[101];
int main() {
int t, n, i, k, key;
scanf("%d", &t);
while (t--) {
m1.clear();
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d", &b[i]);
m1[b[i]] = 1;
}
key = 1;
for (i = 1; i <= n; i++) {
k = b[i];
while (m1.count(k)) {
k++;
}
if (k <= 2 * n) {
a[i] = k;
m1[a[i]] = 1;
} else {
key = 0;
break;
}
}
if (key) {
for (i = 1; i <= n; i++) {
if (i == n) {
printf("%d %d\n", b[i], a[i]);
} else {
printf("%d %d ", b[i], a[i]);
}
}
} else {
printf("-1\n");
}
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.util.*;
import java.io.*;
import java.math.*;
/**
*
* @Har_Har_Mahadev
*/
public class C {
private static long INF = 2000000000000000000L, M = 1000000007, MM = 998244353;
private static int N = 0;
public static int lower(ArrayList<Integer> q, int target) {
int ages[] = new int[q.size()];
int ii = 0;
while(ii<q.size()) {
ages[ii] = q.get(ii);
ii++;
}
if (ages == null || ages.length == 0) {
return 0;
}
int l = 0;
int r = ages.length - 1;
if (target <= ages[0]) {
return 0;
}
if (target > ages[r]) {
return -1;
}
while (l < r) {
int m = l + (r - l ) / 2 ;
if (ages[m] >= target) {
r = m;
}else {
l = m + 1;
}
}
return r==q.size()?-1:r;
}
public static void process() throws IOException {
int n = sc.nextInt();
int arr[] = sc.readArray(n);
PriorityQueue<Pair> lis = new PriorityQueue<C.Pair>();
HashSet<Integer> q = new HashSet<Integer>();
for(int i=1; i<=2*n; i++)q.add(i);
for(int i=0; i<n; i++) {
lis.add(new Pair(arr[i], i+1));
q.remove(arr[i]);
}
if(q.size() != n) {
println(-1);
return;
}
ArrayList<Integer> qq = new ArrayList<Integer>();
for(int e : q)qq.add(e);
Collections.sort(qq);
int ans[] = new int[2*n+2];
while(!lis.isEmpty()) {
Pair e = lis.poll();
int val = lower(qq, e.x);
if(val == -1) {
println(-1);
return;
}
int vale = qq.get(val);
qq.remove(val);
int index = e.y;
ans[index*2] = vale;
ans[index*2-1] = e.x;
}
for(int i=1; i<=2*n; i++)print(ans[i]+" ");
println();
}
//=============================================================================
//--------------------------The End---------------------------------
//=============================================================================
static FastScanner sc;
static PrintWriter out;
public static void main(String[] args) throws IOException {
boolean oj = true;
if (oj) {
sc = new FastScanner();
out = new PrintWriter(System.out);
} else {
sc = new FastScanner(100);
out = new PrintWriter("output.txt");
}
int t = 1;
t = sc.nextInt();
while (t-- > 0) {
process();
}
out.flush();
out.close();
}
static class Pair implements Comparable<Pair> {
int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
return Integer.compare(this.y, o.y);
}
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (!(o instanceof Pair)) return false;
// Pair key = (Pair) o;
// return x == key.x && y == key.y;
// }
//
// @Override
// public int hashCode() {
// int result = x;
// result = 31 * result + y;
// return result;
// }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static void println(Object o) {
out.println(o);
}
static void println() {
out.println();
}
static void print(Object o) {
out.print(o);
}
static void pflush(Object o) {
out.println(o);
out.flush();
}
static int ceil(int x, int y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static long ceil(long x, long y) {
return (x % y == 0 ? x / y : (x / y + 1));
}
static int max(int x, int y) {
return Math.max(x, y);
}
static int min(int x, int y) {
return Math.min(x, y);
}
static int abs(int x) {
return Math.abs(x);
}
static long abs(long x) {
return Math.abs(x);
}
static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
static long max(long x, long y) {
return Math.max(x, y);
}
static long min(long x, long y) {
return Math.min(x, y);
}
public static int gcd(int a, int b) {
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.intValue();
}
public static long gcd(long a, long b) {
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.longValue();
}
public static long lca(long a, long b) {
return (a * b) / gcd(a, b);
}
public static int lca(int a, int b) {
return (a * b) / gcd(a, b);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner() throws FileNotFoundException {
br = new BufferedReader(new InputStreamReader(System.in));
}
FastScanner(int a) throws FileNotFoundException {
br = new BufferedReader(new FileReader("input.txt"));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
String nextLine() throws IOException {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) throws IOException {
int[] A = new int[n];
for (int i = 0; i != n; i++) {
A[i] = sc.nextInt();
}
return A;
}
long[] readArrayLong(int n) throws IOException {
long[] A = new long[n];
for (int i = 0; i != n; i++) {
A[i] = sc.nextLong();
}
return A;
}
}
static void ruffleSort(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
Arrays.sort(a);
}
}
| JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | t = int(input())
for j in range(t):
n = int(input())
values = [0]*(2*n+2)
b = list(map(int, input().split()))
a = [0]*(2*n+2)
#print(a,b)
for i in range(n):
a[2*i] = b[i]
values[b[i]] = 1
for i in range(n):
check = False
if b[i] >= 2*n:
break
if values[b[i]+1] == 0:
#print("Entered direct")
values[b[i]+1] = 1
a[2*i+1] = b[i]+1
check = True
else:
for p in range(b[i]+1,2*n+1):
if values[p]==0:
a[2*i+1] = p
values[p] = 1
check = True
break
if check == False:
break
if check == False:
break
#print(a)
success = True
for i in range(2*n):
if a[i] > 2*n or a[i] == 0:
success = False
break
if success == False:
print(-1)
else:
for i in range(2*n):
print(a[i],end = ' ')
print() | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long t, n, x[1005000];
void solve() {
cin >> n;
set<long long> s;
vector<pair<long long, long long> > b;
vector<pair<long long, pair<long long, long long> > > v;
for (int i = 1; i <= 2 * n; i++) s.insert(i);
for (int i = 1; i <= n; i++)
cin >> x[i], s.erase(x[i]), b.push_back({x[i], i});
vector<long long> a((s).begin(), (s).end());
sort((b).begin(), (b).end());
for (int i = 0; i < n; i++) {
if (a[i] <= b[i].first) {
cout << -1 << endl;
return;
}
}
for (int i = 0; i < n; i++) swap(b[i].first, b[i].second);
sort((b).begin(), (b).end());
for (int i = 0; i < n; i++) {
cout << b[i].second << ' ' << *(s.upper_bound(b[i].second)) << ' ';
s.erase(s.upper_bound(b[i].second));
}
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) solve();
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | from typing import List
import collections
import bisect
import itertools
import functools
from fractions import gcd
import heapq
from math import ceil, sqrt, floor
# import sys
# sys.setrecursionlimit(50000)
def bisect_ceil(A, target):
low = 0
high = len(A) - 1
if not A or A[-1] < target:
return -1
if A[0] > target:
return 0
while low <= high:
mid = (low + high) // 2
midv = A[mid]
if midv == target:
return mid
elif target > midv:
low = mid + 1
elif mid - 1 >= 0 and A[mid-1] < target < A[mid]:
return mid
else:
high = mid - 1
t = int(input())
for _ in range(t):
n = int(input())
B = list(map(int, input().split()))
B_set = set(B)
candidate = [i for i in range(1, 2*n+1) if i not in B_set]
result = [0] * (2 * n)
p = ""
for i in range(n):
result[2*i] = B[i]
index = bisect_ceil(candidate, B[i])
if index == -1:
p = '-1'
break
result[2*i+1] = candidate[index]
del candidate[index]
if p:
print(p)
else:
print(' '.join([str(i) for i in result]))
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long t, n, b[100009], f[100009], j, fl;
struct st {
long long x, i;
} a[100009];
bool cmp(st a, st b) { return a.x < b.x; }
bool cm(st a, st b) { return a.i < b.i; }
int main() {
cin >> t;
while (t--) {
j = 1;
fl = 0;
cin >> n;
for (int i = 0; i <= n * 2 + 50; i++) b[i] = 0, f[i] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i].x;
a[i].i = i;
f[a[i].x] = 1;
}
for (int i = 1; i <= n; i++) {
j = a[i].x;
while (f[j]) j++;
b[a[i].i * 2 - 1] = a[i].x;
b[a[i].i * 2] = j;
f[j] = 1;
}
for (int i = 1; i <= n; i++) {
if (a[i].x != min(b[i * 2], b[i * 2 - 1]) || b[i * 2] > 2 * n ||
b[i * 2 - 1] > 2 * n) {
fl = 1;
break;
}
}
if (fl)
cout << -1 << endl;
else {
for (int i = 1; i <= n * 2; i++) cout << b[i] << " ";
cout << endl;
}
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
check = [True] * (2 * n)
for i in range(n):
check[a[i]-1] = False
res = []
right = []
for idx, value in enumerate(a):
res.append(value)
i = value - 1
while i < 2*n and not check[i]:
i += 1
if i < 2*n:
check[i] = False
res.append(i+1)
else:
res = [-1]
break
print(*res) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.util.*;
import java.awt.print.Book;
import java.io.*;
import java.math.BigInteger;
public class A {
static long mod = (long) (1e9 + 7);
public static long facn(long n, long x) {
long ans = 1;
for (int i = 1; i <= x; i++) {
ans *= n;
ans %= mod;
n--;
}
long k = 1;
long t = x;
for (int i = 1; i <= t; i++) {
k *= x;
k %= mod;
x--;
}
// BigInteger b = BigInteger.valueOf((k)).modInverse(BigInteger.valueOf((mod)));
long b = modPow(k, mod - 2, mod);
return ((b % mod) * ans) % mod;
}
public static long modInverse(long a, long m) {
a = a % m;
for (int x = 1; x < m; x++)
if ((a * x) % m == 1)
return x;
return 1;
}
static long modPow(long a, long e, long mod) // O(log e)
{
a %= mod;
long res = 1;
while (e > 0) {
if ((e & 1) == 1)
res = (res * a) % mod;
a = (a * a) % mod;
e >>= 1;
}
return res % mod;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
// pw=new PrintWriter("C:\\Users\\Hp\\Desktop\\outt.txt");
int t=sc.nextInt();
out: while(t-->0) {
int n=sc.nextInt();
HashSet<Integer>hs=new HashSet<Integer>();
boolean[]vis=new boolean[2*n+1];
int[]Arr=new int[n];
for(int i=0;i<n;i++) {
int a=sc.nextInt();
Arr[i]=a;
vis[a]=true;
hs.add(a);
}
ArrayList<Integer>arr=new ArrayList<Integer>();
for(int i=0;i<n;i++) {
int a=Arr[i];
arr.add(Arr[i]);
for(int j=a+1;j<=2*n;j++) {
if(!vis[j]) {
arr.add(j);
vis[j]=true;
break;
}
}
}
if(arr.size()!=2*n) {
pw.print(-1);
}else {
for(int x:arr)
pw.print(x+" ");
}
pw.println();
}
pw.close();
}
public static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException, IOException {
return br.ready();
}
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import sys
import math
get_string = lambda: sys.stdin.readline().strip()
get_list = lambda: list( map(int,sys.stdin.readline().strip().split()) )
get_int = lambda: int(sys.stdin.readline())
#----------------------------------------------------------------------#
for _ in range(get_int()):
n=get_int()
b=get_list()
l=2*n+1
done=[0]*l
for i in b:
done[i]=1
ans=[]
for i in b:
ans.append(i)
for j in range(i+1,l):
if(done[j])==0:
done[j]=1
ans.append(j)
break
else:
print(-1)
break
else:
print(*ans)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int arr[106];
int memo[206];
int solve(int n, int reff) {
for (int i = 1; i <= n; i++) {
if ((memo[i] == -1) && i > reff) {
memo[i] = 1;
return i;
}
}
return 0;
}
int main() {
int t;
cin >> t;
while (t--) {
int n, x;
memset(memo, -1, sizeof memo);
cin >> n;
int cnt = 1;
for (int i = 0; i < n; i++) {
cin >> x;
arr[cnt] = x;
memo[x] = 1;
cnt += 2;
}
n *= 2;
bool chk = false;
for (int i = 1; i <= n; i++) {
if (!(i & 1)) {
arr[i] = solve(n, arr[i - 1]);
}
if (!arr[i]) {
chk = true;
break;
}
}
if (chk) {
cout << -1 << endl;
continue;
}
for (int i = 1; i <= n; i++) cout << arr[i] << ' ';
cout << endl;
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | t = int(input())
for _ in range(t):
n = int(input())
seq = list(map(int, input().split()))
sol = [0]*(2*n)
nums = {i for i in range(1,2*n+1)}
for i in range(n):
sol[2*i] = seq[i]
nums.remove(seq[i])
geht = True
for i in range(n):
cand = [el for el in nums if el > sol[2*i]]
if len(cand) == 0:
geht = False
break
sol[2*i + 1] = min(cand)
nums.remove(min(cand))
if geht:
print(*sol)
else:
print(-1) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int test;
cin >> test;
while (test--) {
int n;
cin >> n;
int a[n + 5];
int v[2 * n + 10], u[2 * n + 10];
for (int i = 1; i <= 2 * n; i++) {
v[i] = i;
u[i] = i;
}
int j = 1;
for (int i = 1; i <= n; i++) {
cin >> a[i];
v[a[i]] = 0;
u[a[i]] = 0;
}
int s = 0;
for (int i = 1; i <= n; i++) {
for (int j = a[i]; j <= 2 * n; j++) {
if (v[j] > a[i]) {
s++;
v[j] = 0;
break;
}
}
}
if (s != n)
cout << "-1";
else {
for (int i = 1; i <= n; i++) {
for (int j = a[i]; j <= 2 * n; j++) {
if (u[j] > a[i]) {
cout << a[i] << " " << u[j] << " ";
u[j] = 0;
break;
}
}
}
}
cout << endl;
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | kl = int(input())
for l in range(kl):
n = int(input())
t = n * 2
at = list(range(t + 1))
nl = [1] * t
b = []
rz = []
pr = 0
# print(nl)
# print(at)
for i in input().split():
i = int(i)
nl[i - 1] = 0
b = b + [i]
# print(nl)
# print(b)
for i in range(n):
if pr:
break
pr = 1
# print(b[i], t)
# print(at)
# print(nl)
for j in range(b[i], t + 1):
if at[j] * nl[j - 1] != 0:
nl[j - 1] = 0
rz = rz + [b[i]] + [at[j]]
pr = 0
break
# print(nl)
# print(b)
# print(rz)
if pr:
print(-1)
else:
for i in range(t):
print(rz[i], '', end='')
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int n, a[209], vis[209], b[109];
set<int> s;
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
cin >> n;
s.clear();
for (int i = 0; i < n * 2; i++) s.insert(i + 1);
for (int i = 0; i < n; i++) {
cin >> b[i];
s.erase(b[i]);
}
for (int i = 0; i < n * 2; i += 2) a[i] = b[i / 2];
bool no = 0;
memset(vis, 0, sizeof(vis));
for (auto it = s.begin(); it != s.end(); it++) {
int b = -1, x = *it;
for (int i = 0; i < n * 2; i += 2) {
if (a[i] < x && b == -1 && vis[i] == 0) b = i;
}
a[b + 1] = x;
if (b == -1) no = 1;
if (no) break;
vis[b] = 1;
}
if (no)
cout << -1;
else
for (int i = 0; i < 2 * n; i++) cout << a[i] << " ";
cout << "\n";
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | for _ in range(int(input())):
n=int(input())
b=[int(x) for x in input().split()]
a=[]
for i in range(n):
a.append(b[i])
k=b[i]
while(k in a or k in b):
k+=1
if(k>2*n):
print(-1)
break
a.append(k)
else:
print(*a) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | /*
/$$$$$ /$$$$$$ /$$ /$$ /$$$$$$
|__ $$ /$$__ $$| $$ | $$ /$$__ $$
| $$| $$ \ $$| $$ | $$| $$ \ $$
| $$| $$$$$$$$| $$ / $$/| $$$$$$$$
/$$ | $$| $$__ $$ \ $$ $$/ | $$__ $$
| $$ | $$| $$ | $$ \ $$$/ | $$ | $$
| $$$$$$/| $$ | $$ \ $/ | $$ | $$
\______/ |__/ |__/ \_/ |__/ |__/
/$$$$$$$ /$$$$$$$ /$$$$$$ /$$$$$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$$$$$$$ /$$$$$$$
| $$__ $$| $$__ $$ /$$__ $$ /$$__ $$| $$__ $$ /$$__ $$| $$$ /$$$| $$$ /$$$| $$_____/| $$__ $$
| $$ \ $$| $$ \ $$| $$ \ $$| $$ \__/| $$ \ $$| $$ \ $$| $$$$ /$$$$| $$$$ /$$$$| $$ | $$ \ $$
| $$$$$$$/| $$$$$$$/| $$ | $$| $$ /$$$$| $$$$$$$/| $$$$$$$$| $$ $$/$$ $$| $$ $$/$$ $$| $$$$$ | $$$$$$$/
| $$____/ | $$__ $$| $$ | $$| $$|_ $$| $$__ $$| $$__ $$| $$ $$$| $$| $$ $$$| $$| $$__/ | $$__ $$
| $$ | $$ \ $$| $$ | $$| $$ \ $$| $$ \ $$| $$ | $$| $$\ $ | $$| $$\ $ | $$| $$ | $$ \ $$
| $$ | $$ | $$| $$$$$$/| $$$$$$/| $$ | $$| $$ | $$| $$ \/ | $$| $$ \/ | $$| $$$$$$$$| $$ | $$
|__/ |__/ |__/ \______/ \______/ |__/ |__/|__/ |__/|__/ |__/|__/ |__/|________/|__/ |__/
*/
import java.util.*;
import java.io.*;
import java.math.*;
import java.io.FileWriter;
import java.io.IOException;
public class cf{
static class Pair{
int x;
int y;
public Pair(int x,int y){
this.x=x;
this.y=y;
}
}
static InputReader sc;
static PrintWriter pw;
////////////////////////////////////Code Start/////////////////////////////////////////////////
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
sc = new InputReader(inputStream);
pw = new PrintWriter(outputStream);
solve();
pw.close();
}
public static void solve() {
int t=s(0);
while(t-->0){
int n=s(0);
int a[]=new int[n];
feedArr(a);
HashSet<Integer> set=new HashSet<>();
for(int i=0;i<n;i++){
set.add(a[i]);
}
ArrayList<Integer> arr=new ArrayList<>();
for(int i=0;i<n;i++){
arr.add(a[i]);
if(set.contains(a[i]+1)==false){
arr.add(a[i]+1);
set.add(a[i]+1);
}
else{
int x=a[i];
while(set.contains(x)==true){
x++;
}
arr.add(x);
set.add(x);
}
}
boolean f=true;
for(int i=0;i<arr.size();i++){
if(arr.get(i)>2*n){
f=false;
}
}
if(f){
for(int i=0;i<arr.size();i++){
System.out.print(arr.get(i)+" ");
}
System.out.println();
}
else{
System.out.println("-1");
}
}
}
////////////////////////////////////Code End//////////////////////////////////////////////////
static int LowerBound(ArrayList<Integer> a, int x) {
int l=-1,r=a.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(a.get(m)>=x) r=m;
else l=m;
}
return r;
}
static int UpperBound(int a[], int x) {
int l=-1,r=a.length;
while(l+1<r) {
int m=(l+r)>>>1;
if(a[m]<=x) l=m;
else r=m;
}
return l+1;
}
public static String sortString(String inputString)
{
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
public static long atMostK(char []chrr, int k){
if(k<0)
return 0;
int l=0,cnt=0;
long ans=0l;
for(int i=0;i<chrr.length;i++){
if(chrr[i]=='1')
cnt++;
while(cnt>k){
if(chrr[l++]=='1')
cnt--;
}
ans+=(long)(i-l)+1l;
}
return ans;
}
public static int ask(int l, int r){
System.out.println("? "+l+" "+r);
System.out.flush();
return sc.nextInt();
}
public static void sort(long []arr){
ArrayList<Long> list=new ArrayList<>();
for(int i=0;i<arr.length;i++)
list.add(arr[i]);
Collections.sort(list);
for(int i=0;i<arr.length;i++)
arr[i]=list.get(i);
}
public static void swap(char []chrr, int i, int j){
char temp=chrr[i];
chrr[i]=chrr[j];
chrr[j]=temp;
}
public static int countSetBits(int n){
int a=0;
while(n>0){
a+=(n&1);
n>>=1;
}
return a;
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return a>b?gcd(b, a % b):gcd(a, b % a);
}
static long fast_pow(long base,long n,long M){
if(n==0)
return 1;
if(n==1)
return base;
long halfn=fast_pow(base,n/2,M);
if(n%2==0)
return ( halfn * halfn ) % M;
else
return ( ( ( halfn * halfn ) % M ) * base ) % M;
}
static long modInverse(long n,long M){
return fast_pow(n,M-2,M);
}
public static int s(int n){
return sc.nextInt();
}
public static long s(long n){
return sc.nextLong();
}
public static String s(String n){
return sc.next();
}
public static double s(double n){
return sc.nextDouble();
}
public static void p(int n){
pw.print(n);
}
public static void p(long n){
pw.print(n);
}
public static void p(String n){
pw.print(n);
}
public static void p(double n){
pw.print(n);
}
public static void pln(int n){
pw.println(n);
}
public static void pln(long n){
pw.println(n);
}
public static void pln(String n){
pw.println(n);
}
public static void pln(double n){
pw.println(n);
}
public static void feedArr(long []arr){
for(int i=0;i<arr.length;i++)
arr[i]=sc.nextLong();
}
public static void feedArr(double []arr){
for(int i=0;i<arr.length;i++)
arr[i]=sc.nextDouble();
}
public static void feedArr(int []arr){
for(int i=0;i<arr.length;i++)
arr[i]=sc.nextInt();
}
public static void feedArr(String []arr){
for(int i=0;i<arr.length;i++)
arr[i]=sc.next();
}
public static String printArr(int []arr){
StringBuilder sbr=new StringBuilder();
for(int i:arr)
sbr.append(i+" ");
return sbr.toString();
}
public static String printArr(long []arr){
StringBuilder sbr=new StringBuilder();
for(long i:arr)
sbr.append(i+" ");
return sbr.toString();
}
public static String printArr(String []arr){
StringBuilder sbr=new StringBuilder();
for(String i:arr)
sbr.append(i+" ");
return sbr.toString();
}
public static String printArr(double []arr){
StringBuilder sbr=new StringBuilder();
for(double i:arr)
sbr.append(i+" ");
return sbr.toString();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
| JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | I=input
for _ in[0]*int(I()):
n=2*int(I());a=[];b=*map(int,I().split()),;c={*range(n+1)}-{*b};i=1
try:
for x in b:y=min(c-{*range(x)});c-={y};a+=x,y;i+=2
except:a=-1,
print(*a) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t-- > 0) {
int n;
cin >> n;
int ar[n];
unordered_map<int, int> umap;
for (int i = 0; i < n; i++) {
cin >> ar[i];
umap[ar[i]] = 1;
}
int ans[2 * n];
int k = 0;
int flag1 = 0;
for (int i = 0; i < n; i++) {
int flag = 0;
for (int j = ar[i] + 1; j <= (2 * n); j++) {
if (umap.find(j) == umap.end()) {
ans[k] = ar[i];
ans[k + 1] = j;
umap[j] = 1;
flag = 1;
k += 2;
break;
}
}
if (flag == 0) {
flag1 = 1;
break;
}
}
if (flag1 == 1) {
cout << "-1\n";
} else {
for (int i = 0; i < 2 * n; i++) {
if (i == (2 * n) - 1) {
cout << ans[i] << "\n";
} else {
cout << ans[i] << " ";
}
}
}
umap.clear();
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | t=int(input())
for i in range(t):
n=int(input())
ans=[]
l=[int(i) for i in input().split()]
unl=[]
for i in range(1,2*n+1):
if i not in l:
unl.append(i)
#print(l,unl)
ok=True
for i in range(2*n):
if i%2==0:
ans.append(l[i//2])
else:
for i in range(len(unl)):
if unl[i]>ans[-1]:
ans.append(unl[i])
unl[i]=0
break
else:
ok=False
print(-1)
break
if ok==True:
print(*ans)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
signed main() {
long long nn;
cin >> nn;
while (nn--) {
long long n;
cin >> n;
vector<long long> vec(n + 1);
vector<long long> res(2 * n + 1);
vector<long long> vissited(2 * n + 1, 0);
for (long long i = 1; i <= n; ++i) {
cin >> vec[i];
res[2 * i - 1] = vec[i];
vissited[vec[i]] = 1;
}
bool ctr = 0;
for (long long j = 1; j <= n; ++j) {
long long cur = vec[j] + 1;
while (cur <= 2 * n and vissited[cur] == 1) cur++;
if (cur == 2 * n + 1) {
cout << -1 << "\n";
ctr = 1;
break;
}
vissited[cur] = 1;
res[2 * j] = cur;
}
if (ctr) continue;
for (long long k = 1; k <= 2 * n; ++k) {
cout << res[k] << " ";
}
cout << "\n";
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | from math import *
from collections import *
t = int(input())
for y in range(t):
n = int(input())
a = list(map(int,input().split()))
ch = [0 for i in range(2*n+1)]
b = [-1 for i in range(2*n)]
for i in range(n):
ch[a[i]] = 1
b[2*i] = a[i]
flag = 0
for i in range(0,2*n,2):
m = -1
for j in range(b[i],2*n+1):
if(ch[j] == 0):
ch[j] = 1
m = j
break
if(m == -1):
flag = -1
break
b[i+1] = m
#print(ch)
if(flag == -1):
print(-1)
else:
for i in b:
print(i,end = " ")
print() | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long int mod = 1000000007;
long long int power(long long int a, long long int c) {
if (c == 0) return 1;
if (c % 2 == 0) {
long long int m = power(a, c / 2);
return (m * m) % mod;
}
return (a * power(a, c - 1)) % mod;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
long long int t;
cin >> t;
for (long long int w1 = 0; w1 < t; w1++) {
long long int n;
cin >> n;
long long int a[n];
map<long long int, long long int> m;
for (long long int i = 0; i < n; i++) {
cin >> a[i];
m[a[i]]++;
}
vector<long long int> v;
for (long long int i = 0; i < n; i++) {
for (long long int j = a[i]; j <= 2 * n; j++) {
if (m[j] == 0) {
v.push_back(a[i]);
v.push_back(j);
m[j]++;
break;
}
}
}
if (v.size() != 2 * n)
cout << -1 << endl;
else {
for (long long int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
public class Two {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
int t;
t = in.nextInt();
//t = 1;
while (t > 0) {
solver.call(in,out);
t--;
}
out.close();
}
static class TaskA {
public void call(InputReader in, PrintWriter out) {
int n , a=0 ,b =0;
n = in.nextInt();
int[] arr = new int[n];
int[] array = new int[2*n+1];
ArrayList<Integer> arrayList = new ArrayList<>();
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
array[arr[i]]++;
if(arr[i]==2*n)
a++;
if(arr[i]==1)
b++;
}
if(a!=0 || b==0){
out.println(-1);
}
else{
for (int i = 0; i < n; i++) {
arrayList.add(arr[i]);
for (int j = arr[i]+1; j <array.length ; j++) {
if(array[j]==0){
arrayList.add(j);
array[j]++;
break;
}
}
}
if(arrayList.size()==2*n){
for (int i=0;i<arrayList.size();i++)
out.print(arrayList.get(i)+" ");
out.println();
}
else
out.println(-1);
}
}
}
static class answer implements Comparable<answer>{
int a, b;
public answer(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer o) {
return this.a - o.a;
}
}
static long lcm(long a , long b){
return (a/gcd(a,b)) *b;
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static final Random random=new Random();
static void shuffleSort(int[] arr) {
int n = arr.length;
for (int i=0; i<n; i++) {
int a=random.nextInt(n), temp=arr[a];
arr[a]=arr[i];
arr[i]=temp;
}
Arrays.sort(arr);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | '''
@sksshivam007 - Template 1.0
'''
import sys, re, math
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx): return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))[::-1]
toret = {}
for tup in temp:
toret[tup[0]] = tup[1]
return toret
def sortDictWithKey(passedDic):
return dict(OrderedDict(sorted(passedDic.items())))
INF = float('inf')
mod = 10 ** 9 + 7
t = INT()
while(t!=0):
n = INT()
b = LIST()
if((1 not in b) or (2*n in b)):
print(-1)
else:
left = []
for i in range(1, 2*n+1):
if(i not in b):
left.append(i)
left.sort()
final = []
# print(left)
for i in range(n):
flag = 0
for j, el in enumerate(left):
if(el>b[i]):
final.append(b[i])
final.append(el)
flag = 1
break
if(flag==1):
left.pop(j)
else:
break
if(flag==0):
print(-1)
else:
print(*final)
t-=1
'''
1 2 3 4
5 6 7 8
1 5 2 6 3 7 4 8
''' | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | #include <bits/stdc++.h>
template <typename T>
std::istream& operator>>(std::istream& input, std::vector<T>& v) {
for (T& a : v) input >> a;
return input;
}
void no_answer() { std::cout << -1 << '\n'; }
void answer(const std::vector<unsigned>& v) {
const char* separator = "";
for (const unsigned x : v) {
std::cout << separator << x;
separator = " ";
}
std::cout << '\n';
}
void solve(const std::vector<unsigned>& b) {
const size_t n = b.size();
std::set<unsigned> s;
for (unsigned x = 1; x <= 2 * n; ++x) s.insert(x);
for (const unsigned x : b) s.erase(x);
std::vector<unsigned> a(2 * n);
for (size_t i = 0; i < n; ++i) {
const auto it = s.upper_bound(b[i]);
if (it == s.cend()) return no_answer();
a[2 * i + 0] = b[i];
a[2 * i + 1] = *it;
s.erase(it);
}
answer(a);
}
void test_case() {
size_t n;
std::cin >> n;
std::vector<unsigned> b(n);
std::cin >> b;
solve(b);
}
int main() {
size_t t;
std::cin >> t;
while (t-- > 0) test_case();
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 |
import java.util.Arrays;
import java.util.Scanner;
public class C_623 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for(int tc=0;tc<q;tc++){
int n=in.nextInt();
int[] b = new int[n];
int[] a = new int[2*n];
int[] p = new int[2*n+1];
Arrays.fill(p, 1);
for(int i=0;i<n;i++){
b[i] = in.nextInt();
a[2*i] = b[i];
p[b[i]] = 0;
}
for(int i=0;i<n;i++){
int j=0;
for(j=a[2*i]+1;j<=2*n;j++){
if(p[j]==1){
break;
}
}
if(j==2*n+1){
a[0] = -1;
break;
}
a[2*i+1] = j;
p[j] = 0;
}
if(a[0]==-1){
System.out.println(-1);
}
else {
for (int i=0;i<2*n;i++){
System.out.print(a[i] + " ");
}
System.out.println();
}
}
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), 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 b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 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 a_1, β¦, a_{2n} β 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 | 2 | 9 | I=input
for _ in[0]*int(I()):
n=2*int(I());a=[0]*n;b=a[::2]=*map(int,I().split()),;c={*range(n+1)}-{*b};i=1
try:
for x in b:y=a[i]=min(c-{*range(x)});c-={y};i+=2
except:a=-1,
print(*a) | PYTHON3 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.