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; const int mod = 998244353; const int nag = 2e5 + 5; template <typename T> void add(T& a, T b) { a += b; while (a >= mod) a -= mod; while (a < 0) a += mod; } template <typename T> void mul(T& a, T b) { a = a * b % mod; } template <typename T> void up_self(T& a, T b) { a = max(a, b); } template <typename T> void min_self(T& a, T b) { a = min(a, b); } string find_largest_pref_palindrome(const string& a) { string second = a; reverse(second.begin(), second.end()); second = a + "%" + second; int c = 0; vector<int> pref(2 * ((int)a.size()) + 2); for (int i = 1; i < second.size(); i++) { while (c != 0 && second[i] != second[c]) c = pref[c - 1]; if (second[i] == second[c]) c++; pref[i] = c; } return second.substr(0, c); } long long int binexpomodulo(long long int x, long long int y) { long long int res = 1; x %= mod; if (!x) return 0; while (y) { if (y & 1) { mul(res, x); } mul(x, x); y >>= 1; } return res; } long long int nCrInOr(long long int n, long long int r) { long long int res = 1; if (r > n - r) r = n - r; long long int rin = 1; for (long long int i = 1; i <= r; i++) rin = (rin * i) % mod; rin = binexpomodulo(rin, mod - 2); for (long long int i = 1; i <= r; i++) res = (res * (n - i + 1)) % mod; res = (res * rin) % mod; return res; } int msb(long long int n) { int ans; for (int i = 0; i < 64; i++) if (n & (1LL << i)) ans = i + 1; return ans; } long long int get_(vector<long long int>& a, int pos) { long long int res = 0; for (; pos >= 0; pos = (pos & (pos + 1)) - 1) { res += a[pos]; } return res; } void upd(vector<long long int>& a, int pos, int val) { for (; pos < a.size(); pos = (pos | (pos + 1))) a[pos] += val; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; cin >> t; while (t--) { int n; cin >> n; vector<int> b(n), a(2 * n); for (int i = 0; i < n; i++) cin >> b[i]; ; set<int> in_b; for (int i : b) { in_b.insert(i); } vector<bool> available(2 * n + 1, true); bool cant = false; for (int i = 0; i < n; i++) { int c = 0; for (int j = b[i]; j <= 2 * n; j++) { if ((c == 1 && available[j] && (in_b.find(j) == in_b.end())) || (!c)) { available[j] = false; a[2 * i + c] = j; c++; } if (c == 2) break; } if (c < 2) { cant = true; break; } } if (cant) cout << -1 << "\n"; else { for (int i = 0; i < a.size(); i++) cout << a[i] << " "; 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; const long long mod = 1000000007; const long long inf = 1000000000; const long long N = 2 * (1e2) + 5; long long ar[N]; long long ar2[N]; long long ar3[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long t = 1; cin >> t; while (t--) { long long n; cin >> n; vector<long long> done(2 * n + 1, 0); for (long long i = 1; i <= n; ++i) { cin >> ar[i]; done[ar[i]] = 1; } long long j = 1; for (long long i = 1; i <= 2 * n; ++i) { if (!done[i]) { ar2[j++] = i; } } long long d = 1; for (long long i = 1; i <= n; ++i) { long long b = 0; long long j = lower_bound(ar2 + 1, ar2 + 1 + n, ar[i] + 1) - ar2; while (j <= n) { if (!done[ar2[j]]) { done[ar2[j]] = 1; ar3[i] = ar2[j]; b = 1; break; } j++; } if (!b) { d = 0; break; } } if (!d) cout << -1 << "\n"; else { for (long long i = 1; i <= n; ++i) { cout << ar[i] << " " << ar3[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
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0), ios::sync_with_stdio(false); int t, n; cin >> t; while (t--) { cin >> n; vector<int> a(n), b(2 * n); vector<bool> used(2 * n + 1, false); for (int i = 0; i < n; ++i) { cin >> a[i]; used[a[i]] = true; } bool ok = true; for (int i = 1; i <= 2 * n && ok; ++i) { if (used[i]) continue; bool tmp = false; for (int j = 0; j < n; ++j) { if (i > a[j]) { b[2 * j] = a[j]; b[2 * j + 1] = i; a[j] = 205; tmp = true; break; } } ok = tmp; } if (ok) { for (int i = 0; i < 2 * n; ++i) { cout << b[i] << " \n"[i == 2 * n - 1]; } } else { cout << "-1\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
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class ProblemC { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int tt = 0; tt < t; tt++) { int n = sc.nextInt(); int[] input = new int[n]; int[] output = new int[2*n]; Set<Integer> used = new HashSet<>(); for(int i = 0; i < n; i++) { input[i] = sc.nextInt(); used.add(input[i]); } int max = 2*n; boolean impossible = false; for(int i = 1; i < 2*n; i += 2) { output[i-1] = input[i/2]; int nextNotUsed = output[i-1]+1; while (used.contains(nextNotUsed)) nextNotUsed++; if(nextNotUsed > max) { impossible = true; break; } used.add(nextNotUsed); output[i] = nextNotUsed; } if(impossible) { System.out.println(-1); } else { for(int i = 0; i < 2* n; i++) { System.out.print(output[i]); if(i < (2 * n) -1) { System.out.print(" "); } } 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
# from collections import defaultdict for _ in range(int(input())): n = int(input()) b = list(map(int, input().split())) a = [] taken = [False] * (2*n+1) for bi in b: taken[bi] = True for bi in b: a.append(bi) next = bi+1 while next <= 2*n and taken[next]: next += 1 if next <= 2*n: a.append(next) taken[next] = 1 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
t = int(input()) b_all = list() for i in range(t): n = int(input()) b_str = input().split(' ') b_all.append([int(r) for r in b_str]) results = list() for b in b_all: n = len(b) used = [False] * (2*n+1) in_res = [False] * (2*n+1) bad_seq = False for num in b: if num > 2*n: bad_seq = True break used[num] = True if bad_seq: print('-1') continue res = [] for j in b: res.append(str(j)) in_res[j] = True try: false_index = used.index(False, j) res.append(str(false_index)) in_res[false_index] = True used[false_index] = True except ValueError: res = ['-1'] break print(' '.join(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
#include <bits/stdc++.h> using namespace std; const int mod = 998244353.; const int N = 1e6 + 5; bool sortbysec(const pair<pair<long long, long long>, long long> &a, const pair<pair<long long, long long>, long long> &b) { return (a.second > b.second); } long long powermod(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } bool 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; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; loop: while (t--) { long long n; cin >> n; vector<long long> a(n); set<long long> s; for (long long i = 0; i < n; i++) { cin >> a[i]; s.insert(a[i]); } vector<long long> b; for (long long i = 0; i < n; i++) { b.push_back(a[i]); s.insert(a[i]); long long f = 0; for (long long j = a[i] + 1; j <= 2 * n; j++) { if (s.find(j) == s.end()) { f = 1; s.insert(j); b.push_back(j); break; } } if (f == 0) { cout << -1 << "\n"; goto loop; } } for (long long i = 0; i < b.size(); i++) cout << b[i] << " "; ; 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 sys queries = int(sys.stdin.readline()) for query in range(queries): length = int(sys.stdin.readline()) arr = [int(x) for x in sys.stdin.readline().strip().split()] mx = arr[0] ans_arr = [0] * (length*2) arr_ins = [x for x in range(length*2,0,-1)] for el in arr: del arr_ins[arr_ins.index(el)] for pos in range(length): ans_arr[pos*2] = arr[pos] ins_pos = len(arr_ins)-1 while ins_pos >= 0 and arr_ins[ins_pos] < arr[pos]: ins_pos -= 1 if ins_pos == -1: ans_arr = ['-1'] break else: ans_arr[pos*2+1] = arr_ins[ins_pos] del arr_ins[ins_pos] #print(arr_ins) print(" ".join(map(str,ans_arr)))
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): op=[] n=int(input()) x=list(input().split()) xi=[int(i) for i in x] for j in range(2*n): if j%2==0: t=xi[j//2] op.append(t) else: op.append(0) xx=1 for j in range(n): z=op[xx-1]+1 if z in op: for q in range(z+1,(2*n+1)): if q not in op: op[xx]=q break else: op[xx]=z xx+=2 if max(op)>(2*n): print("-1") elif 0 in op: print("-1") else: for j in range(len(op)): print(op[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
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n, i, p = 0, q, h, r = 0, j; cin >> n; long long a[n], b[2 * n], c[2 * n]; memset(c, 0, sizeof(c)); for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { c[a[i] - 1] = 1; } for (i = 0; i < n; i++) { h = 1; b[2 * i] = a[i]; for (j = a[i]; j < 2 * n; j++) { if (c[j] != 1) { break; } } b[2 * i + 1] = j + 1; if (j < 2 * n) c[j] = 1; } for (i = 0; i < 2 * n; i++) { if (b[i] <= 2 * n) c[b[i] - 1] = 1; } for (i = 0; i < 2 * n; i++) { if (c[i] == 0) { r = 1; break; } } if (r == 1) { cout << "-1" << endl; } else { for (i = 0; i < 2 * n; 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
#include <bits/stdc++.h> using namespace std; const int N = 410; int b[N]; int a[N]; int t, n; bool used[N]; int find(int x) { for (int i = x + 1; i <= 2 * n; i++) { if (!used[i]) { return i; } } return -1; } int main() { ios::sync_with_stdio(false); cin >> t; while (t--) { memset(used, 0, sizeof(used)); cin >> n; for (int i = 1; i <= n; i++) { cin >> b[i]; used[b[i]] = 1; } bool noans = 0; for (int i = 1; i <= n; i++) { int f = find(b[i]); if (f == -1) { noans = 1; cout << -1 << endl; break; } else { a[2 * i - 1] = b[i]; a[2 * i] = f; used[f] = 1; } } if (!noans) { for (int i = 1; i <= 2 * n; i++) { cout << a[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 input = sys.stdin.readline for j in range(int(input())): n = int(input()) b = list(map(int, input().split(" "))) save = b.copy() temp = set(b) c =[] for k in range(1, 2*n+1): if k not in temp: c.append(k) b.sort() c.sort() ansDict = {} ans = 0 for lol in range(n): if(b[lol]>c[lol]): ans = -1 for sad in range(n): mini = max(c) for f in c: if(f<mini and f>save[sad]): mini = f ansDict[save[sad]] = mini c.remove(mini) if(ans==-1): print(-1) else: ans = [] for go in range(n): ans.append(str(save[go])) ans.append(str(ansDict[save[go]])) print(" ".join(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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class test { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); StringTokenizer st; for(int i=0;i<t;i++) { int n=Integer.parseInt(br.readLine()); st=new StringTokenizer(br.readLine()); int[] arr=new int[2*n+1]; boolean[] b=new boolean[2*n+1]; boolean notpossible=false; for(int j=1;j<=n;j++) { arr[2*j-1]=Integer.parseInt(st.nextToken()); b[arr[2*j-1]]=true; } if(!b[1]) { notpossible=true; } if(!notpossible) { for(int j=1;j<=n;j++) { int r=arr[2*j-1]; r++; while(r<=2*n && b[r]) { r++; } if(r>2*n) { notpossible=true; break; } arr[2*j]=r; b[r]=true; } } if(!notpossible) { for(int j=1;j<=n;j++) { System.out.print(arr[2*j-1]+" "+arr[2*j]+" "); } System.out.println(); }else { System.out.println(-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
import java.io.OutputStream; import java.util.*; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.util.LinkedHashSet; import java.io.Writer; import java.io.OutputStreamWriter; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Egor Kulikov ([email protected]) */ public class Solution { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); Solutio solver = new Solutio(); int testCount = Integer.parseInt(in.next()); int sum=0,te=0; for (int i = 1; i <= testCount; i++) { solver.solve(i, in, out); } out.close(); } //**********************SOLVE HERE EVERYTHING****************************** //1 2 3 4 5 static class Solutio { public void solve(int testNumber, InputReader in, OutputWriter out) { int n=in.readInt(); int arr[]=new int[n]; for(int i=0;i<n;i++) arr[i]=in.readInt(); int arr2[]=new int[2*n],te=0,ch=0; for(int i=0,k=0;i<n;i++,k+=2) { arr2[k]=arr[i]; te=arr[i]+1;ch=0; while(ch==0 && te<=2*n) { ch=1; for(int j=0;j<2*i;j++) { if(arr2[j]==te) { te++;ch=0;} } for(int l=0;l<n;l++) { if(arr[l]==te) { te++;ch=0;} } } arr2[k+1]=te; if(te>2*n) { System.out.println(-1);return; } } //System.out.println(Arrays.toString(arr)+" arr"); for(int i=0;i<2*n;i++) System.out.print(arr2[i]+" "); System.out.println(); } } //*********************END HERE EVERYTHING******************************* static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public void readIntArrays(int[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) { arrays[j][i] = readInt(); } } } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.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
#!/usr/bin/env python from __future__ import division, print_function import os import sys from io import BytesIO, IOBase import datetime if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def main(): starttime=datetime.datetime.now() if(os.path.exists('input.txt')): sys.stdin = open("input.txt","r") sys.stdout = open("output.txt","w") for _ in range(int(input())): n=int(input()) b=[int(x) for x in input().split()] a=[0]*(2*n) d={} for i in b: d[i]=1 ans=0 for i in range(1,n+1): a[2*(i)-2]=b[i-1] t=0 for j in range(b[i-1]+1,2*n+1): if j not in d: a[2*(i)-1]=j d[a[2*(i)-1]]=1 t=1 break if t==0: ans=-1 break if ans==-1: print(ans) else: print(*a) endtime=datetime.datetime.now() time=(endtime-starttime).total_seconds()*1000 if(os.path.exists('input.txt')): print("Time:",time,"ms") # 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") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion 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
for _ in range(int(input())): n = int(input()) ar1 = list(map(int, input().split())) ar2 = [] kek = set() for i in range(1, 2 * n + 1): if i not in ar1: kek.add(i) ans = [] flag = 0 for i in range(n): ans.append(ar1[i]) num = 2 * n + 1 for num in range(ar1[i] + 1, 2 * n + 1): if num in kek: break if num < ar1[i] or num not in kek: flag = 1 break ans.append(num) kek.discard(num) if flag == 1: 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
a=int(input()) for i in range(a): n=int(input()) z=list(map(int,input().split())) t=[0 for i in range(3*max(z))] ans=[] for i in range(len(z)): if(t[z[i]]==0): t[z[i]]=1 for i in range(len(z)): ans.append(z[i]) for j in range(z[i]+1,len(t)): if(t[j]==0): t[j]=1 ans.append(j) break; t=sorted(ans) flag=0 for i in range(len(t)): if(t[i]!=i+1): flag=1 break; if(flag==1): print(-1) else: print(' '.join(map(str,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 os import heapq import sys,threading import math as mt import operator from copy import copy from collections import defaultdict,deque from io import BytesIO, IOBase sys.setrecursionlimit(2*10 ** 5) #threading.stack_size(2**27) def gcd(a, b): if b == 0: return a else: return gcd(b, a % b) """def pw(a,b): result=1 while(b>0): if(b%2==1): result*=a a*=a b//=2 return result""" def inpt(): return [int(k) for k in input().split()] def main(): for _ in range(int(input())): # n,k=inpt() # k=k*k # if(k>n): # print('NO') # elif((n-k)%2==0): # print('YES') # else: # print('NO') n=int(input()) ar=inpt() st=set(ar) temp=[] for i in range(1,2*n+1): if(i not in st): temp.append(i) temp.sort() ans=[0]*(2*n) j=0 ok=True for i in range(0,2*n-1,2): ans[i]=ar[i//2] st=set() for i in range(1,2*n,2): mn=10**9 for j in temp: if(j not in st and j>ans[i-1]): mn=min(mn,j) if(mn==10**9): ok=False break ans[i]=mn st.add(mn) if(not ok): print(-1) else: print(*ans) 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() #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
import java.util.*; import java.io.*; public class C623 { public static void main(String [] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t > 0) { int n = sc.nextInt(); int [] b = new int[n]; for (int i = 0; i < n; i++) b[i] = sc.nextInt(); TreeSet<Integer> set = new TreeSet<>(); for (int i = 1; i <= 2 * n; i++) { set.add(i); } for (int i = 0; i < n; i++) set.remove(b[i]); int [] a = new int[2 * n]; boolean ok = true; for (int i = 0; i < 2 * n; i += 2) { a[i] = b[i / 2]; if (set.higher(a[i]) == null) { ok = false; break; } a[i + 1] = set.higher(a[i]); set.remove(a[i]); set.remove(a[i + 1]); } if (ok) { for (int i = 0; i < 2 * n; i++) out.print(a[i] + " "); out.println(); } else { out.println(-1); } t--; } out.close(); } //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { 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
#include <bits/stdc++.h> using namespace std; long long int fun(vector<long long int> &v, long long int t, long long int l, long long int r) { if (l == r) return l; if (abs(l - r) == 1) { long long int t1 = abs(5 * v[l] - 2 * t); long long int t2 = abs(5 * v[r] * 2 * t); if (t1 < t2) return l; else return r; } long long int m = (l + r) / 2; long long int t1 = abs(5 * v[m - 1] - 2 * t); long long int t2 = abs(5 * v[m + 1] - 2 * t); if (t1 < t2) return fun(v, t, l, m); else return fun(v, t, m, r); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << setprecision(10) << fixed; long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int b[n]; map<long long int, long long int> m; for (long long int i = 0; i < n; i++) { cin >> b[i]; m[b[i]] = 1; } long long int f = 0; vector<long long int> v; for (long long int i = 0; i < n; i++) { long long int t1 = b[i] + 1; while (m[t1] != 0) t1++; if (t1 > n * 2) { f = 1; break; } else { v.push_back(b[i]); v.push_back(t1); m[t1] = 1; } } if (f == 1) 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
# ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase 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") # ------------------- fast io -------------------- import bisect #yeah i have an idea testcases=int(input()) for j in range(testcases): n=int(input()) vals=list(map(int,input().split())) setty=set(vals) unused=[] for s in range(1,2*n+1): if not(s in setty): unused.append(s) outy=[] broke=False for s in range(n): b1=vals[s] b2=unused[min(bisect.bisect_left(unused,b1),len(unused)-1)] if b1<b2: unused.pop(min(bisect.bisect_left(unused,b1),len(unused)-1)) outy.append(b1) outy.append(b2) else: broke=True if broke==True: print(-1) else: print(*outy)
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()) arr = list(map(int, input().split())) arr2 = [] for i in arr: arr2.append(i) t = i+1 while t in arr or t in arr2: t += 1 arr2.append(t) if max(arr2) == 2 * n: print(*arr2) 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 bisect import bisect_right for _ in range(int(input())): n = int(input()) b = list(map(int, input().split())) try: a = [0] * 2 * n a[::2] = b.copy() m = sorted(set(range(1, 2 * n + 1)).difference(set(b))) + [float('inf')] for i in range(1, 2 * n, 2): a[i] = m.pop(bisect_right(m, a[i-1])) if list(range(1, 2 * n + 1)) != sorted(a): print(-1) else: print(*a) except: 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
#!/usr/bin/env python def gen(b): a = list(range(1, 2 * n + 1)) a = [x for x in a if x not in b] seq = [] for b_ in b: try: idx = next(x for x, v in enumerate(a) if v > b_) seq += [b_, a[idx]] del a[idx] except: return None return seq t = int(input()) for _ in range(t): n = int(input()) b = list(map(int, input().split())) ans = gen(b) if ans: print(*ans) 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
# Код ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΡ‹ написал Π½Π° языкС Python 3 import sys import bisect def main(): n = int(sys.stdin.readline()) q = [int(i) for i in sys.stdin.readline().split()] w = [] for i in range(1, 2 * n + 1): if i not in q: w.append(i) w.sort() res = [0 for i in range(2*n)] for i in range(n): res[i * 2] = q[i] for i in range(n - 1): e = bisect.bisect(w, q[i]) if e != n - i: r = w[e] del w[e] res[i * 2 + 1] = r else: print(-1) return 0 if w[-1] < q[-1]: print(-1) return 0 else: res[-1] = w[-1] print(*res) for test in range(int(sys.stdin.readline())): 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
""" // Author : snape_here - Susanta Mukherjee """ from __future__ import division, print_function import os,sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip def ii(): return int(input()) def si(): return input() def mi(): return map(int,input().split()) def li(): return list(mi()) def gcd(x, y): while y: x, y = y, x % y return x def read(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') import math mod=10**9 mod+=7 def main(): for i in range(ii()): n=ii() b=li() a=[0]*205 h=[0]*205 c=[0]*205 for i in range(n): h[b[i]]=1 pre=1 f=0 for i in range(n): d=1e7 for j in range(n): if a[j]: continue e=b[j] if e<d: d=e pos=j a[pos]=1 while(h[pre]): pre+=1 h[pre]=1 c[pos]=pre i=0 while(i<n and f==0): if min(b[i],c[i])!=b[i]: f=1 i+=1 if f: print(-1) else: for i in range(n): for j in range(i+1,n): if min(b[i],c[j])==b[i] and c[i]>=c[j]: c[i],c[j]=c[j],c[i] l=[] for i in range(n): l.append(b[i]) l.append(c[i]) print(*l) # 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") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": #read() main() #Comment read()
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 _ in range(int(input())): n = int(input()) s = [*map(int,input().split())] ans = [] for i in s: ans.append(i) t = i+1 while t in s or t in ans: t += 1 ans.append(t) if sorted(ans) == [i for i in range(1,2*n + 1)]: print(*ans) 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
#!/usr/bin/env python import os import sys from io import BytesIO, IOBase import threading from collections import Counter mod=998244353 def main(): for _ in range(int(input())): n=int(input()) arr=list(map(int,input().split())) a=list(zip(arr,range(1,n+1))) # a.sort() a.reverse() taken=[0]*(2*n+1) ans=[-1]*(2*n+1) for i in range(n): taken[arr[i]]=1 b=True while len(a): ele,ind=a.pop() el2=-1 for i in range(ele+1,2*n+1): if taken[i]==0: el2=i break if el2==-1: # print(-1) b=False break else: taken[el2]=1 ans[2*ind-1]=ele ans[2*ind]=el2 if b: print(*(ans[1:])) else: print(-1) 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") # endregion 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
#Pye from os import path from sys import stdin, stdout from bisect import bisect_left, bisect_right maxn = 200005 dd = [0 for i in range(maxn)] a = [0 for i in range(maxn)] if path.exists('inp.txt'): stdin = open("inp.txt", "r") q = int(stdin.readline()) for _ in range(q): s = []; check = 0; n = int(stdin.readline()) res = [0 for i in range(2*n+1)] for i in range(1, 2*n+1): dd[i] = 0 a[1:] = list(map(int, stdin.readline().split())) for i in a: dd[i] += 1 for i in range(1, 2*n+1): if not dd[i]: s.append(i) for i in range(1, n+1): if bisect_left(s, a[i]) != len(s): res[2*i-1] = a[i] res[2*i] = s[bisect_left(s, a[i])] s.pop(bisect_left(s, a[i])) else: check = 1; break if not check: print(*res[1:]) 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 java.io.*; import java.math.BigInteger; import java.util.*; import java.lang.*; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; public class Contest { //static ArrayList<Integer> prime=new ArrayList(); public static TreeMap<Long,Integer> t1=new TreeMap(); public static void solve() { long m=1; for(int j=2;j<=1000;j++) { if(isprime(j)) { m=m*j; if(m>1000000) { break; }else{ t1.put(m,1); } } } } public static long gcd(long n1, long n2) { if (n2 != 0) return gcd(n2, n1 % n2); else return n1; } ////////////////// public static class trie { int x,y,z; trie (int x,int y,int z){ this.x=x; this.y=y; this.z=z; } } public static class pair { int x,y; pair (int x,int y){ this.x=x; this.y=y; } public boolean equals(Object o) { if(o instanceof pair) { pair p = (pair)o; return (x == p.x && y == p.y); } return false; } public int hashCode() { return (Integer.hashCode(x) * 3 + Integer.hashCode(y) ); } } ////////////////// static class Cmp implements Comparator<pair> { public int compare(pair a,pair b) { if(a.x==b.x) { return(a.y-b.y); } return(a.x-b.x); } } ////////////////// ////////////////// 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; } } ////////////////////// //public static ArrayList<Integer> a=new ArrayList(); public static ArrayList<Integer> Divisors(int n) { // Note that this loop runs till square root ArrayList<Integer> a=new ArrayList(); long sum=0; for (int i=1; i*i<=(n); i++) { if (n%i == 0) { // If divisors are equal, print only one if (n/i == i) { // printf("%d ", i); sum=sum+1; a.add(i); } else // Otherwise print both { //printf("%d %d ", i, n/i); sum=sum+2; a.add(i); a.add(n/i); } } } return(a); } ///////////////////// public static long SmallestPrimeDivisor(long n) { // if divisible by 2 if (n % 2 == 0) return 2; // iterate from 3 to sqrt(n) for (long i = 3; i * i <= n; i += 2) { if (n % i == 0) return i; } return n; } //////////////////// public static boolean isprime(long n) { int count=0; for(long i=2;i*i<=n;i++) { if(n%i==0) { return(false); } } return(true); } ////////////////////// // (x^n)%1000000007 public static long modularExponentiation(long x,long n,long M) { if(n==0) return 1; else if(n%2 == 0) //n is even return modularExponentiation((x*x)%M,n/2,M); else //n is odd return (x*modularExponentiation((x*x)%M,(n-1)/2,M))%M; } public static long nCr(int n,int k) { if(k==0) return 1; return(n*nCr(n-1,k-1)/k); } public static long factorial(int n) { if(n==1) return 1; return((n)*factorial(n-1)); } public static void debug(Object...o) { System.out.println( Arrays.deepToString(o) ); } public static boolean prime[]=new boolean[100005]; //prime[j]==false means it's a prime. public static void seive() { prime[0]=false; prime[1]=false; for(int i=2;i*i<=100005;i++) { if(!prime[i]) { for(int j=i*i;i<=100005;j+=i) { prime[j]=true; } } } } public static long AtMost(int k,char c[]) { long ans=0; int left=0,right=0; int n=c.length; HashMap<Character,Integer> h=new HashMap(); h.put('1',0); while(right<n) { h.put(c[right],h.getOrDefault(c[right],0)+1); while(h.get('1')>k) { h.replace(c[left],h.get(c[left])-1); left++; } ans=ans+(right-left+1); right++; } return ans; } public static boolean equals(StringBuilder sb1, StringBuilder sb2){ /* * If both refer to the same object, they are equal */ if(sb1 == sb2) return true; /* * If any of them is null, then they are not equal. * We know both are not null at this point because of the previous * condition which checks equality. */ if(sb1 == null || sb2 == null) return false; boolean isEqual = true; /* * If both are not same length, they are not equal */ if(sb1.length() == sb2.length()){ /* * Now, compare the characters one by one, and break the loop * as soon as the characters at given position are different. */ for(int i = 0 ; i < sb1.length(); i++){ if(sb1.charAt(i) != sb2.charAt(i)){ isEqual = false; break; } } }else{ isEqual = false; } return isEqual; } public static void main(String []args) throws Exception { FastReader in =new FastReader(); PrintWriter pw = new PrintWriter(System.out); //Scanner sc=new Scanner(System.in) // int x=in.nextInt(); //String a[]=in.nextLine().split(" "); //String b=in.nextLine(); // StringBuilder s1=new StringBuilder(in.nextLine()); // StringBuilder s2=new StringBuilder(in.nextLine()); int t=in.nextInt(); for(int i=0;i<t;i++) { int n=in.nextInt(); int b[]=new int[n]; int a[]=new int[2*n]; TreeMap<Integer,Integer> h=new TreeMap(); for(int j=1;j<=2*n;j++) { h.put(j,1); } for(int j=0;j<n;j++) { b[j]=in.nextInt(); a[2*(j+1)-2]=b[j]; h.remove(b[j]); } int m=0; for(int j=0;j<n;j++) { Integer x=h.higherKey(b[j]); if(x==null) { m=1; } else { a[2*(j+1)-1]=(int)x; h.remove(x); } } if(m==1) { pw.print(-1); } else { for(int j=0;j<2*n;j++) { pw.print(a[j]+" "); } } pw.println(""); } pw.flush(); pw.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
import java.util.*; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int r = 0; r < t; r++) { int n = in.nextInt(); int[] b = new int[n]; int[] a = new int[2 * n]; TreeSet<Integer> set = new TreeSet<>(); for (int i = 1; i <= 2 * n; i++) { set.add(i); } for (int i = 0; i < n; i++) { int x = in.nextInt(); b[i] = x; a[(2 * i)] = x; set.remove(x); } boolean noE = true; for (int i = 0; i < n; i++) { try { int x = set.higher(b[i]); set.remove(x); a[i * 2 + 1] = x; } catch (Exception e) { noE = false; System.out.println(-1); break; } } if (noE) { 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.*; import java.util.Arrays; import java.util.BitSet; import java.util.Comparator; import java.util.Scanner; import static java.lang.Math.*; public class C { private static class Pair<K, V> { K k; V v; public Pair(K k, V v) { this.k = k; this.v = v; } } public static void main(String[] args) { Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in))); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int t = in.nextInt(); for (int z = 0; z < t; z++) { int n = in.nextInt(); Pair<Pair<Integer, Integer>, Pair<Integer, Integer>>[] arr = new Pair[n]; BitSet used = new BitSet(); for (int i = 0; i < n; i++) { int a = in.nextInt(); used.set(a, true); arr[i] = new Pair<>(new Pair<>(a, i), new Pair<>(a, a)); } //Arrays.sort(arr, Comparator.comparingInt((o) -> o.k.k)); boolean bad = false; for (int i = 0; i < n; i++) { Pair<Pair<Integer, Integer>, Pair<Integer, Integer>> p = arr[i]; int f = used.nextClearBit(p.k.k); if (f > n * 2) { out.println("-1"); bad = true; break; } p.v.k = p.k.k; p.v.v = f; used.set(f); } if (bad) continue; //Arrays.sort(arr, Comparator.comparingInt((o) -> o.k.v)); for (int i = 0; i < n; i++) { Pair<Pair<Integer, Integer>, Pair<Integer, Integer>> p = arr[i]; int r = p.k.k; } for (int i = 0; i < n; i++) out.print(arr[i].v.k + " " + arr[i].v.v + " "); out.println(); } 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
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int,input().split())) b = sorted(a) used = [0 for i in range(2*n+1)] flg = 1 for i in range(n): if b[i] > i*2+1: print(-1) flg = 0 break used[b[i]] = 1 if flg == 0: continue ans = [] for i in range(n): ans.append(a[i]) for j in range(a[i],2*n+1): if used[j] == 0: ans.append(j) used[j] = 1 break 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
t=int(input()) for _ in range(t): n=int(input()) l=list(map(int,input().split())) r=[] for i in range(1,2*n+1): if i in l: pass else: r.append(i) i=0 k=[] while len(r)>0 or i<n: for j in range(len(r)): if l[i]<r[j]: k.append(r[j]) break r.remove(r[j]) i=i+1 if len(k)==n: for i in range(n): print(l[i],k[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
for namber in range(int(input())): tmp = input() data = list(map(int, input().split())) sortdat = sorted(data) if sortdat[0] == 1: maxlen = 2 for i in range(1, len(data)): if sortdat[i] - sortdat[i - 1] > maxlen: print(-1) break elif sortdat[i] - sortdat[i - 1] == maxlen: maxlen = 2 else: maxlen += 2 - sortdat[i] + sortdat[i - 1] else: out = [False] * (len(data) * 2) for j in data: out[j - 1] = True prn = '' for j in data: prn += str(j) + ' ' tmp = j while out[tmp]: tmp += 1 prn += str(tmp + 1) + ' ' out[tmp] = True print(prn) 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 java.util.*; import static java.lang.System.*; public class Main { public static void main(String[] args) { Scanner s = new Scanner(in); int t = s.nextInt(); s.nextLine(); while (t-- > 0) { int n = s.nextInt(); int[] a = new int[n]; boolean[] taken = new boolean[n * 2 + 1]; boolean tooLarge = false; int largest = 0; for (int i = 0; i < n; i++) { int temp = s.nextInt(); if (temp >= n * 2) { tooLarge = true; break; } a[i] = temp; taken[temp] = true; if (temp > largest) largest = temp; } s.nextLine(); if (tooLarge) { out.println(-1); continue; } boolean fail = false; int usableNums = 0; for (int i = 1; i < largest; i++) { if (!taken[i]) usableNums--; else usableNums++; if (usableNums < 0) { fail = true; break; } } if (fail) { out.println(-1); continue; } for (int i = 0; i < n; i++) { out.print(a[i] + " "); int temp = a[i] + 1; while (taken[temp]) temp++; taken[temp] = true; out.print(temp + " "); } 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
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int b[100 + 5], a[200 + 10]; bool used[200 + 10]; int main() { ios::sync_with_stdio(false); cin.tie(0); int t, n; cin >> t; while (t--) { cin >> n; memset(a, -1, sizeof a); memset(used, false, sizeof used); memset(b, -1, sizeof b); bool flag = false; for (int i = 0; i < n; i++) { cin >> b[i]; } for (int i = 0; i < 2 * n; i += 2) { a[i] = b[i / 2]; used[b[i / 2]] = true; } for (int i = 1; i < 2 * n; i += 2) { flag = false; for (int j = a[i - 1] + 1; j <= 2 * n; j++) { if (!used[j]) { a[i] = j; used[j] = true; flag = true; break; } } if (!flag) { cout << -1 << endl; break; } } if (flag) { for (int i = 0; i < 2 * n; i++) { cout << a[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 javafx.util.Pair; import org.omg.CORBA.INTERNAL; import sun.awt.image.ImageWatched; import sun.reflect.generics.tree.Tree; import java.nio.channels.ScatteringByteChannel; import java.text.SimpleDateFormat; import java.util.*; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int tc = s.nextInt(); tc:for (int t = 0; t < tc; t++) { int n = s.nextInt(); int boundary = 2*n; boolean[] flag = new boolean[boundary + 1]; int[] answer = new int[2*n]; int seq[] = new int[n]; for (int i = 0; i < n; i++) { seq[i] = s.nextInt(); flag[seq[i]] = true; } local:for (int i = 0; i < n; i++) { int num = seq[i]; if (num >= boundary) { System.out.println(-1); continue tc; } answer[2*i] = num; for (int j = num+1; j < 2*n+1; j++) { if (flag[j] == false) { flag[j] = true; answer[2*i+1] = j; continue local; } } System.out.println(-1); continue tc; } for (int k : answer) { System.out.print(k + " "); } System.out.print("\n"); } } }
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 _ in range(t): n = int(input()) arr = list(map(int,input().split())) d = {i:0 for i in range(1,n*2+1)} for i in arr: d[i] = 1 resultset = [] gflag = True for i in arr: flag = False # print(i,d) for j in range(i+1,n*2+1): if d[j] == 0: resultset.append(i) resultset.append(j) d[j] = 1 flag = True break if not flag: gflag = False break print(' '.join(str(i) for i in resultset) if gflag else -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 = [int(__) for __ in input().split()] shit = list(range(1, 2 * n + 1)) ans = [] flag = 1 for i in b: shit.remove(i) for i in b: for j in shit: if j > i: ans.append(i) ans.append(j) shit.remove(j) flag = 1 break else: flag = 0 if flag == 0: break if flag == 0: print(-1) else: for i in ans: 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
import java.util.Scanner; public class Perms { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); int tCount= scanner.nextInt(); int [][]cases= new int[tCount][]; for(int i=0; i<tCount;i++){ int itemsCount= scanner.nextInt(); int[]arr= new int[itemsCount]; for(int j=0;j<itemsCount;j++){ arr[j]=scanner.nextInt(); } cases[i]=arr; } for (int i=0; i<cases.length;i++){ printArr(perms(cases[i])); } } static void printArr(int[]arr){ for (int i=0; i<arr.length;i++){ System.out.print(arr[i]+" "); } System.out.println(); } static int[] perms(int []a){ int len= a.length; int[] noResult={-1}; int[] result= new int [2*len]; if(max(a,len)>2*len||find(a,len,2*len)){ return noResult; } for(int i=0;i<len;i++){ result[2*i]=a[i]; } for(int i=0;i<len;i++){ int ind=2*i+1; for(int j=1;j<=2*len;j++){ if((!find(result,2*len,j))&&j>result[ind-1]){ result[ind]=j; break; } } if(result[ind]==0){ return noResult; } } return result; } static int max(int[]arr, int len){ int m= arr[0]; for(int i=1;i<len;i++){ if(arr[i]>m){ m= arr[i]; } } return m; } static boolean find(int[]arr, int len, int x){ for(int i=0;i<len;i++){ if(arr[i]==x){ return true; } } return false; } }
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=input() from collections import Counter,defaultdict for i in range(t): n=input() l=list(map(int,raw_input().split())) l1=[] c=Counter(l) d=defaultdict(int) ans=True for j in l: for k in range(j+1,201): if c[k]<>1 and d[k]<>1: l1.append(j) l1.append(k) d[j]=1 d[k]=1 break for j in range(1,max(l1)+1): if d[j]==0: ans=False break if ans==True: s='' for j in l1: s=s+str(j)+' ' print s.rstrip(' ') else: print -1
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
t = int(input()) for _ in range(t): n = int(input()) b = list(map(int, input().split())) if max(b) >= n*2 or 1 not in b: print(-1) else: a = [0]*(2*n) ok = True for i in range(n): a[i*2] = b[i] while 0 in a: moved = False for i in range(1, 1+2*n): ind = a.index(0) if i not in a and i > a[ind-1]: a[ind] = i moved = True break if not moved: ok = False break if not ok: 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
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.InputMismatchException; import java.util.TreeSet; public class Main { private static final String NO = "NO"; private static final String YES = "YES"; InputStream is; PrintWriter out; String INPUT = ""; private static final long MOD = 1000000007L; private static final long MAX = Long.MAX_VALUE / 2; void solve() { int T = ni(); outer: while (T-- > 0) { int N = ni(); int[][] a = new int[N][]; TreeSet<Integer> s = new TreeSet<Integer>(); for (int i = 1; i <= 2 * N; i++) s.add(i); for (int i = 0; i < N; i++) { a[i] = new int[] { i, ni(), 0 }; s.remove(a[i][1]); } for (int i = 0; i < N; i++) { Integer n = s.ceiling(a[i][1]); if (n == null) { out.println(-1); continue outer; } s.remove(n); a[i][2] = n; } StringBuffer sb = new StringBuffer(); for (int e[] : a) { sb.append(e[1]).append(' ').append(e[2]).append(' '); } out.println(sb); } } long power(long a, long b) { long x = 1, y = a; while (b > 0) { if (b % 2 != 0) { x = (x * y) % MOD; } y = (y * y) % MOD; b /= 2; } return x % MOD; } private long gcd(long a, long b) { while (a != 0) { long tmp = b % a; b = a; a = tmp; } return b; } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private boolean vis[]; private 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++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' // ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n) { if (!(isSpaceChar(b))) buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private Integer[] na2(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int[][] na(int n, int m) { int[][] a = new int[n][]; for (int i = 0; i < n; i++) a[i] = na(m); return a; } private Integer[][] na2(int n, int m) { Integer[][] a = new Integer[n][]; for (int i = 0; i < n; i++) a[i] = na2(m); return a; } private int ni() { 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(); } } private long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private long nl() { 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(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
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 _ in range(t): n = int(input()) b = list(map(int, input().split())) a = [0] * 1111 for c in b: a[c] = 1 res = [] for x in b: flag = False for j in range(x+1, 2*n+1): if a[j] == 0: flag = True a[j] = 2 res.append(x) res.append(j) break if not flag: res = [-1] break print(' '.join([str(c) for c in 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
t = int(input()) for _ in range(t): n = int(input()) b = list(map(int, input().split())) c = [0]*(2*n) d = [] for i in range(0, 2*n, 2): c[i] = b[i//2] for j in range(1, (2*n)+1): if (j not in c): d.append(j) for i in range(n): for j in range(0, 2*n, 2): if (c[j+1] == 0): if (d[i] > c[j]): c[j+1] = d[i] break if (0 in c): print("-1") else: print(*c)
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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.TreeSet; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); CRestoringPermutation solver = new CRestoringPermutation(); solver.solve(1, in, out); out.close(); } static class CRestoringPermutation { public void solve(int testNumber, FastReader sc, PrintWriter out) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] b = new int[n]; for (int i = 0; i < n; ++i) b[i] = sc.nextInt(); n = 2 * n; int[] a = new int[n]; TreeSet<Integer> tree = new TreeSet<>(); for (int i = 1; i <= n; ++i) tree.add(i); int ptr = 0; for (int i = 0; i < n; i += 2) { a[i] = b[ptr++]; tree.remove(a[i]); } boolean yes = false; for (int i = 1; i < n && !yes; i += 2) { int x = a[i - 1]; if (tree.higher(x) == null) yes = true; else { a[i] = tree.higher(x); tree.remove(a[i]); } } if (yes) out.print(-1); else for (int i = 0; i < a.length; ++i) out.print(a[i] + " "); out.println(); } } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader(InputStream stream) { br = new BufferedReader(new InputStreamReader(stream), 32768); } public String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(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
if __name__ == '__main__': for _ in range (int(input())): n = int(input()) l = list(map(int, input().split())) a = min(l) b = max(l) if a != 1 or b == 2*n: print(-1) else: B = [] d = dict() for i in range (n): d.setdefault(l[i],1) for i in range (1,(2*n)+1): d.setdefault(i,0) if d[i] == 0: B.append(i) B.sort() ans = [] i = 0 while i < n: ans.append(l[i]) for j in range (len(B)): if B[j] > l[i]: ans.append(B[j]) B.pop(j) break i+=1 if len(ans) < 2*n: print(-1) continue 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
/** * ******* Created on 23/2/20 10:07 PM******* */ import java.io.*; import java.util.*; public class C1315 implements Runnable { private static final int MAX = (int) (1E5 + 5); private static final int MOD = (int) (1E9 + 7); private static final long Inf = (long) (1E14 + 10); private void solve() throws IOException { int t = reader.nextInt(); int[] b = new int[105]; int[] a = new int[210]; List<Integer>list = new ArrayList<>(); Set<Integer>set = new HashSet<>(); while(t-->0){ set.clear(); int n = reader.nextInt(); for(int i=1;i<=n;i++) { b[i] = reader.nextInt(); set.add(b[i]); } boolean flag = true; for(int i=1;i<=n; i++){ int val = 2*n+1; for(int j=b[i]+1;j<=2*n;j++){ if(!set.contains(j)){ val =j;break; } } if(val > 2*n) flag =false; a[2*i-1]=b[i]; a[2*i]= val; set.add(val); } set.clear(); for(int i=1;i<=2*n;i++){ if(set.contains(a[i])) flag =false; set.add(a[i]); } if(!flag) writer.print("-1"); else for(int i=1;i<=2*n;i++) writer.print(a[i]+" "); writer.println(); } } public static void main(String[] args) throws IOException { try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) { new C1315().run(); } } StandardInput reader; PrintWriter writer; @Override public void run() { try { reader = new StandardInput(); writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } interface Input extends Closeable { String next() throws IOException; default int nextInt() throws IOException { return Integer.parseInt(next()); } default long nextLong() throws IOException { return Long.parseLong(next()); } default double nextDouble() throws IOException { return Double.parseDouble(next()); } default int[] readIntArray() throws IOException { return readIntArray(nextInt()); } default int[] readIntArray(int size) throws IOException { int[] array = new int[size]; for (int i = 0; i < array.length; i++) { array[i] = nextInt(); } return array; } default long[] readLongArray(int size) throws IOException { long[] array = new long[size]; for (int i = 0; i < array.length; i++) { array[i] = nextLong(); } return array; } } private static class StandardInput implements Input { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer stringTokenizer; @Override public void close() throws IOException { reader.close(); } @Override public String next() throws IOException { if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.nextToken(); } } }
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
for _ in range(int(input())): n = int(input()) b = list(map(int, input().split())) ans = [] for i in b: ans.append(i) t = i+1 while(t in b) or (t in ans): t += 1 ans.append(t) if(max(ans) == 2*n): print(*ans) 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 import collections input = sys.stdin.readline def main(): t = int(input()) for _ in range(t): N = int(input()) B = [int(x) for x in input().split()] c = collections.Counter() ans = [] for b in B: if c[b] > 0: print(-1) break ans.append(b) c[b] += 1 i = 1 while c[b + i] != 0 or b + i in B: i += 1 ans.append(b + i) c[b + i] += 1 else: for i in range(1, 2 * N + 1): if c[i] == 0: print(-1) break else: print(*ans) 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.*; import java.io.*; public class Main{ static int repow(int b,int p){ long a = b; long res=1; while(p>0){ if(p%2==1){ res*=a; } a*=a; p/=2; } return (int)res; } static int repow(int b,int p,int modder){ long a = b%modder; long res=1; while(p>0){ if(p%2==1){ res=(res*a)%modder; } a=(a*a)%modder; p/=2; } return (int)res; } static long repow(long b,long p){ long a = b; long res=1; while(p>0){ if(p%2==1){ res*=a; } a*=a; p/=2; } return res; } static long repow(long b,long p,long modder){ long a = b%modder; long res=1; while(p>0){ if(p%2==1){ res=(res*a)%modder; } a=(a*a)%modder; p/=2; } return res; } static long gcd(long c,long d){ while(true){ long f=c%d; if(f==0){ return d; } c=d; d=f; } } static long lcm(long c,long d){ return c/gcd(c,d)*d; } static ArrayList<Integer> divList(int n){ ArrayList<Integer> div=new ArrayList<Integer>(); for(int i=1;i*i<=n;i++){ if(n%i==0){ div.add(i); if(i*i!=n){ div.add((int)(n/i)); } } } return div; } static HashSet<Integer> divSet(int n){ HashSet<Integer> div=new HashSet<Integer>(); for(int i=1;i*i<=n;i++){ if(n%i==0){ div.add(i); div.add((int)(n/i)); } } return div; } static Map<Long,Integer> pFacMap(long x){ Map<Long,Integer> mp=new HashMap<>(); long t=x; for(long i=2;i*i<=x&&i<=t;i++){ if(t%i==0){ int num=0; while(t%i==0){ t/=i; num++; } mp.put(i,num); } } if(t!=1){ mp.put(t,1); } return mp; } static boolean iP(long n){ if(n==2){ return true; } if((n&1)==0||n==1){ return false; } // if(n>3037007383L){ if(n>Integer.MAX_VALUE){ return tameshiwari(n); } long d=n-1; int s=0; while((d&1)==0){ d/=2; s++; } int[] aa = {2,3,5,7,11,13,17}; for(int i=0;i<7&&aa[i]<n;i++){ int a = aa[i]; long t = d; long y=repow(a,t,n); while(t!=n-1&&y!=1&&y!=n-1){ y = (y*y)%n; t=t<<1; } if(y!=n-1&&(t&1)==0){ return false; } } return true; } static boolean tameshiwari(long n){ for(long i=2;i*i<=n;i++){ if(n%i==0){ return false; } } return true; } static class NCK{ int max; int mod; long[] fac; long[] finv; long[] inv; NCK(){ this(510000,1000000007); } NCK(int max,int mod){ this.max=max; this.mod=mod; pre(max,mod); } void pre(int nmax,int nmod){ fac = new long[nmax]; finv = new long[nmax]; inv = new long[nmax]; fac[0]=fac[1]=1; finv[0]=finv[1]=1; inv[1]=1; for(int i=2;i<nmax;i++){ fac[i]=fac[i-1]*i%nmod; inv[i]=nmod-inv[nmod%i]*(nmod/i)%nmod; finv[i]=finv[i-1]*inv[i]%nmod; } } long nCk(int n,int k){ if(n<k){return 0;} if(n<0||k<0){return 0;} return fac[n]*(finv[k]*finv[n-k]%mod)%mod; } } static <T extends Comparable<T>> int BSA(List<T> l,T target){ return BSA(l,target,0,l.size(),(s1,s2)->s1.compareTo(s2)); } static <T> int BSA(List<T> l,T target,Comparator<T> c){ return BSA(l,target,0,l.size(),c); } static <T extends Comparable<T>> int BSA(List<T> l,T target,int left,int right){ return BSA(l,target,left,right,(s1,s2)->s1.compareTo(s2)); } static <T> int BSA(List<T> l,T target,int left,int right,Comparator<T> c){ int lt = left; int rt = right-1; while(lt<=rt){ T gaze=l.get(lt+(rt-lt)/2); if(c.compare(gaze,target)<0){ lt=lt+(rt-lt)/2+1; }else if(c.compare(gaze,target)>0){ rt=lt+(rt-lt)/2-1; }else{ return lt+(rt-lt)/2; } } return -1; } static <T extends Comparable<T>> int BSF(List<T> l,T target){ return BSF(l,target,0,l.size(),(s1,s2)->s1.compareTo(s2)); } static <T> int BSF(List<T> l,T target,Comparator<T> c){ return BSF(l,target,0,l.size(),c); } static <T extends Comparable<T>> int BSF(List<T> l,T target,int left,int right){ return BSF(l,target,left,right,(s1,s2)->s1.compareTo(s2)); } static <T> int BSF(List<T> l,T target,int left,int right,Comparator<T> c){ int lt = left; int rt = right-1; while(lt<=rt){ T gaze=l.get(lt+(rt-lt)/2); if(c.compare(gaze,target)<0){ lt=lt+(rt-lt)/2+1; }else if(c.compare(gaze,target)>=0){ rt=lt+(rt-lt)/2-1; } } return lt; } static <T extends Comparable<T>> int BSL(List<T> l,T target){ return BSL(l,target,0,l.size(),(s1,s2)->s1.compareTo(s2)); } static <T> int BSL(List<T> l,T target,Comparator<T> c){ return BSL(l,target,0,l.size(),c); } static <T extends Comparable<T>> int BSL(List<T> l,T target,int left,int right){ return BSL(l,target,left,right,(s1,s2)->s1.compareTo(s2)); } static <T> int BSL(List<T> l,T target,int left,int right,Comparator<T> c){ int lt = left; int rt = right-1; while(lt<=rt){ T gaze=l.get(lt+(rt-lt)/2); if(c.compare(gaze,target)<=0){ lt=lt+(rt-lt)/2+1; }else if(c.compare(gaze,target)>0){ rt=lt+(rt-lt)/2-1; } } return lt; } static <T extends Comparable<T>> int BSA(T[] a,T target){ return BSA(a,target,0,a.length,(s1,s2)->s1.compareTo(s2)); } static <T> int BSA(T[] a,T target,Comparator<T> c){ return BSA(a,target,0,a.length,c); } static <T extends Comparable<T>> int BSA(T[] a,T target,int left,int right){ return BSA(a,target,left,right,(s1,s2)->s1.compareTo(s2)); } static <T> int BSA(T[] a,T target,int left,int right,Comparator<T> c){ int lt = left; int rt = right-1; while(lt<=rt){ T gaze=a[lt+(rt-lt)/2]; if(c.compare(gaze,target)<0){ lt=lt+(rt-lt)/2+1; }else if(c.compare(gaze,target)>0){ rt=lt+(rt-lt)/2-1; }else{ return lt+(rt-lt)/2; } } return -1; } static <T extends Comparable<T>> int BSF(T[] a,T target){ return BSF(a,target,0,a.length,(s1,s2)->s1.compareTo(s2)); } static <T> int BSF(T[] a,T target,Comparator<T> c){ return BSF(a,target,0,a.length,c); } static <T extends Comparable<T>> int BSF(T[] a,T target,int left,int right){ return BSF(a,target,left,right,(s1,s2)->s1.compareTo(s2)); } static <T> int BSF(T[] a,T target,int left,int right,Comparator<T> c){ int lt = left; int rt = right-1; while(lt<=rt){ T gaze=a[lt+(rt-lt)/2]; if(c.compare(gaze,target)<0){ lt=lt+(rt-lt)/2+1; }else if(c.compare(gaze,target)>=0){ rt=lt+(rt-lt)/2-1; } } return lt; } static <T extends Comparable<T>> int BSL(T[] a,T target){ return BSL(a,target,0,a.length,(s1,s2)->s1.compareTo(s2)); } static <T> int BSL(T[] a,T target,Comparator<T> c){ return BSL(a,target,0,a.length,c); } static <T extends Comparable<T>> int BSL(T[] a,T target,int left,int right){ return BSL(a,target,left,right,(s1,s2)->s1.compareTo(s2)); } static <T> int BSL(T[] a,T target,int left,int right,Comparator<T> c){ int lt = left; int rt = right-1; while(lt<=rt){ T gaze=a[lt+(rt-lt)/2]; if(c.compare(gaze,target)<=0){ lt=lt+(rt-lt)/2+1; }else if(c.compare(gaze,target)>0){ rt=lt+(rt-lt)/2-1; } } return lt; } static int BSA(int[] a,int target){ return BSA(a,target,0,a.length,true); } static int BSA(int[] a,int target, boolean syojun){ return BSA(a,target,0,a.length,syojun); } static int BSA(int[] a,int target,int left,int right){ return BSA(a,target,left,right,true); } static int BSA(int[] a,int target,int left,int right,boolean syojun){ int lt = left; int rt = right-1; while(lt<=rt){ int gaze=a[lt+(rt-lt)/2]; if((syojun&&gaze<target)||(!syojun&&gaze>target)){ lt=lt+(rt-lt)/2+1; }else if((syojun&&gaze>target)||(!syojun&&gaze<target)){ rt=lt+(rt-lt)/2-1; }else{ return lt+(rt-lt)/2; } } return -1; } static int BSF(int[] a,int target){ return BSF(a,target,0,a.length,true); } static int BSF(int[] a,int target,boolean syojun){ return BSF(a,target,0,a.length,syojun); } static int BSF(int[] a,int target,int left,int right){ return BSF(a,target,left,right,true); } static int BSF(int[] a,int target,int left,int right,boolean syojun){ int lt = left; int rt = right-1; while(lt<=rt){ int gaze=a[lt+(rt-lt)/2]; if((syojun&&gaze<target)||(!syojun&&gaze>target)){ lt=lt+(rt-lt)/2+1; }else if((syojun&&gaze<=target)||(!syojun&&gaze>=target)){ rt=lt+(rt-lt)/2-1; } } return lt; } static int BSL(int[] a,int target){ return BSL(a,target,0,a.length,true); } static int BSL(int[] a,int target,boolean syojun){ return BSL(a,target,0,a.length,syojun); } static int BSL(int[] a,int target,int left,int right){ return BSL(a,target,left,right,true); } static int BSL(int[] a,int target,int left,int right,boolean syojun){ int lt = left; int rt = right-1; while(lt<=rt){ int gaze=a[lt+(rt-lt)/2]; if((syojun&&gaze<=target)||(!syojun&&gaze>=target)){ lt=lt+(rt-lt)/2+1; }else if((syojun&&gaze>target)||(!syojun&&gaze<target)){ rt=lt+(rt-lt)/2-1; } } return lt; } static int BSA(long[] a,long target){ return BSA(a,target,0,a.length,true); } static int BSA(long[] a,long target, boolean syojun){ return BSA(a,target,0,a.length,syojun); } static int BSA(long[] a,long target,int left,int right){ return BSA(a,target,left,right,true); } static int BSA(long[] a,long target,int left,int right,boolean syojun){ int lt = left; int rt = right-1; while(lt<=rt){ long gaze=a[lt+(rt-lt)/2]; if((syojun&&gaze<target)||(!syojun&&gaze>target)){ lt=lt+(rt-lt)/2+1; }else if((syojun&&gaze>target)||(!syojun&&gaze<target)){ rt=lt+(rt-lt)/2-1; }else{ return lt+(rt-lt)/2; } } return -1; } static int BSF(long[] a,long target){ return BSF(a,target,0,a.length,true); } static int BSF(long[] a,long target,boolean syojun){ return BSF(a,target,0,a.length,syojun); } static int BSF(long[] a,long target,int left,int right){ return BSF(a,target,left,right,true); } static int BSF(long[] a,long target,int left,int right,boolean syojun){ int lt = left; int rt = right-1; while(lt<=rt){ long gaze=a[lt+(rt-lt)/2]; if((syojun&&gaze<target)||(!syojun&&gaze>target)){ lt=lt+(rt-lt)/2+1; }else if((syojun&&gaze<=target)||(!syojun&&gaze>=target)){ rt=lt+(rt-lt)/2-1; } } return lt; } static int BSL(long[] a,long target){ return BSL(a,target,0,a.length,true); } static int BSL(long[] a,long target,boolean syojun){ return BSL(a,target,0,a.length,syojun); } static int BSL(long[] a,long target,int left,int right){ return BSL(a,target,left,right,true); } static int BSL(long[] a,long target,int left,int right,boolean syojun){ int lt = left; int rt = right-1; while(lt<=rt){ long gaze=a[lt+(rt-lt)/2]; if((syojun&&gaze<=target)||(!syojun&&gaze>=target)){ lt=lt+(rt-lt)/2+1; }else if((syojun&&gaze>target)||(!syojun&&gaze<target)){ rt=lt+(rt-lt)/2-1; } } return lt; } static int kmp(String t,String p){ int[] f=new int[p.length()+1]; int i=0; int j=1; f[1]=0; while(j<p.length()){ if(i==0||p.charAt(i-1)==p.charAt(j-1)){ i++; j++; f[j]=i; }else{ i=f[i]; } } i=1; j=1; while(i<=p.length()&&j<=t.length()){ if(i==0||p.charAt(i-1)==t.charAt(j-1)){ i++; j++; }else{ i=f[i]; } } return i==(p.length()+1)?j-i:-1; } static String StSort(String s){ StringBuilder sb = new StringBuilder(s); int lg = sb.length(); int l; int r; int gaze; for(int i=1;i<lg;i++){ l=0; r=i-1; while(l<=r){ gaze=(l+r)/2; if(sb.charAt(gaze)<=sb.charAt(i)){ l=gaze+1; }else if(sb.charAt(gaze)>sb.charAt(i)){ r=gaze-1; } } sb.insert(l,sb.charAt(i)); sb.deleteCharAt(i+1); } return sb.toString(); } static class Xy{ int x; int y; Xy(int x,int y){ this.x=x; this.y=y; } public int manht(Xy o){ return Math.abs(x-o.x)+Math.abs(y-o.y); } public String toString(){ return "["+x+","+y+"]"; } public double henkaku(){ return Math.atan2(y,x); } public static int hencom(Xy s1,Xy s2){ return (int)Math.signum(s1.henkaku()-s2.henkaku()); } public boolean equals(Object o){ return x==((Xy)o).x&&y==((Xy)o).y; } } static class Zip1{ Map<Long,Integer> zip=new HashMap<>(); long[] unzip; Zip1(long[] a){ Arrays.sort(a); long mae=0;int zure=0; for(int i=0;i<a.length;i++){ if(i==0||mae!=a[i]){ zip.put(a[i],i-zure); mae=a[i]; }else{ zure++; } } unzip=new long[size()]; zure=0; for(int i=0;i<a.length;i++){ if(i==0||mae!=a[i]){ unzip[i-zure]=a[i]; }else{ zure++; } } } int zip(long t){ return zip.get(t); } long unzip(int i){ return unzip[i]; } int size(){ return zip.size(); } } static class UnFd{ int n; int[] a; int forest; UnFd(int n){ forest=this.n=n; a = new int[n]; for(int i=0;i<n;i++){ a[i] = i; } } boolean isRoot(int i){ return a[i]==i; } int rootOf(int i){ if(isRoot(i)){ return i; } return a[i] = rootOf(a[i]); } boolean ud(int i,int j){ return rootOf(i)==rootOf(j); } boolean marge(int i,int j){ if(ud(i,j)){ return false; } i=rootOf(i); j=rootOf(j); a[Integer.max(i,j)]=Integer.min(i,j); forest-=1; return true; } int[] roots(){ int[] rs = new int[forest]; int p=0; for(int i=0;i<n;i++){ if(isRoot(i)){ rs[p]=i; p++; } } return rs; } } static class SegTree<T>{ int n; int m; java.util.function.BinaryOperator<T> op; int[] l; int[] r; T[] a; T ident; SegTree(int n,java.util.function.BinaryOperator<T> op,T ident){ this.n=n; this.op=op; this.ident=ident; int ii=n-1; int p=0; while(ii>0){ ii/=2; p++; } m=repow(2,p+1)-1; @SuppressWarnings("unchecked") T[] b=(T[])(new Object[m]); a=b; Arrays.fill(a,ident); l=new int[m]; r=new int[m]; for(int i=0;i<=m/2;i++){ l[m/2+i]=i; r[m/2+i]=i+1; } for(int i=m/2-1;i>=0;i--){ l[i]=l[lch(i)]; r[i]=r[rch(i)]; } } SegTree(int n,java.util.function.BinaryOperator<T> op,T ident,T[] ary){ this.n=n; this.op=op; this.ident=ident; int ii=n-1; int p=0; while(ii>0){ ii/=2; p++; } m=repow(2,p+1)-1; @SuppressWarnings("unchecked") T[] b=(T[])(new Object[m]); a=b; for(int i=0;i<n;i++){ a[m/2+i]=ary[i]; } for(int i=m/2+n;i<m;i++){ a[i]=ident; } for(int i=m/2-1;i>=0;i--){ a[i]=op.apply(a[lch(i)],a[rch(i)]); } l=new int[m]; r=new int[m]; for(int i=0;i<=m/2;i++){ l[m/2+i]=i; r[m/2+i]=i+1; } for(int i=m/2-1;i>=0;i--){ l[i]=l[lch(i)]; r[i]=r[rch(i)]; } } public T getAll(){ return a[0]; } public T get(int from,int to){ if(from<0||n<to||from>=to){ throw new IllegalArgumentException(String.valueOf(from)+","+String.valueOf(to)); } return get(from,to,0); } private T get(int from,int to,int node){ if(from==l[node]&&to==r[node]){ return a[node]; }else{ if(to<=l[node]+(r[node]-l[node])/2){ return get(from,to,lch(node)); }else if(l[node]+(r[node]-l[node])/2<=from){ return get(from,to,rch(node)); }else{ return op.apply(get(from,l[node]+(r[node]-l[node])/2,lch(node)),get(l[node]+(r[node]-l[node])/2,to,rch(node))); } } } public void set(T ob,int i){ if(i<0||n<=i){ throw new IndexOutOfBoundsException(String.valueOf(i)+"isOutFromLength "+String.valueOf(n)); } int j=m/2+i; a[j]=ob; while(true){ if(j==0){ break; } j=prt(j); a[j]=op.apply(a[lch(j)],a[rch(j)]); } } static private int prt(int node){ return (node-1)/2; } static private int lch(int node){ return 2*node+1; } static private int rch(int node){ return 2*node+2; } public String toString(){ return Arrays.toString(a); } } static boolean next_permutation(int[] per){ if(per.length<2){ return false; } int i; for(i=per.length-1;i>0;i--){ if(per[i-1]<per[i]){ break; } } if(0<i){ i--; int tmp; int j; for(j=per.length-1;j>i;j--){ if(per[j]>per[i]){ break; } } //swap(i,j) tmp=per[i]; per[i]=per[j]; per[j]=tmp; for(int k=i+1;k<(i+1+per.length)/2;k++){ //swap(k,per.length-k+i) tmp=per[k]; per[k]=per[per.length-k+i]; per[per.length-k+i]=tmp; } return true; } int tmp; for(int k=0;k<per.length;k++){ //swap(k,per.length-k-1) tmp=per[k]; per[k]=per[per.length-k-1]; per[per.length-k-1]=tmp; } return false; } static boolean next_bits(boolean[] b){ for(int i=0;i<b.length;i++){ if(b[i]){ b[i]=false; }else{ b[i]=true; return true; } } return false; } static class Scnr{ private final InputStream ins; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; Scnr(){ this(System.in); } Scnr(InputStream in){ ins = in; } private boolean hasNextByte(){ if(ptr<buflen){ return true; }else{ ptr = 0; try{ buflen = ins.read(buffer); }catch(IOException e){ e.printStackTrace(); } if(buflen<=0){ return false; } } return true; } private int readByte(){ if(hasNextByte()){ return buffer[ptr++]; }else{ return -1; } } private static boolean isPrintableChar(int c){ return 33<=c&&c<=126; } public boolean hasNext(){ while(hasNextByte()&&!isPrintableChar(buffer[ptr])){ ptr++; } return hasNextByte(); } public String next(){ return nextBuilder().toString(); } public StringBuilder nextBuilder(){ if(!hasNext()){ throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); int b = readByte(); while(isPrintableChar(b)){ sb.appendCodePoint(b); b = readByte(); } return sb; } public double nextDouble(){ return Double.parseDouble(next()); } public long nextLong(){ if(!hasNext()){ throw new NoSuchElementException(); } long n = 0; boolean minus = false; int b = readByte(); if(b=='-'){ minus=true; b=readByte(); } if(b<'0'||'9'<b){ throw new NumberFormatException(); } while(true){ if('0'<=b&&b<='9'){ n*=10; n+=b-'0'; }else if(b==-1||!isPrintableChar(b)){ return minus?-n:n; }else{ throw new NumberFormatException(); } b=readByte(); } } public int nextInt(){ long nl=nextLong(); if(nl<Integer.MIN_VALUE||Integer.MAX_VALUE<nl){ throw new NumberFormatException(); } return (int) nl; } public String[] nextA(int n){ String[] a = new String[n]; nextA(a,n); return a; } public void nextA(String[] a,int n){ nextA(a,0,n); } public void nextA(String[] a,int off,int len){ for(int i=off;i<off+len;i++){ a[i]=next(); } } public int[] nextAInt(int n){ int[] a = new int[n]; nextAInt(a,n); return a; } public void nextAInt(int[] a,int n){ nextAInt(a,0,n); } public void nextAInt(int[] a,int off,int len){ for(int i=off;i<off+len;i++){ a[i] = nextInt(); } } public long[] nextALong(int n){ long[] a = new long[n]; nextALong(a,n); return a; } public void nextALong(long[] a,int n){ nextALong(a,0,n); } public void nextALong(long[] a,int off,int len){ for(int i=off;i<off+len;i++){ a[i] = nextLong(); } } public double[] nextADouble(int n){ double[] a = new double[n]; nextADouble(a,n); return a; } public void nextADouble(double[] a,int n){ nextADouble(a,0,n); } public void nextADouble(double[] a,int off,int len){ for(int i=off;i<off+len;i++){ a[i] = nextDouble(); } } public List<Integer> nextLInt(int n){ List<Integer> l = new ArrayList<>(n); for(int i=0;i<n;i++){ l.add(sc.nextInt()); } return l; } public List<Long> nextLLong(int n){ List<Long> l = new ArrayList<>(n); for(int i=0;i<n;i++){ l.add(sc.nextLong()); } return l; } public List<Double> nextLDouble(int n){ List<Double> l = new ArrayList<>(n); for(int i=0;i<n;i++){ l.add(sc.nextDouble()); } return l; } } static Scnr sc = new Scnr(); static PrintWriter out = new PrintWriter(System.out); public static void main(String args[]){ int t=sc.nextInt(); for(int tt=0;tt<t;tt++){ solve(); } out.flush(); } public static void solve(){ int n=sc.nextInt(); int[] b=sc.nextAInt(n); int[] q=new int[2*n+1]; Arrays.fill(q,-1); for(int i=0;i<b.length;i++){ q[b[i]]=i; } int[] a=new int[2*n]; for(int i=1;i<=2*n;i++){ if(q[i]!=-1){ a[q[i]*2]=i; continue; } boolean flag=false; for(int j=0;j<n;j++){ if(b[j]<i&&a[j*2+1]==0){ a[j*2+1]=i; flag=true; break; } } if(flag){ continue; } // System.err.println(Arrays.toString(a)); out.println(-1); return; } for(int i=0;i<2*n-1;i++){ out.print(a[i]+" "); } out.println(a[2*n-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
#include <bits/stdc++.h> using namespace std; int n; int num[110]; int ans[220]; bool used[220]; void init() { for (int i = 0; i <= 105; i++) { num[i] = 0; ans[i * 2] = ans[2 * i + 1] = 0; used[i * 2] = used[2 * i + 1] = 0; } } void solve() { for (int i = 1; i <= n; i++) { ans[2 * i - 1] = num[i]; used[num[i]] = 1; } for (int i = 1; i <= n; i++) { for (int j = ans[2 * i - 1]; j <= n * 2; j++) { if (!used[j]) { used[j] = 1; ans[2 * i] = j; break; } } if (ans[i * 2] == 0) { puts("-1"); return; } } for (int i = 1; i <= 2 * n; i++) { cout << ans[i] << ' '; } puts(""); } int main() { int t; cin >> t; while (t--) { init(); cin >> n; for (int i = 1; i <= n; i++) { cin >> num[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
TC = int(input()) def get_unused(nums, res): unused = set(range(1, 2 * n + 1)) for v in res: unused.remove(v) return unused def solve(nums): res = [] n = len(nums) extra = 10 * n used = [0] * (2 * n + 1) for num in nums: res.append(extra) res.append(num) used[num] = 1 for i in range(n): found = False for j in range(nums[i] + 1, 2 * n + 1): if not used[j]: res[2 * i] = j used[j] = 1 found = True break if not found: return [] if res[2 * i] > res[2 * i + 1]: res[2 * i], res[2 * i + 1] = res[2 * i + 1], res[2 * i] return res for _ in range(TC): n = int(input()) *nums, = map(int, input().split()) res = solve(nums) if res: print(*res) 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
t=int(input()) for _ in range(t): n=int(input()) B=[int(i) for i in input().split()] B_set=set(B) check=True Ans=[] Used=[True]*(2*n) for i in range(2*n): if i+1 in B_set: Used[i]=False for b in B: Ans.append(b) for i in range(b,2*n): if Used[i]: Ans.append(i+1) Used[i]=False break else: check=False if check: print(*Ans) 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 java.io.*; import java.util.*; import java.lang.Math; public class Main{ static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter writer; static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } static long nextLong() throws IOException { return Long.parseLong(nextToken()); } static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } static String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static String nexts() throws IOException { tokenizer = new StringTokenizer(reader.readLine()); String s=""; while (tokenizer.hasMoreTokens()) { s+=tokenizer.nextElement()+" "; } return s; } public static int gcd(int x, int y){ if (y == 0) return x; else return gcd(y, x % y); } public static boolean isPrime(int nn) { if(nn<=1){ return false; } if(nn<=3){ return true; } if (nn%2==0||nn%3==0){ return false; } for (int i=5;i*i<=nn;i=i+6){ if(nn%i==0||nn%(i+2)==0){ return false; } } return true; } public static void shuffle(int[] A) { for (int i = 0; i < A.length; i++) { int j = (int)(i * Math.random()); int tmp = A[i]; A[i] = A[j]; A[j] = tmp; } } public static void shufflel(Long[] A) { for (int i = 0; i < A.length; i++) { int j = (int)(i * Math.random()); long tmp = A[i]; A[i] = A[j]; A[j] = tmp; } } public static String reverse(String input) { StringBuilder input2=new StringBuilder(); input2.append(input); input2 = input2.reverse(); return input2.toString(); } public static long power(int x, long n) { // long mod = 1000000007; if (n == 0) { return 1; } long pow = power(x, n / 2); if ((n & 1) == 1) { return (x * pow * pow); } return (pow * pow); } public static long power(long x, long y, long p) //x^y%p { long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) { res = (res * x) % p; } y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long ncr(int n, int k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) { k = n - k; } // Calculate value of [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } public static int inv(int a, int m) //Returns modulo inverse of a with respect to m using extended euclid algorithm { int m0=m,t,q; int x0=0,x1=1; if(m==1){ return 0; } while(a>1) { q=a/m; t=m; m=a%m;a=t; t=x0; x0=x1-q*x0; x1=t; } if(x1<0){ x1+=m0; } return x1; } static int modInverse(int n, int p) { return (int)power((long)n, (long)(p - 2),(long)p); } static int ncrmod(int n, int r, int p) { if (r == 0) // Base case return 1; int[] fac = new int[n + 1]; // Fill factorial array so that we can find all factorial of r, n and n-r fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; //IMPORTANT } static int upper(int a[], int x) //Returns rightest index of x in sorted arr else -1 { //If not present returns index of just smaller element int n=a.length; int l=0; int r=n-1; int ans=-1; while(l<=r){ int m=(r-l)/2+l; if(a[m]<=x){ ans=m; l=m+1; } else{ r=m-1; } } return ans; } static int lower(int a[], int x) //Returns leftest index of x in sorted arr else n { //If not present returns index of just greater element int n=a.length; int l=0; int r=n-1; int ans=n; while(l<=r){ int m=(r-l)/2+l; if(a[m]>=x){ ans=m; r=m-1; } else{ l=m+1; } } return ans; } public static long stringtoint(String s){ // Convert bit string to number long a = 1; long ans = 0; StringBuilder sb = new StringBuilder(); sb.append(s); sb.reverse(); s=sb.toString(); for(int i=0;i<s.length();i++){ ans = ans + a*(s.charAt(i)-'0'); a = a*2; } return ans; } String inttostring(long a,long m){ // a is number and m is length of string required String res = ""; // Convert a number in bit string representation for(int i=0;i<(int)m;i++){ if((a&(1<<i))==1){ res+='1'; }else res+='0'; } StringBuilder sb = new StringBuilder(); sb.append(res); sb.reverse(); res=sb.toString(); return res; } static class R implements Comparable<R>{ int x, y; public R(int x, int y) { //a[i]=new R(x,y); this.x = x; this.y = y; } public int compareTo(R o) { return x-o.x; //Increasing order(Which is usually required) } } /* FUNCTION TO SORT HASHMAP ACCORDING TO VALUE //USE IT AS FOLLOW //Make a new list List<Map.Entry<Integer, Integer> > list = new LinkedList<Map.Entry<Integer, Integer> >(h.entrySet()); // Sort the list Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() { public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) { return (o1.getValue()).compareTo(o2.getValue()); //This is used to sort string stored as VALUE, use below function here accordingly } }); // put data from sorted list to new hashmap HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>(); for (Map.Entry<String, Integer> aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; //Now temp is our new sorted hashmap OR for(int i = 0;i<h.size();i++) //accessing elements from the list made { int count = list.get(i).getValue(); //Value while(count >= 0) { int key=list.get(i).getKey(); //key count -- ; } } //////////////////////////////////////////////////////////////////////////////////////// public int compare(Entry<Integer, Integer> o1, Entry<Integer, Integer> o2) { if(o1.getValue() != o2.getValue()) return o1.getValue() > o2.getValue()?1:-1; //Sorts Value in increasing order else if(o1.getValue() == o2.getValue()){ return o1.getKey() > o2.getKey() ? 1:-1; //If value equal, then sorts key in increasing order } return Integer.MIN_VALUE; } */ //Check if palindrome string - System.out.print(A.equals(new StringBuilder(A).reverse().toString())?"Yes":"No"); public static void main(String[] args) throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); tokenizer = null; writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } private static void solve() throws IOException { int t = nextInt(); while(t-->0){ //String s= nextToken(); //String[] s = str.split("\\s+"); //ArrayList<Integer> ar=new ArrayList<Integer>(); //HashSet<Integer> set=new HashSet<Integer>(); //HashMap<Integer,String> h=new HashMap<Integer,String>(); //R[] a1=new R[n]; //char[] c=nextToken().toCharArray(); int n = nextInt(); //long n = nextLong(); int[] b=new int[n+1]; int[] a=new int[(2*n)+1]; int[] h=new int[(2*n)+1]; //long[] a=new long[n]; for(int i=1;i<=n;i++){ b[i]=nextInt(); h[b[i]]++; } //shuffle(a); //shufflel(a); //Arrays.sort(a); // if(h[1]==0||h[2*n]!=0){ // writer.println(-1); // continue; // } int j=1; int qq=0; for(int i=1;i<=n;i++){ int c=b[i]; int c1=-1; for(int p=c;p<=2*n;p++){ if(h[p]==0){ c1=p; break; } } if(c1==-1){ qq=1; break; } h[c1]++; a[j]=Math.min(c,c1); a[j+1]=Math.max(c,c1); j+=2; } if(qq==1){ writer.println(-1); continue; } for(int i=1;i<=2*n;i++){ writer.print(a[i]+" "); } writer.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
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx") using namespace std; const int MX = 1e9 + 1; const int Imp = -1; const int N = 1e6 + 5; const int M = 1e6 + 5; const long long INF = 1e18 + 1; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int q; cin >> q; while (q--) { int n; cin >> n; int a[n * 2]; set<int> st; bool ok = 1; for (int i = 1; i <= 2 * n; ++i) st.insert(i); for (int i = 0; i < n; ++i) { int x; cin >> x; if (x == n * 2) { ok = 0; } a[i * 2] = x; a[i * 2 + 1] = -1; st.erase(x); } vector<int> v; for (int i = 0; i < n * 2; ++i) { if (a[i] == -1) { if (st.lower_bound(a[i - 1]) == st.end()) { ok = 0; break; } int x = *st.lower_bound(a[i - 1]); v.push_back(x); st.erase(x); } else { v.push_back(a[i]); } } if (!ok) { cout << "-1\n"; continue; } else { for (int i = 0; i < 2 * n; ++i) { cout << v[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
class GFG: def __init__(self,graph, seq): # residual graph self.graph = graph self.ppl = len(graph) self.jobs = len(graph[0]) self.seq = seq # A DFS based recursive function # that returns true if a matching # for vertex u is possible def bpm(self, u, matchR, seen): # Try every job one by one for v in range(self.jobs-1,-1,-1): # If applicant u is interested # in job v and v is not seen if self.graph[u][v] and seen[v] == False: # Mark v as visited seen[v] = True '''If job 'v' is not assigned to an applicant OR previously assigned applicant for job v (which is matchR[v]) has an alternate job available. Since v is marked as visited in the above line, matchR[v] in the following recursive call will not get job 'v' again''' if matchR[v] == -1 or self.bpm(matchR[v], matchR, seen): matchR[v] = u return True return False # Returns maximum number of matching def maxBPM(self): '''An array to keep track of the applicants assigned to jobs. The value of matchR[i] is the applicant number assigned to job i, the value -1 indicates nobody is assigned.''' matchR = [-1] * self.jobs # Count of jobs assigned to applicants result = 0 for i in self.seq: i -= 1 # Mark all jobs as not seen for next applicant. seen = [False] * self.jobs # Find if the applicant 'u' can get a job if self.bpm(i, matchR, seen): result += 1 # print(matchR) self.matchR = matchR return result t = int(input()) for _ in range(t): n = int(input()) b = list(map(int, input().split())) g = [[0 for _ in range(2*n)] for _ in range(2*n)] for i in b: for j in range(i,2*n): if j+1 not in b: g[i-1][j] = 1 m = GFG(g, b) if m.maxBPM() != n: print(-1) else: for i in b: print(i, m.matchR.index(i-1)+1, 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 main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long b[n]; for (long long i = 0; i < n; i++) cin >> b[i]; bool c[2 * n + 1]; memset(c, true, 2 * n + 1); long long a[2 * n]; memset(a, -1, 2 * n); for (long long i = 0; i < n; i++) { a[2 * (i + 1) - 1] = b[i]; c[b[i]] = false; } bool T = true; for (long long i = 0; i < 2 * n; i += 2) { long long j = a[i + 1]; while (c[j] == false && j <= 2 * n) { j++; } if (j > 2 * n) { T = false; break; } a[i] = j; c[j] = false; } if (!T) { cout << -1 << endl; continue; } for (long long i = 0; i < 2 * n; i += 2) { if (a[i] > a[i + 1]) { swap(a[i], a[i + 1]); } } for (long long i = 0; 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
t=int(input()) for i in range(t): n=int(input()) L=[int(j) for j in input().split()] L1=[0]*(2*n) L2=[] for k in range(0,2*n,2): L1[k]=L[k//2] for l in range(1,(2*n)+1,1): if(l not in L1): L2.append(l) for x in range(n): for y in range(0,2*n,2): if(L1[y+1]==0): if(L2[x]>L1[y]): L1[y+1]=L2[x] break if(0 in L1): print(-1) else: print(*L1)
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 long long N = 200 + 10; const long long M = 1e5 + 10; const long long inf = 1e9 + 7; const long long Mod = 1e9 + 7; const double eps = 1e-6; int T; int n; int a[N], b[N]; bool fix[N]; bool f; void init() { for (int i = 0; i < N; ++i) { fix[i] = 0; } f = 1; } int main() { scanf("%d", &T); while (T--) { scanf("%d", &n); init(); for (int i = 1; i <= n; ++i) { scanf("%d", b + i); a[2 * i - 1] = b[i]; fix[b[i]] = 1; } for (int i = 2; i <= 2 * n; i += 2) { bool find = 0; for (int j = b[i / 2] + 1; j <= 2 * n; ++j) { if (fix[j]) continue; a[i] = j; fix[j] = 1; find = 1; break; } if (find == 0) { f = 0; break; } } if (f == 0) puts("-1"); else { for (int i = 1; i <= 2 * n; ++i) printf("%d ", a[i]); puts(""); } } 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 collections import defaultdict for _ in range(int(input())): N = int(input()) List = [int(x) for x in input().split()] Dict = defaultdict(int) for i in List: Dict[i] = 1 Res = [] flag = 0 for i in range(N): Res.append(List[i]) if(List[i] == 2*N): flag = 1 print(-1) break for j in range(List[i]+1,2*N+1): if(Dict[j]==0): Res.append(j) Dict[j] = 1 break elif(j==2*N and Dict[j] == 1): flag = 1 print(-1) break if(flag==1): break if(flag == 0): 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
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; bool used[314] = {}; int A[210] = {}; while (T--) { int N; cin >> N; for (int i = 0; i < N; i++) { cin >> A[i * 2]; used[A[i * 2]] = true; } for (int i = 0; i < N; i++) { int j = A[i * 2] + 1; while (used[j]) j++; A[i * 2 + 1] = j; used[j] = true; } bool ok = true; for (int i = 1; i <= N * 2; i++) { if (!used[i]) ok = false; } if (!ok) cout << -1 << endl; else { for (int i = 0; i < N * 2; i++) cout << A[i] << " "; cout << endl; } ok = true; fill(used, used + 310, false); } }
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 int MAXN = 200009; const long long MOD = 119 << 23 | 1; class { public: int a[222], b[222]; void solve() { int t; cin >> t; while (t--) { int n; cin >> n; set<int> Se; for (int i = 1; i <= 2 * n; ++i) { Se.insert(i); } for (int i = 0; i < n; ++i) cin >> a[i], Se.erase(a[i]); bool ok = 1; for (int i = 0; i < 2 * n; i += 2) { auto it = Se.lower_bound(a[i / 2]); if (it == Se.end()) { ok = 0; break; } b[i] = a[i / 2]; b[i + 1] = *it; Se.erase(it); } if (ok) { for (int i = 0; i < 2 * n; ++i) cout << b[i] << " "; cout << '\n'; } else { cout << -1 << '\n'; } } } } NSPACE; int main() { ; ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); NSPACE.solve(); }
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 import math import heapq import collections fast_reader = sys.stdin.readline fast_writer = sys.stdout.write def input(): return fast_reader().strip() def print(*argv): fast_writer(' '.join((str(i)) for i in argv)) fast_writer('\n') def printspace(*argv): fast_writer(' '.join((str(i)) for i in argv)) fast_writer(' ') def inputnum(): return(int(input())) def inputnums(): return(map(int,input().split())) def inputlist(): return(list(map(int,input().split()))) def inputstring(): return([x for x in input()]) def inputstringnum(): return([ord(x)-ord('a') for x in input()]) def inputmatrixchar(rows): arr2d = [[j for j in input().strip()] for i in range(rows)] return arr2d def inputmatrixint(rows): arr2d = [] for _ in range(rows): arr2d.append([int(i) for i in input().split()]) return arr2d t = int(input()) for q in range(t): n = inputnum() b = inputlist() s = [] ans = [] for i in range(1, 2*n + 1): if i not in b: s.append(i) for i in range(n): ans.append(b[i]) for j in range(n): if s[j] > b[i]: ans.append(s[j]) s[j] = -1 break s.sort() if s[n-1] != -1: 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
for _ in range(int(input())): n=int(input()) b=list(map(int,input().split()))[:n] 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
def solve(): n = eval(input()) b = input().split() vis = [0 for i in range(0, 420)] a = [0 for i in range(0, 400)] pos = 0 flag = True for x in b: vis[int(x)] = 1 for x in b: y = int(x) pos += 1 a[2 * pos - 1] = y ok = False for j in range(y + 1, 2 * n + 1): if vis[j] == 0: vis[j] = 1 a[pos * 2] = j ok = True break if ok == False: flag = False break if flag == False: print(-1) else: for i in range(1, 2 * n + 1): print(a[i], end=" ") print("") t = eval(input()) while t: solve() t -= 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 long long M = 1e8 + 7, maxn = 4e5 + 5; long long int ans, n, m, x, y, q, k, a, b, sum; int32_t main() { cin >> q; while (q--) { cin >> n; long long int a[n]; set<long long int> s; k = 0; for (long long int i = 1; i <= 2 * n; i++) s.insert(i); s.insert(M); for (long long int i = 0; i < n; i++) { cin >> a[i]; if (!s.count(a[i])) { k = 1; } else s.erase(a[i]); } long long int b[2 * n]; long long int j = 0; for (long long int i = 1; i < 2 * n; i += 2) { x = *s.lower_bound(a[j]); if (x == M) { k = 1; break; } b[i] = x; s.erase(x); j++; } j = 0; if (k) { cout << -1 << endl; } else { for (long long int i = 1; i < 2 * n; i += 2) { cout << a[j] << " " << b[i] << " "; j++; } 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, tmp; cin >> n; int a[2 * n]; bool ss[2 * n + 1]; for (int i = (0); i < (2 * n); i++) a[i] = -1; for (int i = (0); i < (2 * n + 1); i++) ss[i] = false; for (int i = (0); i < (n); i++) { cin >> a[i * 2]; ss[a[i * 2]] = true; } bool is = false; for (int i = 0; i < 2 * n; i += 2) { bool is1 = true; for (int j = (a[i] + 1); j < (2 * n + 1); j++) { if (!ss[j]) { a[i + 1] = j; ss[j] = true; is1 = false; break; } } if (is1) { is = true; break; } } if (!is) { for (int i : a) cout << i << ' '; cout << endl; } else { cout << -1 << 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 main() { long long int t = 1; ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while (t--) { long long int start = 0, end = 0, n, m, x = 0, y = 0, index = 0; cin >> n; long int arr[n]; unordered_map<long int, bool> mp; for (long int i = 1; i <= 2 * n; i++) mp[i] = true; for (long int i = 0; i < n; i++) { cin >> arr[i]; mp[arr[i]] = false; } vector<long int> v; for (long int i = 0; i < n; i++) { v.push_back(arr[i]); start = 0; for (long int j = arr[i] + 1; j <= 2 * n; j++) { if (mp[j]) { start = 1; mp[j] = false; v.push_back(j); break; } } if (start == 0) { end = 1; cout << -1 << '\n'; break; } } if (end == 0) { for (auto it : v) cout << it << " "; 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()) lst = [int(i) for i in input().split()] s = {*(range(2 * n + 1))} - {*lst} a = [] try: for i in lst: minn = min(s - {*range(i)}) s -= {minn} a += i, minn 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() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { int n; cin >> n; int aa[n + 1], cc[n + 1], bb[n * 2 + 2], dd[2 * n + 2]; for (int i = 0; i < 2 * n + 2; i++) bb[i] = 1; for (int i = 0; i < n; i++) { cin >> aa[i]; cc[i] = aa[i]; bb[aa[i]] = 0; } sort(cc, cc + n); bool f = true; if (cc[0] != 1 && cc[n - 1] == 2 * n) f = false; int j; for (int i = 0; i < n; i++) { j = aa[i]; while (!bb[j] && j <= 2 * n) j++; if (j > 2 * n) { f = false; break; } dd[i * 2] = aa[i]; dd[i * 2 + 1] = j; bb[j] = 0; } if (!f) cout << "-1" << endl; else { for (int i = 0; i < 2 * n; i++) cout << dd[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; int main() { ios_base::sync_with_stdio(false); int t; cin >> t; while (t--) { int n; cin >> n; int inp[n]; set<int> s; for (int i = 0; i < n; i++) { cin >> inp[i]; s.insert(inp[i]); } vector<pair<int, int> > ans; int act; for (int i = 0; i < n; i++) { act = inp[i] + 1; while (act <= 2 * n) { if (s.count(act) == 0) { ans.push_back(pair<int, int>(inp[i], act)); s.insert(act); break; } else act++; } if (act > 2 * n) break; } if (act > 2 * n) cout << "-1\n"; else { for (int i = 0; i < n; i++) { if (i == n - 1) cout << ans[i].first << " " << ans[i].second << "\n"; else cout << ans[i].first << " " << ans[i].second << " "; } } } 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
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void resPermu(int[] arr, int n){ HashSet<Integer> hset = new HashSet<Integer>(); for(int i = 0 ;i<n ;i++) hset.add(arr[i]); int max= 0; int[] result = new int[2*n]; int index =0; for(int i = 0 ;i<n;i++){ result[index] = arr[i]; int curr = arr[i]; while(hset.contains(curr)){ curr++; } index++; if(curr>max) max = curr; result[index] = curr; index++; hset.add(curr); } if(max>(n*2)){ System.out.print(-1); }else{ for(int i = 0;i<2*n;i++){ System.out.print(result[i]+" "); } } System.out.println(); return; } public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int itr = Integer.parseInt(br.readLine()); while(itr-->0){ int n = Integer.parseInt(br.readLine()); String[] s =br.readLine().split(" "); int[] arr = new int[n]; for(int i = 0 ;i<n ;i++) arr[i] = Integer.parseInt(s[i]); resPermu(arr,n); } } }
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
for _ in range(int(input())): n = int(input()) dic = {x : 1 for x in range(1, 2*n+1)} b = [];ans=True for el in map(int, input().split()): if el == 2*n:print(-1);ans = False dic[el] = 0 b.append(el) if ans: if dic[1] == 1:print(-1);continue a = {} for el in b: i = 1 while dic[i+el] == 0: if i+el>=2*n: ans = False;break i+= 1 a[el] = i+el dic[i+el] = 0 answ = [] for el in b: answ.extend([el, a[el]]) if ans: print(*answ) 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 java.io.*; import java.util.*; import java.lang.*; import java.math.*; public class C { public void run() throws Exception { FastScanner sc = new FastScanner(); int test = sc.nextInt(); outer: for (int j = 0; j<test; j++) { int n = sc.nextInt(); int[] b = new int[n]; int[] a = new int[2*n]; int[] dupb = new int[n]; int[] not = new int[n]; for (int i = 0; i<n; i++) { b[i] = sc.nextInt(); dupb[i] = b[i]; a[2*i] = b[i]; } Arrays.sort(dupb); int point = 0; int points = 0; for (int i = 1; i<=2*n; i++) { if (point==n) { not[points] = i; points++; } else if (dupb[point] == i) { point++; } else { not[points] = i; points++; } } if (not[0] == 1 || dupb[n-1] == 2*n) { System.out.println(-1); continue; } boolean[] ok = new boolean[n]; Arrays.fill(ok, true); for (int i = 0; i<n; i++) { for (int x = 0; x<n+1; x++) { if (x == n) { System.out.println(-1); continue outer; } else if (a[2*i]>not[x]) { ; } else if (!ok[x]) ; else { a[2*i+1] = not[x]; ok[x] = false; break; } } } for (int i = 0; i<n; i++) { System.out.print(a[2*i] + " " + a[2*i+1] + " "); } System.out.println(); } } static class FastScanner { public BufferedReader reader; public StringTokenizer tokenizer; public FastScanner() { 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 int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } } public static void main (String[] args) throws Exception { new C().run(); } }
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
for t in range(int(input())): n=int(input()) a=list(map(int,input().split())) b=[0 for i in range(2*n)] c=[False for i in range(2*n)] i=0 j=0 while(i<2*n): b[i]=a[j]-1 c[a[j]-1]=True i+=2 j+=1 i,k,flag=1,0,1 while(i<2*n): k=b[i-1] flag=1 while(k<2*n): if(c[k]==False): c[k]=True flag=0 break k+=1 if(flag==0): b[i]=k else: break i+=2 if(flag==1): print(-1) else: for i in range(2*n): print(b[i]+1,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; const int INF = 0x3f3f3f3f; const double PI = acos(-1.0); const int T = 102; int a[T], b[T]; int main() { ios_base::sync_with_stdio(false); int tc; cin >> tc; while (tc--) { int n; cin >> n; vector<pair<int, int> > need; set<int> disp; for (int i = 1; i <= 2 * n; i++) disp.insert(i); for (int i = 1; i <= n; i++) { cin >> b[i]; disp.erase(b[i]); a[2 * i - 1] = b[i]; need.emplace_back(2 * i, b[i]); } sort(need.begin(), need.end(), greater<pair<int, int> >()); bool f = 0; while (need.size()) { pair<int, int> top = need.back(); need.pop_back(); int at = -1; for (auto x : disp) { if (x > top.second) { at = x; break; } } if (at == -1) { f = 1; break; } else { disp.erase(at); a[top.first] = at; } } if (f) cout << -1 << '\n'; else { for (int i = 1; i <= 2 * n; i++) cout << a[i] << " "; 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
for _ in range(int(input())): n = int(input()) array = list(map(int, input().split())) dick = {} for i in array: dick[i] = 1 ans = [0]*(2*n) for i in range(n): ans[2*i] = array[i] flag = True for i in range(n): k = ans[2*i] klag = False for j in range(k+1, 2*n +1): if dick.get(j, 0) == 0: dick[j] = 1 klag = True ans[2*i + 1] = j break if klag == False: flag = False break if not flag: 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
t = int(input()) for x in range(t): n = int(input()) arr = list(map(int, input().split())) count = 0 a = 0 out = [] fail = False while(a < n): out.append(arr[a]) count = out[-1] while(count < 2*n): count+=1 if (not count in arr) and (not count in out): if count > arr[a]: out.append(count) count = 2*n + 10 if(out[-1] == arr[a]): fail = True a = 2*n + 1 a+=1 if fail: print(int(-1)) else: print(*out)
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 N = 1e5 + 55; int a[N]; pair<int, int> c[N]; int b[N]; bool vis[N]; int main() { int t; cin >> t; while (t--) { memset(vis, 0, sizeof vis); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; vis[a[i]] = 1; c[i] = {a[i], i}; b[i * 2 - 1] = a[i]; } bool q = 0; for (int i = 1; i <= n; i++) { int j; for (j = a[i] + 1; j <= n * 2; j++) { if (vis[j] == 0) { vis[j] = 1; b[i * 2] = j; break; } } if (j == n * 2 + 1) { q = 1; cout << "-1\n"; break; } } if (!q) for (int i = 1; i <= n; i++) { if (a[i] != min(b[i * 2], b[i * 2 - 1])) { q = 1; cout << "-1\n"; break; } } if (!q) { for (int i = 1; i <= n * 2; i++) cout << b[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 i in range(t): n=int(input()) l=[0]*2*n+[0,0] b=list(map(int,input().split())) if 1 not in b:print(-1) else: t=1 b.insert(0,0) for i in range(1,n+1): l[2*i-1],w=b[i],b[i] while w in b or w in l:w+=1 if w>2*n:t=0;break l[2*i]=w l.pop(0) l.pop(-1) if t:print(*l) 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 javax.jws.soap.SOAPBinding; import java.io.*; import java.lang.reflect.Array; import java.security.acl.LastOwnerException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.stream.Collectors; public class Main { static Long max = (long) (2 * 10E5); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); // Arrays.stream(br.readLine().split("\\s+")).map(Integer::parseInt).toArray(Integer[]::new); // Arrays.stream(br.readLine().split("\\s+")).map(Long::parseLong).toArray(Long[]::new); // Integer.parseInt(br.readLine()); // Long.parseLong(br.readLine()); public static void main(String[] args) throws IOException { int t = Integer.parseInt(br.readLine()); for (int _t = 0; _t < t; _t++) { int n = Integer.parseInt(br.readLine()); Integer[] b = Arrays.stream(br.readLine().split("\\s+")).map(Integer::parseInt).toArray(Integer[]::new); HashSet<Integer> original = new HashSet<>(Arrays.asList(b)); HashSet<Integer> notUsed = new HashSet<>(); for (int i = 1; i <= 2 * n; i++) if (!original.contains(i)) notUsed.add(i); ArrayList<Integer> permutation = new ArrayList<>(); for (int i = 0; i < n; i++) { permutation.add(b[i]); for (int j = b[i] + 1; j <= 2 * n; j++) if (notUsed.contains(j)) { permutation.add(j); notUsed.remove(j); break; } if (permutation.size() != (i + 1) * 2) break; } if (permutation.size() == 2 * n){ for (int i = 0; i < 2 * n - 1; i++) bw.write(permutation.get(i) + " "); bw.write(permutation.get(2 * n - 1) + "\n"); } else bw.write("-1\n"); } bw.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
t=int(input()) for i in range(t): n=int(input()) arr=list(map(int,input().split())) tot=[j for j in range(1,n*2+1)] rem=[j for j in tot if j not in arr] rem=sorted(rem) liste=[] for j in range(n): b1=arr[j] flag=False for k in range(len(rem)): if(rem[k]>b1): liste.append(b1) liste.append(rem[k]) del rem[k] flag=True break if(flag==False): print("-1") break if(flag==True): print(*liste)
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; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; pair<long long, long long> a[n]; long long re[2 * n + 1]; map<long long, long long> mp; for (long long i = 0; i < n; i++) { cin >> a[i].first; mp[a[i].first] = 1; a[i].second = i; } bool ch = true; long long ma = 2 * n; for (long long i = 0; i < n; i++) { ma = a[i].first; while (ma <= 2 * n && mp[ma] == 1) { ma++; } if (ma > 2 * n) { ch = false; break; } if (a[i].second % 2 == 0) { re[2 * a[i].second] = a[i].first; re[2 * a[i].second + 1] = ma; mp[ma] = 1; } else { re[2 * a[i].second] = a[i].first; re[2 * a[i].second + 1] = ma; mp[ma] = 1; } } if (!ch) cout << -1 << endl; else { for (long long i = 0; i < 2 * n; i++) cout << re[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()) l=list(map(int,input().split())) a=[*range(1,2*n+1)] b=[i for i in a + l if i not in a or i not in l] b.sort() ans=[] used=[] f=0 for i in l: for j in b: if j>i and j not in used: used.append(j) ans.append(i) ans.append(j) break if len(ans)!=2*n: 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
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; long long a[n + 10]; map<long long, long long> mp; for (long long i = 0; i < n; i++) { cin >> a[i]; mp[a[i]] = 1; } vector<long long> v; for (long long i = 0; i < n; i++) { v.push_back(a[i]); for (long long j = a[i] + 1; j <= 2 * n; j++) { if (mp[j] == 0) { mp[j] = 1; v.push_back(j); break; } } } if (v.size() == 2 * n) { for (auto x : v) { cout << x << " "; } cout << "\n"; return; } cout << "-1" << "\n"; } int main() { long long t; cin >> t; while (t--) solve(); }
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.Arrays; import java.util.Vector; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.util.Collections; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author xwchen */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { int t = in.nextInt(); while (t-- > 0) { int n = in.nextInt(); int[] b = new int[n + 1]; int[] pos = new int[2 * n + 1]; int[] a = new int[2 * n + 1]; boolean[] occur = new boolean[n * 2 + 1]; for (int i = 1; i <= n; ++i) { b[i] = in.nextInt(); occur[b[i]] = true; pos[b[i]] = 2 * i - 1; } Vector<Integer> others = new Vector<>(); for (int i = 1; i <= 2 * n; ++i) { if (!occur[i]) { others.add(i); } } Collections.sort(others); Arrays.sort(b, 1, n + 1); boolean OK = true; for (int i = 1; i <= n; ++i) { int other = others.get(i - 1); if (b[i] > others.get(i - 1)) { OK = false; break; } else { pos[other] = pos[b[i]] + 1; } } if (!OK) { out.println(-1); continue; } for (int i = 1; i <= 2 * n; ++i) { a[pos[i]] = i; } boolean move = false; do { move = false; for (int i = 2; i <= 2 * n; i += 2) { for (int j = i + 2; j <= 2 * n; j += 2) { int p = i; int q = j; if (a[p] > a[q] && a[p - 1] < a[q] && a[q - 1] < a[p]) { int tmp = a[p]; a[p] = a[q]; a[q] = tmp; move = true; } } } } while (move); for (int i = 1; i <= 2 * n; ++i) { out.print(a[i] + " "); } out.println(); } } } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer = new StringTokenizer(""); public InputReader(InputStream inputStream) { this.reader = new BufferedReader( new InputStreamReader(inputStream)); } public String next() { while (!tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { e.printStackTrace(); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(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
def min_largest(arr, x): start = 0 end = len(arr) - 1 while start <= end: mid = start + (end - start) // 2 if arr[mid] < x: start = mid + 1 else: end = mid - 1 result = start if start < len(arr) else -1 return result def permutation(arr, n): present = set(arr) missing = [i for i in range(1, 2*n+1) if i not in present] result = [] for number in arr: result.append(number) temp = min_largest(missing, number) if temp == -1: return [-1] result.append(missing.pop(temp)) return result if __name__ == '__main__': T = int(input()) for t in range(T): n = int(input()) arr = list(map(int, input().split())) result = permutation(arr, n) for item in result: print(item, end=' ') print() ''' 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10 '''
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 Solution { public static void main(String[] args) { /* * */ Scanner scan = new Scanner(System.in); int t = scan.nextInt(); while(t-->0) { int n = scan.nextInt(); int[] arr = new int[(2*n)]; boolean flag=false; int[] nums = new int[2*n+1]; for(int i=0;i<2*n;i+=2) { int num=scan.nextInt(); if(num>=2*n) flag=true; arr[i]=num; nums[num]++; if(nums[num]>1) flag=true; } if(flag) { System.out.println("-1"); continue; } for(int i=0;i<2*n;i+=2) { int num = arr[i]; for(int j=num+1;j<2*n+1;j++) { if(nums[j]==0) { nums[j]=1; arr[i+1]=j; break; } } if(arr[i+1]==0) { System.out.println(-1); flag=true; break; } } if(!flag) { for(int i=0;i<2*n;i++) { if(i>0) System.out.print(" "); System.out.print(arr[i]); } System.out.println(); } } scan.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
import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class MakingString implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } // if modulo is required set value accordingly public static long[][] matrixMultiply2dL(long t[][],long m[][]) { long res[][]= new long[t.length][m[0].length]; for(int i=0;i<t.length;i++) { for(int j=0;j<m[0].length;j++) { res[i][j]=0; for(int k=0;k<t[0].length;k++) { res[i][j]+=t[i][k]+m[k][j]; } } } return res; } static long combination(long n,long r) { long ans=1; for(long i=0;i<r;i++) { ans=(ans*(n-i))/(i+1); } return ans; } static int mat(String a,String b) { int count=0; for(int i=0;i<a.length();i++) { if(a.charAt(i)!=b.charAt(i)) count++; } return count; } static int accumulate(int arr[], int first, int last) { int init = 0; for (int i = first; i< last; i++) { init = init + arr[i]; } return init; } // Returns true if it is possible to divide // array into two halves of same sum. // This function mainly uses combinationUtil() static Boolean isPossible(int arr[], int n) { // If size of array is not even. if (n % 2 != 0) return false; // If sum of array is not even. int sum = accumulate(arr, 0, n); if (sum % 2 != 0) return false; // A temporary array to store all // combination one by one int k=n/2; int half[] = new int[n/2]; // Print all combination using temporary // array 'half[]' return combinationUtil(arr, half, 0, n - 1, 0, n, sum); } /* arr[] ---> Input Array half[] ---> Temporary array to store current combination of size n/2 start & end ---> Staring and Ending indexes in arr[] index ---> Current index in half[] */ static Boolean combinationUtil(int arr[], int half[], int start, int end, int index, int n, int sum) { // Current combination is ready to // be printed, print it if (index == n / 2) { int curr_sum = accumulate(half, 0 , n/2); return (curr_sum + curr_sum == sum); } // replace index with all possible elements. // The condition "end-i+1 >= n/2-index" makes // sure that including one element at index // will make a combination with remaining // elements at remaining positions for (int i = start; i <= end && end - i + 1 >= n/2 - index; i++) { half[index] = arr[i]; if (combinationUtil(arr, half, i + 1, end, index + 1, n, sum)) return true; } return false; } public static void main(String args[]) throws Exception { new Thread(null, new MakingString(),"MakingString",1<<27).start(); } // **just change the name of class from Main to reuquired** public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = sc.nextInt(); for(int i=0;i<n;i++) { int t=sc.nextInt(); int arr[]=new int[t]; HashMap <Integer,Integer> map = new HashMap(); int max=0; int sum=0; for(int j=0;j<t;j++) { arr[j]=sc.nextInt(); sum=sum+arr[j]; max=Math.max(arr[j],max); map.put(arr[j],j); } int res[]=new int[2*t]; for(int j=0;j<t;j++) { int temp=arr[j]; while(map.containsKey(temp)) { temp++; } map.put(temp,1); res[2*j]=arr[j]; res[2*j+1]=temp; sum=sum+temp; max=Math.max(temp,max); } max=(max*(max+1))/2; if(max==sum) { for(int j=0;j<2*t;j++) { w.print(res[j] + " "); } } else w.print(-1); w.println(""); } System.out.flush(); w.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
for _ in range(int(input())): n=int(input()) a=list(map(int,input().split()));d={} for i in range(n): d[a[i]]=1 ans=[];f=0 for i in range(n): if a[i]<2*n: for j in range(a[i],(2*n)+2): if not d.get(j) and j<=2*n: ans.append(a[i]) ans.append(j) d[j]=1 break if j>2*n: print(-1) f=1 break else: print(-1) f=1 break if f==1: break if f==0: for i in ans: 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
nn = int(input()) for tt in range(nn): total = [] num = int(input()) arr = [] arr1 = str(input()) arr1 = arr1.split() dicts = {} last = 2 * num for i in arr1: kt = int(i) arr.append( int(i) ) dicts[kt] = 1 ans = [] flag = 0 for i in arr: ans.append(i) while True: i += 1 if i > last: flag = 1 break if i not in dicts: dicts[i] = 1 ans.append(i) break if flag == 1: break if flag == 1: print(-1) else: strs = "" for i in ans: strs += str(i) strs += " " print(strs)
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 gcd(long long n, long long m) { if (n == 0) return m; return gcd(m % n, n); } bool ifprime(long long n) { if (n == 1) return false; long long i; for (i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } void disp(vector<long long> v) { for (auto u : v) cout << u << " "; cout << "\n"; } void remleadzero(vector<int> &v) { vector<int> u; int i = 0; while (v[i] == 0) i++; while (i < v.size()) { u.push_back(v[i]); i++; } v = u; } long long fast_expo(long long n, long long m, long long md) { long long a = 1; while (m > 0) { if (m & 1) a = (a * n) % md; n = (n * n) % md; m /= 2; } return a % md; } long long sqroot(long long n) { long double N = n; N = sqrtl(N); long long sq = N - 2; sq = max(sq, 0LL); while (sq * sq < n) { sq++; } if ((sq * sq) == n) return sq; return -1; } int dig_sum(long long n) { int ret = 0; while (n > 0) { ret += (n % 10); n /= 10; } return ret; } int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); ; long long t; cin >> t; while (t--) { int n; cin >> n; vector<int> v; for (int i = 1; i <= 2 * n; i++) v.push_back(i); int b[n]; for (int i = 0; i < n; i++) { cin >> b[i]; if (count((v).begin(), (v).end(), b[i])) { int j = 0; for (; j < v.size(); j++) { if (v[j] == b[i]) break; } v.erase(v.begin() + j); } } sort((v).begin(), (v).end()); vector<int> ans; int done = 0; for (int i = 0; i < n; i++) { ans.push_back(b[i]); int j, found = 0; for (j = 0; j < v.size(); j++) { if (v[j] > b[i]) { found = 1; break; } } if (!found) { done = 1; break; } ans.push_back(v[j]); v.erase(v.begin() + j); } if (done) { cout << -1 << '\n'; continue; } for (int i = 0; i < n; i++) { if (min(ans[2 * i], ans[2 * i + 1]) != b[i]) { cout << -1; done = 1; break; } } if (!done) { for (auto u : ans) cout << u << ' '; } 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.io.BufferedReader; import java.io.InputStreamReader; public class Solution { public static void main(String[] args){ try { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int T = Integer.parseInt(br.readLine()); while (T != 0) { T--; int n= Integer.parseInt(br.readLine()); int [] b = new int[n+1]; boolean [] vis = new boolean[2*(n+1)]; String [] si = br.readLine().split(" "); for(int i=0;i<n;i++){ vis[2*i] = false; vis[2*i +1] = false; } for(int i=0;i<n;i++){ b[i] = Integer.parseInt(si[i]); vis[b[i]-1]=true; } boolean impossible = false; String res = ""; for(int i=0;i<n;i++){ if(i!=0) res +=" "; res+=b[i]; String tmp = ""; for(int j = b[i];j<2*n;j++){ if(!vis[j]){ vis[j]=true; tmp=" "+Integer.toString(j+1); break; } } if(tmp == ""){ impossible = true; break; }else{ res+=tmp; } } if(impossible){ System.out.println(-1); }else{ System.out.println(res); } } }catch (Exception e){ } } }
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 GFG { public static void main (String[] args) throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine().trim()); while(t--!=0){ int n=Integer.parseInt(br.readLine().trim()); String s=br.readLine().trim(); String ss[]=s.split("\\s+"); int a[]=new int[n]; boolean vis[]=new boolean[2*n+1]; for(int i=0;i<n;i++){ a[i]=Integer.parseInt(ss[i]); vis[a[i]]=true; } int res[]=new int[2*n]; boolean pos=true; for(int i=0;i<n;i++){ int l=2*i; int r=2*i+1; /*if(vis[a[i]]){ pos=false; break; } vis[a[i]]=true;*/ res[l]=a[i]; for(int j=a[i]+1;j<=2*n;j++){ if(!vis[j]){ vis[j]=true; res[r]=j; break; } } if(res[r]==0){ pos=false; break; } } if(!pos) System.out.println("-1"); else{ System.out.print(res[0]); for(int i=1;i<2*n;i++) System.out.print(" "+res[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 sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def solve(n,bb): res=[] used = [False] * (2 * n + 1) for b in bb:used[b]=True for b in bb: for c in range(b+1,2*n+1): if not used[c]: res+=[b,c] used[c]=True break else: return [-1] return res def main(): t=II() for _ in range(t): n=II() bb=LI() ans=solve(n,bb) 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
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.TreeSet; public class RestoringPermutation1315C { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int b[] = new int[n]; TreeSet<Integer> ts = new TreeSet<Integer>(); for (int i = 1; i <= 2*n; i++) ts.add(i); for (int i = 0; i < n; i++) { b[i] = sc.nextInt(); ts.remove(b[i]); } int ans[] = new int[2*n]; boolean imp = false; for (int i = 0; i < n && !imp; i++) { ans[(2*(i+1) - 1) - 1] = b[i]; Integer other = ts.higher(b[i]); if (other == null) imp = true; else { ans[2*(i+1) - 1] = other; ts.remove(other); } } if (imp) out.print(-1); else for (int i = 0; i < 2*n; i++) { if (i > 0) out.print(" "); out.print(ans[i]); } out.println(); } out.flush(); out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(FileReader f) { br = new BufferedReader(f); } public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean Ready() throws IOException { return br.ready(); } public void waitForInput(long time) { long ct = System.currentTimeMillis(); while (System.currentTimeMillis() - ct < time) { } ; } } }
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
for t in range(int(input())): n = int(input()) l = [int(i) for i in input().split()] visited = [0 for i in range(2 * n)] res = [] for i in l: visited[i - 1] = 1 for i in l: temp = i while (temp < 2 * n and visited[temp]): temp += 1 if (temp >= 2 * n): res = [-1] break visited[temp] = 1 res.append(i) res.append(temp + 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
#include <bits/stdc++.h> int b[100]; bool used[201]; int pair[201]; int main() { int T; scanf("%d", &T); while (T--) { std::fill(used, used + 201, false); int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { int input; scanf("%d", &input); b[i] = input; used[input] = true; } bool success = true; for (int i = 0; i < n; ++i) { bool found = false; for (int j = b[i]; j <= 2 * n; ++j) { if (!used[j]) { used[j] = true; pair[b[i]] = j; found = true; break; } } if (!found) { success = false; break; } } if (success) { for (int i = 0; i < n; ++i) { printf("%d %d ", std::min(b[i], pair[b[i]]), std::max(b[i], pair[b[i]])); } printf("\n"); } else { printf("-1\n"); } } return 0; }
CPP