Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; int done = 1; for (int i = 0; i < t; i++) { done = 1; int n; cin >> n; int A[n]; int B[2 * n]; int check[2 * n]; for (int i = 0; i < 2 * n; i++) { check[i] = 0; } for (int j = 0; j < n; j++) { cin >> A[j]; B[2 * j] = A[j]; check[A[j] - 1] = 1; } for (int j = 0; j < n; j++) { int val = B[2 * j] + 1; while (val < 2 * n && check[val - 1] == 1) { val++; } if ((val == 2 * n && check[val - 1] == 1 || val == 2 * n + 1)) { done = 0; break; } B[2 * j + 1] = val; check[val - 1] = 1; } if (done == 0) cout << -1; else { for (int j = 0; j < 2 * n; j++) { cout << B[j] << ' '; } } cout << endl; } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import heapq def solve(n,t): remainingCandidates = sorted(list(set([a for a in range(1,2*n + 1)]) - set(t) - set([x for x in range(1, sorted(t)[0], 1)]))) if len(remainingCandidates) != n: return -1 else: used = [False]*len(remainingCandidates) ans = [] for i,x in enumerate(t): ans.append(x) for j,y in enumerate(remainingCandidates): if y > x and not used[j]: ans.append(y) used[j] = True break if len(ans) % 2 != 0: return -1 return " ".join(str(x) for x in ans) for i in range(int(input())): n = int(input()) t = list(map(int, input().split())) print(solve(n,t))
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; template <class T> ostream &operator<<(ostream &os, const vector<T> &V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class T> ostream &operator<<(ostream &os, const set<T> &S) { os << "{ "; for (auto s : S) os << s << " "; return os << "}"; } template <class T> ostream &operator<<(ostream &os, const unordered_set<T> &S) { os << "{ "; for (auto s : S) os << s << " "; return os << "}"; } template <class L, class R> ostream &operator<<(ostream &os, const pair<L, R> &P) { return os << "(" << P.first << "," << P.second << ") "; } template <class L, class R> ostream &operator<<(ostream &os, const map<L, R> &M) { os << "{ "; for (auto m : M) os << "(" << m.first << ":" << m.second << ") "; return os << "}"; } template <class T> ostream &operator<<(ostream &os, stack<T> S) { while (!S.empty()) { os << S.top() << "\n"; S.pop(); } return os; } template <class T> ostream &operator<<(ostream &os, queue<T> Q) { while (!Q.empty()) { os << Q.front() << " "; Q.pop(); } return os; } const long long mod = 1e9 + 7; inline long long modulo(long long x) { x = x % mod; return x < 0 ? x + mod : x; } inline long long add(long long x, long long y) { long long z = modulo(x) + modulo(y); return modulo(z); } inline long long sub(long long x, long long y) { long long z = modulo(x) - modulo(y); return modulo(z); } inline long long mul(long long x, long long y) { long long z = modulo(x) * 1ll * modulo(y); return modulo(z); } inline long long power(long long x, long long y) { x = modulo(x); y = modulo(y); if (y == 0) return 1; long long temp = power(x, y / 2); temp = mul(temp, temp); if (y % 2 == 1) temp = mul(temp, x); return temp; } inline long long inv(long long x) { return power(x, mod - 2); } void solve() { long long n; cin >> n; vector<long long> cache(2 * n, 0), dp(2 * n + 1, 0); for (long long i = 0; i < n; i++) { cin >> cache[2 * i]; dp[cache[2 * i]] = 1; } long long count = 0; for (long long i = 1; 1 < 2 * n ? i <= 2 * n : i >= 2 * n; 1 < 2 * n ? i++ : i--) { if (dp[i] == 0 && count < (i + 1) / 2) { cout << -1 << endl; return; } if (dp[i] == 1) count++; } for (long long i = 0; i < n; i++) { long long index = cache[2 * i]; while (dp[index]) index++; cache[2 * i + 1] = index; dp[index] = 1; index++; } for (auto v : cache) cout << v << " "; cout << endl; } int32_t main() { long long T = 1; cin >> T; for (long long t = 1; 1 < T ? t <= T : t >= T; 1 < T ? t++ : t--) { solve(); } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
I=input for _ in[0]*int(I()): I();b=*map(int,I().split()),;s={*range(2*len(b)+1)}-{*b};a=[] try: for x in b:y=min(s-{*range(x)});s-={y};a+=x,y 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; using ll = long long; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> b(n); vector<int> a(2 * n); vector<char> used(2 * n); for (int i = 0; i < n; ++i) { cin >> b[i]; --b[i]; used[b[i]] = true; a[2 * i] = b[i]; } bool good = true; for (int i = 0; i < n; ++i) { int j = b[i] + 1; for (; j < 2 * n; ++j) { if (!used[j]) { break; } } if (j == 2 * n) { good = false; break; } used[j] = true; a[2 * i + 1] = j; } if (good) { for (auto e : a) cout << e + 1 << " "; } else { cout << -1; } 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
n = int(input()) for a in range(n): answer = "" size = int(input()) nums = list(map(int,input().split())) taken = set(nums) possible = True if 1 not in taken: print(-1) continue for num in nums: answer += str(num) + " " found = False for b in range(num+1,2*size+1): if b not in taken: found = True taken.add(b) answer += str(b) + " " break if not found: possible = False break if possible: print(answer) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in range(int(input())): n=int(input()) b=list(map(int,input().split())) a=[] rem=set([i for i in range(1,2*n+1)])-set(b) f=True for i in range(n): a.append(b[i]) ai=float('inf') for j in rem: if j>b[i] and ai>j: ai=j if ai==float('inf'): f=False break rem.remove(ai) a.append(ai) if f: print(*a) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import sys import bisect input = sys.stdin.readline ins = lambda: input().rstrip() ini = lambda: int(input().rstrip()) inm = lambda: map(int, input().rstrip().split()) inl = lambda: list(map(int, input().split())) out = lambda x: print('\n'.join(map(str, x))) finalans = [] t = ini() for _ in range(t): n = ini() b = inl() a = [i for i in range(1, n*2+1) if i not in b] out = True j = 0 for index, number in enumerate(sorted(b)): if number > a[index]: out = False finalans.append([-1]) break if not out: continue ans = [] for i in b: x = bisect.bisect_right(a, i) ans.append(i) ans.append(a[x]) a.pop(x) finalans.append(ans) for i in finalans: print(*i)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; vector<bool> flag(2 * n + 1, false); for (int i = 0; i < n; i++) { cin >> a[i]; flag[a[i]] = true; } vector<int> st; for (int i = 1; i <= 2 * n; i++) { if (!flag[i]) { st.push_back(i); } } if (flag[2 * n] || !flag[1]) { cout << -1 << endl; } else { vector<int> b; int d = 0; for (int i = 0; i < n; i++) { b.push_back(a[i]); vector<int>::iterator itr; for (itr = st.begin(); itr != st.end(); itr++) { if (*itr > a[i]) { break; } } if (itr == st.end()) { cout << -1 << endl; d++; break; } b.push_back(*itr); st.erase(itr); } if (d) { continue; } for (int i = 0; i < b.size(); 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
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 9; const int MOD = 1000000007; int mod = 999998639; inline long long qpow(long long b, long long e, long long m = MOD) { long long a = 1; for (; e; e >>= 1, b = b * b % m) if (e & 1) a = a * b % m; return a; } set<int> s; int b[109]; vector<int> ve; int main() { int _; while (scanf("%d", &_) != EOF) { while (_--) { ve.clear(); s.clear(); int n; scanf("%d", &n); for (int i = 1; i <= 2 * n; i++) { s.insert(i); } for (int i = 1; i <= n; i++) { scanf("%d", &b[i]); s.erase(b[i]); } bool flag = false; for (int i = 1; i <= n; i++) { set<int>::iterator it = s.upper_bound(b[i]); if (it == s.end()) { flag = true; break; } else { ve.push_back(b[i]); ve.push_back(*it); s.erase(it); } } if (flag) { printf("-1\n"); } else { for (int i = 0; i < ve.size(); i++) { printf("%d ", ve[i]); } printf("\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
# -*- coding: utf-8 -*- import sys from collections import Counter def input(): return sys.stdin.readline().strip() def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)] def ceil(x, y=1): return int(-(-x // y)) def INT(): return int(input()) def MAP(): return map(int, input().split()) def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)] def Yes(): print('Yes') def No(): print('No') def YES(): print('YES') def NO(): print('NO') # sys.setrecursionlimit(10 ** 9) INF = 10 ** 18 MOD = 10 ** 9 + 7 for _ in range(INT()): N = INT() B = LIST() A = [0] * (N*2) S = set() for i in range(N): A[i*2] = B[i] S.add(B[i]) for i in range(N): b = A[i*2] for j in range(b+1, N*2+1): if j not in S: A[i*2+1] = j S.add(j) break for i in range(N): if A[i*2] > A[i*2+1]: print(-1) break else: print(*A)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import sys import bisect def metod(x): if t[x] == "A": w = a else: w = b for i in range(x + 1, len(t)-1): if t[i] != t[i - 1]: if t[i] == "A": w+=a else: w+=b return w def morzermorzer(): n = int(input()) w = list(map(int, input().split())) isk = [] for i in range(1, 2 * n + 1): if i not in w: isk.append(i) isk.sort() ans = [0 for i in range(2*n)] for i in range(n): ans[i * 2] = w[i] fl = 0 for i in range(n - 1): ind = bisect.bisect(isk, w[i]) if ind != n - i: r = isk[ind] del isk[ind] ans[i * 2 + 1] = r else: print(-1) return 0 if isk[-1] < w[-1]: print(-1) return 0 else: ans[-1] = isk[-1] print(*ans) for morzer in range(int(input())): morzermorzer()
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 min_number_after(number:int): global other_sequence for k in range(len(other_sequence)): if other_sequence[k] > number: return other_sequence[k] return "No" def list_subtract(): global sequence, n other_sequence = list(range(1, 2*n+1)) for k in sequence: other_sequence.remove(k) return other_sequence t = int(input()) for i in range(t): n = int(input()) sequence = list(map(int, input().split(' '))) other_sequence = list_subtract() quantity_operations = len(sequence)*2 for j in range(0, quantity_operations, 2): temp = min_number_after(sequence[j]) if temp != "No": sequence.insert(j + 1, temp) other_sequence.remove(sequence[j + 1]) else: print(-1) break else: sequence = [str(x) for x in sequence] print(' '.join(sequence))
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 t=int(sys.stdin.readline()) for i in range(t): n=int(sys.stdin.readline()) b=[int(j) for j in sys.stdin.readline().split()] arr=[] # perm=[] # for p in range(1,2*n+1): # perm.append(p+1) tot=2*n mark=[0]*tot for g in range(n): mark[b[g]-1]=1 ans=True for h in range(n): done=0 for k in range(2*n): if(mark[k]==0 and k+1>b[h]): mark[k]=1 tmp=[b[h],k+1] tmp.sort() arr.append(tmp) done=1 break if(done==0): ans=False break if(ans==True): for w in range(n): print(arr[w][0],end=" ") print(arr[w][1],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
from collections import defaultdict, Counter def getlist(): return list(map(int, input().split())) def main(): t = int(input()) for num in range(t): n = int(input()) arr = getlist() arr1=arr.copy() box = [] if min(arr)!=1 or max(arr)>=2*n: print(-1) else: for number in arr1: box.append(number) while number+1 in arr: number+=1 box.append(number+1) arr.append(number+1) if max(box)>2*n: print(-1) else: print(*box) 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 sys from collections import defaultdict, deque inp = sys.stdin.readline read = lambda: list(map(int, inp().split())) def a(): ans = "" for _ in range(read()[0]): a, b, x, y = read() x += 1 y += 1 ans += str(max(((a-x)*(b)), ((x-1)*(b)), ((a)*(b-y)), ((a)*(y-1)))) + "\n" print(ans) def c(): ans = "" for _ in range(read()[0]): flag = 0 n = int(inp()) dic = dict() for ind, elem in enumerate(read()): dic[elem] = (ind*2, ind*2+1) arr = [None]*(2*n) left = set(i for i in range(1, 2*n+1)).difference(dic.keys()) for i in dic.keys(): elem = dic[i] mins = [y for y in left if y > i] if mins: sup = min(mins) arr[elem[0]], arr[elem[1]] = i, sup left.remove(sup) else: flag = 1 break if flag: ans += "-1\n" else: ans += (" ").join(map(str, arr)) + "\n" print(ans) if __name__ == "__main__": # a() # b() c() # 1 2 3 4 5 6 7 8 # 2 1 3 6 4 7 5 8 # 1 - 1, 2 - 3 # 2 - 7, 8 - 4 # 5 - 3, 4 - 6 # 7 - 5, 6 - 9 # 8 - 9, 10 - 10 # 1 3 5 6 7 9 2 4 8 10 # 3 # 4 # 6 # 9 # 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.Arrays; import java.util.HashSet; import java.util.Scanner; public class restoringPermutation { public static void main(String[] args) { Scanner sc = new Scanner(System.in); StringBuilder finalAns = new StringBuilder(); int t = sc.nextInt(); while (--t >= 0) { StringBuilder ans = new StringBuilder(); int n = sc.nextInt(), b[] = new int[n]; HashSet<Integer> hs = new HashSet<Integer>(2 * n); for (int i = 0; i < n; i++) { b[i] = sc.nextInt(); hs.add(b[i]); } int flag = 0; for (int i = 0; i < n; i++) { ans.append(b[i] + " "); int temp = b[i] + 1; while (hs.contains(temp)) { ++temp; } if (temp > 2 * n) { flag = 1; break; } if (((2 * i) + 1) < 2 * n) { ans.append(temp + " "); hs.add(temp); } } finalAns.append(((flag == 0) ? ans : -1) + "\n"); } sc.close(); System.out.println(finalAns); } }
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; template <typename T> using second = set<T>; template <typename T> using v = vector<T>; const int Max = 2e5 + 100; const int Mx = 5e1 + 10; const int Len = 1e6; const int MOD = 1e9 + 7; const long long INF = 1e18; int A[Max]; bool usedNum[Max]; int main() { int q; cin >> q; while (q--) { int n; cin >> n; bool able = true; fill(usedNum, usedNum + 2 * n + 100, false); fill(A, A + 2 * n + 100, -1); for (int i = 0; i < n; i++) { cin >> A[2 * i]; if (!usedNum[A[2 * i]]) { usedNum[A[2 * i]] = true; } else { able = false; } } for (int i = 1; i < 2 * n; i += 2) { int trg = -1; for (int j = A[i - 1] + 1; j <= 2 * n; j++) { if (!usedNum[j]) { trg = j; usedNum[trg] = true; break; } } A[i] = trg; } for (int i = 0; i < 2 * n; i++) { if (A[i] == -1) able = false; } if (able) { cout << ""; for (int i = 0; i < 2 * n; i++) { cout << A[i] << ' '; } cout << '\n'; } 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.*; import java.util.stream.Collectors; public class RestoringPermutation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int test = scanner.nextInt(); scanner.nextLine(); for (int t = 0; t < test; t++) { int n = scanner.nextInt(); scanner.nextLine(); int[]arr = new int[n]; Set<Integer> nums = new HashSet<>(); for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); nums.add(arr[i]); } scanner.nextLine(); TreeSet<Integer> remaining = new TreeSet<>(); for (int i = 1; i < 2 * n + 1; i++) { if (!nums.contains(i)) { remaining.add(i); } } int[] result = new int[2 * n]; boolean invalid = false; for (int i = 0; i < n; i++) { int num1 = arr[i]; Integer ceiling = remaining.ceiling(num1); if (ceiling != null) { result[2 * i] = num1; result[2 * i + 1] = ceiling; remaining.remove(ceiling); }else{ invalid = true; break; } } if (invalid) { System.out.println(-1); }else{ System.out.println(Arrays.stream(result).mapToObj(e -> String.valueOf(e)).collect(Collectors.joining(" "))); } } } }
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 long long inf = 1e16; const long long N = 1e5 + 1; const long long mod = 1e9 + 7; void solve() { long long n; cin >> n; long long b[n + 1]; for (long long i = 1; i <= n; i++) { cin >> b[i]; } deque<long long> ans(2 * n + 1), rem; for (long long i = 1; i <= 2 * n; i++) { rem.push_back(i); } for (long long i = 1; i <= n; i++) { auto it = find(rem.begin(), rem.end(), b[i]); if (it != rem.end()) { rem.erase(it); } } for (long long i = 1; i <= n; i++) { ans[2 * i - 1] = b[i]; auto it = upper_bound(rem.begin(), rem.end(), b[i]); if (it == rem.end()) { cout << -1 << "\n"; return; } ans[2 * i] = *it; rem.erase(it); } ans.pop_front(); for (auto x : ans) { cout << x << " "; } cout << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) solve(); return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import os,io input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline cases = int(input()) for t in range(cases): n = int(input()) b = list(map(int,input().split())) a = [0]*(2*n) d = {i:True for i in range(1,2*n+1)} for i in range(n): p = i+1 pos = 2*p-1 a[pos-1] = b[i] d[b[i]] = False f = 0 for i in range(n): p = i+1 pos = 2*p v = b[i]+1 while v in d and not d[v]: v+=1 if v>2*n: print(-1) f=1 break else: d[v] = False a[pos-1] = v if f==0: 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
from bisect import bisect_right t = int(input()) for _ in range(t): n = int(input()) b = [ int(x) for x in input().split()] arr = [False]*(2*n+1) result = [0]*(n*2) for i, val in enumerate(b): result[i*2] = val arr[val] = True arr[0] = True arr = [ i for (i, v) in enumerate(arr) if v==False] arr = sorted(arr) err = False for i in range(n): j = bisect_right(arr, result[i*2]) if j==len(arr): err = True break result[2*i+1] = arr[j] arr.pop(j) if err: print("-1") else: print(" ".join(map(str, result)))
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) h=[0]*((2*n)+1) for i in l: h[i]=1 sl=[] i=0 mm=0 for i in range(n): flag=0 for j in range(l[i]+1,(2*n)+1): if h[j]==0: flag=1 sl.append(l[i]) sl.append(j) h[j]=1 break if flag==0: break if flag==0: print(-1,end="") else: for i in range(len(sl)): print(sl[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
# Author : raj1307 - Raj Singh # Date : 23.02.2020 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().strip().split(" ")) def msi(): return map(str,input().strip().split(" ")) def li(): return list(mi()) def dmain(): sys.setrecursionlimit(100000000) threading.stack_size(40960000) thread = threading.Thread(target=main) thread.start() #from collections import deque, Counter, OrderedDict,defaultdict #from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace from math import ceil,floor,log,sqrt,factorial #from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right #from decimal import *,threading from itertools import permutations #Copy 2D list m = [x[:] for x in mark] .. Avoid Using Deepcopy abc='abcdefghijklmnopqrstuvwxyz' abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod=1000000007 #mod=998244353 inf = float("inf") vow=['a','e','i','o','u'] dx,dy=[-1,1,0,0],[0,0,1,-1] def getKey(item): return item[1] def sort2(l):return sorted(l, key=getKey,reverse=True) def d2(n,m,num):return [[num for x in range(m)] for y in range(n)] def isPowerOfTwo (x): return (x and (not(x & (x - 1))) ) def decimalToBinary(n): return bin(n).replace("0b","") def ntl(n):return [int(i) for i in str(n)] def ncr(n,r): return factorial(n)//(factorial(r)*factorial(n-r)) def powerMod(x,y,p): res = 1 x %= p while y > 0: if y&1: res = (res*x)%p y = y>>1 x = (x*x)%p return res def gcd(x, y): while y: x, y = y, x % y return x def isPrime(n) : # Check Prime Number or not if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True def read(): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') def main(): for _ in range(ii()): n=ii() b=li() f=[0]*500 for i in range(n): f[b[i]]=1 l=[] for i in range(n): l.append([b[i],i]) l.sort() ans=[0]*(2*n) flag=0 p=[] for i in range(n): x=b[i] pos=i flag=0 for j in range(x+1,2*n+1): if f[j]==0: f[j]=1 ans[2*pos]=x ans[2*pos+1]=j #print(2*pos-1,2*pos,'j') p.append(j) flag=1 break if flag==0: break """for i in range(n): x=l[i][0] pos=l[i][1] flag=0 for j in range(x+1,2*n+1): if f[j]==0: f[j]=1 ans[2*pos]=x ans[2*pos+1]=j #print(2*pos-1,2*pos,'j') p.append(j) flag=1 break if flag==0: break """ if flag: #p.sort() #for i in range(n): # for j in p: # if a[2*i+1] print(*ans) else: print(-1) continue # 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() #dmain() # 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
t = int(input()) for i in range(t): n = int(input()) l = list(map(int , input().split(' '))) s = set(l) l2 = [] for j in range(1 , 2*n + 1): if(j not in s): l2.append(j) l2.sort() done = 0 for j in range(len(l2)): flag = 0 for k in range(n): if(isinstance(l[k] , int) and l2[j] > l[k]): l[k] = [l[k] , l2[j]] flag = 1 break if(flag == 0): print(-1) done = 1 break flag = 0 if(done == 1): done = 0 else: for j in range(len(l)): print(min(l[j]) , end = ' ') print(max(l[j]) , end = ' ') print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
def oki(n,lst): tm = [] for i in range(1,2*n+1): if i not in lst: tm.append(i) out = [] for i in lst: temp = None for j in range(i + 1, 2*n+1): if j in tm: temp = j break if temp == None: out = [] break out.append(i) out.append(j) tm.remove(temp) # print(out) if out: print(*out) else: print('-1') if __name__ == '__main__': for _ in range(int(input())): n = int(input()) lst = list(map(int,input().split(' '))) oki(n,lst)
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()) def closest_greater(num,a): for x in range(num,len(a)): if(a[x] == 1): a[x] = 0 return x return -1 while(t>0): n = int(input()) s = input() b = ['0'] b= b+ s.split() a = [1]*(2*len(b) - 1) flag = 0 ans = [] for x in range(1,len(b)): a[int(b[x])]-=1 for x in range(1,len(b)): current = int(b[x]) ans.append(current) nex = closest_greater(current,a) if(nex == -1): flag = 1 break else: a[nex] = 0 ans.append(nex) if(flag == 1): print('-1') else: for x in ans: print(x,end=' ') 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; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int b[n + 1]; set<int> s; for (int i = 0; i < n; i++) { cin >> b[i]; s.insert(b[i]); } int a[n * 2 + 5]; memset(a, 0, sizeof(a)); int pos = 0; for (int i = 0; i < n * 2; i += 2) { a[i] = b[pos++]; } bool possible = true; for (int i = 1; i < n * 2; i += 2) { if (a[i] == 0) { int temp = a[i - 1]; while (true) { temp++; if (s.find(temp) == s.end()) break; } if (temp <= n * 2) { a[i] = temp; s.insert(temp); } else { possible = false; break; } } } if (possible) { for (int i = 0; i < n * 2; i++) { cout << a[i] << ' '; } } else { cout << -1; } cout << endl; } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long inf = 0x3f3f3f3f; const long long nax = 0; void solve() { long long n; vector<long long> b(210, 0), arr(105); vector<bool> vis(210, 0); cin >> n; for (int i = 1; i <= n; ++i) { cin >> arr[i]; vis[arr[i]] = 1; } for (int i = 1; i <= n; ++i) { b[2 * i - 1] = arr[i]; int angka = arr[i] + 1; while (vis[angka] && angka <= 2 * n) { if (angka == 2 * n) { angka = -1; break; } angka++; } if (angka == -1 || angka > n * 2) { cout << "-1" << '\n'; return; } vis[angka] = 1; b[2 * i] = angka; } for (int i = 1; i <= 2 * n; ++i) { cout << b[i] << ' '; } cout << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); int 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.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { FastReader scan = new FastReader(); //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("taming.out"))); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); Task solver = new Task(); int t = scan.nextInt(); //int t = 1; for(int i = 1; i <= t; i++) solver.solve(i, scan, out); out.close(); } static class Task { public void solve(int testNumber, FastReader sc, PrintWriter pw) { int x = sc.nextInt(); HashSet<Integer> hs = new HashSet<>(); for(int i=1;i<=2*x;i++){ hs.add(i); } int[] arr = new int[2*x]; for(int i=0;i<2*x;i+=2){ arr[i]=sc.nextInt(); hs.remove(arr[i]); } //pw.println(hs); for(int i=1;i<2*x;i+=2){ if(hs.contains(arr[i-1]+1)){ arr[i]=arr[i-1]+1; hs.remove(arr[i]); } else{ for(int j=arr[i-1];j<=2*x;j++){ if(hs.contains(j)){ arr[i]=j; hs.remove(j); break; } if(j==2*x){ pw.println(-1); return; } } } } for(int t : arr){ pw.print(t+" "); } pw.println(); } } static class tup implements Comparable<tup> { int a, b; tup() { } ; tup(int a, int b) { this.a = a; this.b = b; } @Override public int compareTo(tup o2) { return Integer.compare(b, o2.b); } public String toString(){ return a+" "+b; } } static void shuffle(long[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); long temp = a[i]; a[i] = a[r]; a[r] = temp; } } static void shuffle(int[] a) { Random get = new Random(); for (int i = 0; i < a.length; i++) { int r = get.nextInt(a.length); int temp = a[i]; a[i] = a[r]; a[r] = temp; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s))); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import bisect for _ in range(int(input())): nb = int(input()) b = [int(j) for j in input().split()] vis = [0 for i in range(nb*2)] for i in range(nb): vis[b[i]-1]=1 l = [] for i in range(2*nb): if not vis[i]: l.append(i+1) ans = [] for i in b: ind = bisect.bisect_left(l,i) if ind==len(l): print(-1) break else: ans.append(i) ans.append(l[ind]) l.pop(ind) 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()) while t>0: t-=1 n=int(input()) a=list(map(int,input().split())) x=[] for i in range(n): x.append(a[i]) k=a[i]+1 q=True while q==True: if k in a or k in x : k=k+1 else: q=False break x.append(k) if max(x)==2*n: for i in range(len(x)): print(x[i],end=" ") print(" ") else: print(-1) #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 sys # input = lambda: sys.stdin.readline().rstrip() def last_proverka(v): for i in range(len(v)): i += 1 if i not in v: return -1 return True # v - ans # h - b def l_proverka(v, c): for i in range(len(c)): if v[i] == c[i]: return False return True def x(m, sr): for i in range(1000): if i > sr and i not in m and i not in a: return i return False for _ in range(int(input())): n = int(input()) b = list(map(int, input().split())) a = [0] * 2 * n for i in range(n): a[2 * i] = b[i] k = True for i in range(2 * n): if a[i] == 0: c = x(b, a[i - 1]) if c == False: k = False break else: a[i] = c if last_proverka(a) == True: print(*a) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.Scanner; //https://codeforces.com/problemset/problem/1315/C public class ResortingPermutations { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int[] arr = new int[n]; for (int j = 0; j < n; j++) { arr[j] = sc.nextInt(); } System.out.println(permutation(arr)); } } private static String permutation(int[] ar) { int n = ar.length; int[] per = new int[(2 * n) + 1]; String output = ""; for (int i = 0; i < n; i++) { per[ar[i]] = 1; } for (int i = 0; i < n; i++) { boolean perm = false; final int b = ar[i]; int j = b + 1; for (; j <= 2 * n; j++) { if (per[j] != 1) { output = output + " " + b + " " + j; per[j] = 1; perm = true; break; } } if (!perm) { int[] t = {}; return output = "-1"; } } return output; } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
from bisect import bisect_left as bl, bisect_right as br, insort import sys import heapq from math import * from collections import defaultdict as dd, deque def data(): return sys.stdin.readline().strip() def mdata(): return map(int, data().split()) #sys.setrecursionlimit(100000) for i in range(int(input())): n=int(data()) b=list(mdata()) l=[] for i in range(1,2*n+1): if i not in b: l.append(i) l1=[] for i in range(n): flag = False for j in range(len(l)): if b[i]<l[j]: l1.append(b[i]) l1.append(l[j]) l.pop(j) flag=True break if flag==False: break if flag==True: print(*l1) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int a[205], b[105]; set<int> st; int n; void solve() { cin >> n; for (int i = 1; i <= n; i++) cin >> b[i]; st.clear(); for (int i = 1; i <= 2 * n; i++) st.insert(i); for (int i = 1; i <= n; i++) { st.erase(b[i]); } if (st.size() != n) { cout << -1 << endl; return; } for (int i = 1; i <= n; i++) { a[2 * i - 1] = b[i]; } for (int i = 2; i <= 2 * n; i += 2) { auto tmp = st.upper_bound(a[i - 1]); if (tmp == st.end()) { cout << -1 << endl; return; } a[i] = *tmp; st.erase(tmp); } for (int i = 1; i <= 2 * n; i++) { cout << a[i] << " "; } cout << endl; } int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) { solve(); } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
# -*- coding: utf-8 -*- """ Created on Tue Sep 22 20:42:16 2020 @author: Dark Soul """ t=int(input('')) for _ in range(t): n=int(input('')) tst=list(map(int,input().split())) b=[0] for i in tst: b.append(i) a=[0]*(2*n+1) used=[0]*(2*n+1) khaise=0 for i in range(1,n+1): a[2*(i-1)+1]=b[i] if used[b[i]]: khaise=1 used[b[i]]=1 for i in range(2,2*n+1,2): for j in range(a[i-1],2*n+1,1): if (1-used[j]): break if j>2*n: khaise=1 a[i]=j used[j]=1 for i in range(1,2*n+1): if (1-used[i]): khaise=1 if khaise: print(-1) else: print(*a[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.nio.CharBuffer; import java.util.NoSuchElementException; public class P1315C { public static void main(String[] args) { SimpleScanner scanner = new SimpleScanner(System.in); PrintWriter writer = new PrintWriter(System.out); int caseNum = scanner.nextInt(); while (caseNum-- > 0) { int n = scanner.nextInt(); boolean[] used = new boolean[2 * n + 1]; int[] b = new int[n]; for (int i = 0; i < n; ++i) { b[i] = scanner.nextInt(); used[b[i]] = true; } boolean possible = true; int[] ans = new int[2 * n]; for (int i = 0; i < n; ++i) { ans[i * 2] = b[i]; boolean find = false; for (int j = b[i] + 1; j <= 2 * n; ++j) { if (!used[j]) { used[j] = true; ans[i * 2 + 1] = j; find = true; break; } } if (!find) { possible = false; break; } } if (possible) { for (int i = 0; i < 2 * n; ++i) writer.print(ans[i] + " "); writer.println(); } else writer.println(-1); } writer.close(); } private static class SimpleScanner { private static final int BUFFER_SIZE = 10240; private Readable in; private CharBuffer buffer; private boolean eof; private SimpleScanner(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); buffer = CharBuffer.allocate(BUFFER_SIZE); buffer.limit(0); eof = false; } private char read() { if (!buffer.hasRemaining()) { buffer.clear(); int n; try { n = in.read(buffer); } catch (IOException e) { n = -1; } if (n <= 0) { eof = true; return '\0'; } buffer.flip(); } return buffer.get(); } private void checkEof() { if (eof) throw new NoSuchElementException(); } private char nextChar() { checkEof(); char b = read(); checkEof(); return b; } private String next() { char b; do { b = read(); checkEof(); } while (Character.isWhitespace(b)); StringBuilder sb = new StringBuilder(); do { sb.append(b); b = read(); } while (!eof && !Character.isWhitespace(b)); return sb.toString(); } private int nextInt() { return Integer.parseInt(next()); } private long nextLong() { return Long.parseLong(next()); } private double nextDouble() { return Double.parseDouble(next()); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> istream &operator>>(istream &is, vector<pair<T1, T2>> &v) { for (pair<T1, T2> &t : v) is >> t.first >> t.second; return is; } template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &t : v) is >> t; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &t : v) { os << t << " "; } os << '\n'; return os; } double pi = acos(-1.0); long long md = 1000000007; long long abst(long long a) { return a < 0 ? -a : a; } struct HASH { size_t operator()(const pair<long long, long long> &x) const { return (size_t)x.first * 37U + (size_t)x.second; } }; struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; long long Pow(long long n, long long x) { long long ans = 1; while (x > 0) { if (x & 1) ans = (ans * n) % md; n = (n * n) % md; x = x >> 1; } return ans; } vector<long long> fact, inv; void inverse(long long n) { inv.resize(n + 1); inv[0] = 1; for (long long i = 1; i <= n; i++) inv[i] = Pow(fact[i], md - 2); } void factorial(long long n) { fact.resize(n + 1); fact[0] = 1; for (long long i = 1; i <= n; i++) fact[i] = (fact[i - 1] * i) % md; } long long max(long long a, long long b) { return a > b ? a : b; } long long min(long long a, long long b) { return a < b ? a : b; } void solve() { long long n; cin >> n; vector<long long> v(n); cin >> v; vector<long long> ans(2 * n); vector<long long> vis(2 * n + 1, -1); for (long long i = 0; i < n; i++) { ans[2 * i] = v[i]; vis[v[i]] = 1; } long long ptr; for (long long i = 1; i < 2 * n; i += 2) { ptr = -1; for (long long j = 1; j <= 2 * n; j++) if (vis[j] == -1 && j > ans[i - 1]) { ptr = j; break; } if (ptr == -1) { cout << -1 << "\n"; return; } ans[i] = ptr; vis[ptr] = 1; } cout << ans; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long t = 1; 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.BufferedReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; import java.util.Stack; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int a[]=new int[210]; for(int i=1;i<=200;i++) a[i]=0; int b[]=new int[101]; int ans[]=new int[210]; int n=sc.nextInt(); for(int i=1;i<=n;i++) {b[i]=sc.nextInt();a[b[i]]=1;} for(int i=1;i<=n;i++) { for(int j=b[i]+1;j<=2*n;j++) { if(a[j]==0) {ans[2*i-1]=b[i];ans[2*i]=j;a[j]=1;break;} } } int g=1; for(int i=1;i<=2*n;i++) if(a[i]==0) g=0; if(g==0) System.out.println(-1); else {for(int i=1;i<=2*n;i++) System.out.print(ans[i]+" "); System.out.println(); } } sc.close(); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
def revolt(n,l): l1=[0 for i in range(2*n)] j=0 i=0 while j<n: l1[i]=l[j] j+=1 i+=2 d={} for e in l: d[e]=True k=1 while k<2*n : val=l1[k-1]+1 while d.get(val) and val<=2*n: val+=1 if val>2*n: return -1 d[val]=True l1[k]=val k+=2 return ' '.join(map(str,l1)) for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) print(revolt(n,l))
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import bisect import sys import math input=sys.stdin.readline t=int(input()) #t=1 for _ in range(t): n=int(input()) #a,b=map(int,input().split()) l=list(map(int,input().split())) a=[0]*(2*n) j=0 ind=[0]*(2*n+1) for i in range(n): ind[l[i]]=1 for i in range(n): a[j]=l[i] j+=2 j=1 while j<2*n: for i in range(a[j-1]+1,2*n+1): if ind[i]==0: ind[i]=1 a[j]=i break j+=2 ch=1 for i in range(2*n): if a[i]==0: ch=0 if ch==0: print(-1) else: print(*a)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { int T; cin >> T; while (T--) { int n; cin >> n; vector<int> a(n), ans(2 * n), used(2 * n + 5); for (int i = 0; i < n; i++) { cin >> a[i]; used[a[i]] = 1; ans[i * 2] = a[i]; } for (int i = 1; i < 2 * n; i += 2) { for (int j = ans[i - 1] + 1; j <= 2 * n; j++) if (!used[j]) { ans[i] = j; used[j] = 1; break; } } bool bad = false; for (int i = 0; i < 2 * n; i++) if (ans[i] == 0) { cout << "-1\n"; bad = true; break; } if (bad) continue; for (int i = 0; i < 2 * n; i++) cout << ans[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; bool cmp(pair<long long int, long long int> a, pair<long long int, long long int> b) { return a.first < b.first; } bool check(int a) { int sum = 0; while (a > 0) { sum += (a % 10); a /= 10; } if (sum == 10) return true; return false; } bool prime[10005]; void seivertexe() { memset(prime, 1, sizeof(prime)); prime[0] = 0; prime[1] = 0; for (long long int i = 2; i <= 10000; i++) { if (prime[i] == 1) { for (long long int j = i * i; j <= 10000; j += i) prime[j] = 0; } } } template <typename T> std::string NumberToString(T Number) { std::ostringstream ss; ss << Number; return ss.str(); } bool isPrime(long long int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long int power(long long int x, long long int y) { if (y == 0) return 1; else if (y % 2 == 0) return (power(x, y / 2) % 1000000007 * power(x, y / 2) % 1000000007) % 1000000007; else return (x % 1000000007 * (power(x, y / 2) % 1000000007 * power(x, y / 2) % 1000000007) % 1000000007) % 1000000007; } long long int factorial1(long long int n) { return (n == 1 || n == 0) ? 1 : (n % 1000000007 * factorial1(n - 1) % 1000000007) % 1000000007; } long long int fact(long long int n); long long int nCr(long long int n, long long int r) { return fact(n) % 1000000007 / ((fact(r) % 1000000007 * fact(n - r) % 1000000007)) % 1000000007; } long long int fact(long long int n) { long long int res = 1; for (int i = 2; i <= n; i++) res = (res * i) % 1000000007; return res % 1000000007; } void solve() {} int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int arr[n + 1]; map<long long int, long long int> mp1, mp2, mp3; for (long long int i = 1; i <= n; i++) { cin >> arr[i]; mp1[arr[i]]++; } long long int k = 0; long long int flag = 0; long long int arr1[2 * n + 2]; for (long long int i = 1; i <= n; i++) { long long int x = arr[i]; if (x > 2 * n) { flag = 1; cout << -1 << "\n"; break; } if (mp2[x] > 0) { flag = 1; cout << -1 << "\n"; break; } arr1[k++] = arr[i]; long long int y = arr[i] + 1; while (mp1[y] != 0 || mp2[y] != 0) { y++; } if (y > 2 * n) { flag = 1; cout << -1 << "\n"; break; } else { mp2[y]++; mp2[x]++; arr1[k++] = y; } } if (!flag) { for (long long int i = 0; i < 2 * n; i++) { cout << arr1[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; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t, n, last; cin >> t; bool can; for (int u = 0; u < t; ++u) { cin >> n; vector<int> a(2 * n + 1, 0); vector<int> c(2 * n + 1, 0); last = 1; vector<pair<int, int>> b(n); for (int i = 0; i < n; ++i) { cin >> b[i].first; ++c[b[i].first]; b[i].second = i + 1; } can = true; for (int i = 0; i < n && can; ++i) { last = 1; while (can && (c[last] > 0 || b[i].first > last)) { ++last; if (last > 2 * n) can = false; } a[b[i].second * 2 - 1] = b[i].first; a[b[i].second * 2] = last; c[last] = 1; } if (can) { for (int i = 1; i <= 2 * n; ++i) { cout << a[i] << " "; } } else { cout << -1; } cout << '\n'; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class ll { 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; } } static final int MAXN = 10000001; // stores smallest prime factor for every number static int spf[] = new int[MAXN]; // Calculating SPF (Smallest Prime Factor) for every // number till MAXN. // Time Complexity : O(nloglogn) static void sieve() { spf[1] = 1; for (int i=2; i<MAXN; i++) // marking smallest prime factor for every // number to be itself. spf[i] = i; // separately marking spf for every even // number as 2 for (int i=4; i<MAXN; i+=2) spf[i] = 2; for (int i=3; i*i<MAXN; i++) { // checking if i is prime if (spf[i] == i) { // marking SPF for all numbers divisible by i for (int j=i*i; j<MAXN; j+=i) // marking spf[j] if it is not // previously marked if (spf[j]==j) spf[j] = i; } } } // A O(log n) function returning primefactorization // by dividing by smallest prime factor at every step static int getFactorization(int x) { int c=0; while (x != 1) { c++; x = x / spf[x]; } return c; } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static void ruffleSort(long[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { long oi=r.nextInt(n), temp=a[i]; a[i]=a[(int)oi]; a[(int)oi]=temp; } Arrays.sort(a); } static void ruffleSort(int[] a) { int n=a.length; Random r=new Random(); for (int i=0; i<a.length; i++) { int oi=r.nextInt(n), temp=a[i]; a[i]=a[oi]; a[oi]=temp; } Arrays.sort(a); } public static int findIndex(long arr[], long t) { // if array is Null if (arr == null) { return -1; } // find length of array int len = arr.length; int i = 0; // traverse in the array while (i < len) { // if the i-th element is t // then return the index if (arr[i] == t) { return i; } else { i = i + 1; } } return -1; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } // method to return LCM of two numbers static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } public static int[] swap(int a[], int left, int right) { // Swap the data int temp = a[left]; a[left] = a[right]; a[right] = temp; // Return the updated array return a; } public static int[] reverse(int a[], int left, int right) { // Reverse the sub-array while (left < right) { int temp = a[left]; a[left++] = a[right]; a[right--] = temp; } // Return the updated array return a; } public static int[] findNextPermutation(int a[]) { int last = a.length - 2; // find the longest non-increasing suffix // and find the pivot while (last >= 0) { if (a[last] < a[last + 1]) { break; } last--; } // If there is no increasing pair // there is no higher order permutation if (last < 0) return a; int nextGreater = a.length - 1; // Find the rightmost successor to the pivot for (int i = a.length - 1; i > last; i--) { if (a[i] > a[last]) { nextGreater = i; break; } } // Swap the successor and the pivot a = swap(a, nextGreater, last); // Reverse the suffix a = reverse(a, last + 1, a.length - 1); // Return true as the next_permutation is done return a; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static double pow(double p,double tt) { double ii,q,r; q=1; r=p; while(tt>1) { for(ii=1;2*ii<=tt;ii*=2) p*=p; tt-=ii; q*=p; p=r; } if(tt==1) q*=r; return q; } static int factorial(int n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } public static int primeFactors(int n) { int c=0; // Print the number of 2s that divide n while (n%2==0) { c++; n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n while (n%i == 0) { c++; n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n > 2) c++; return c; } static void PrimeList(){ int i,j; int sieve[]=new int[100001]; for(i=2;i*i<=100000;i++) { if(sieve[i]==0) { for(j=i*i;j<=100000;j+=i) sieve[j]=1; } } ArrayList<Integer> primes=new ArrayList<>(); for(i=2;i<=100000;i++) { if(sieve[i]==0) primes.add(i); } } public static void main(String[] args) { //try //{ FastReader d=new FastReader(); int t,i,j,c,z,k,l,r,n,q; int mod = (int) 1e9 + 7; int Inf=Integer.MAX_VALUE; int negInf=Integer.MIN_VALUE; t=d.nextInt(); //t=1; String s; //char ch,ch1,ch2,ch3; long ans; while(t-->0) { z=c=0; ans=0l; n=d.nextInt(); int a[]=new int[n]; for(i=0;i<n;i++) a[i]=d.nextInt(); int b[]=new int[2*n]; j=0; int num[]=new int[2*n+1]; for(i=0;i<2*n+1;i++) num[i]=i; for(i=0;i<n;i++) num[a[i]]=0; for(i=0;i<n;i++) { b[j]=a[i]; for(k=a[i];k<=2*n;k++) { if(num[k]!=0) { b[j+1]=num[k]; num[k]=0; z=1; break; } } if(z==0) { c=1; break; } else { j+=2; z=0; } } if(c==1) System.out.println(-1); else { for(i=0;i<2*n;i++) System.out.print(b[i]+" "); System.out.println(); } //dont need extra d.nextLine() } /* }catch(Exception e) { System.out.println(0); }*/ } }
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 mi():return map(int,input().split()) def li():return list(mi()) def ii():return int(input()) def si():return input() t=ii() while(t): t-=1 n=ii() a=li() b=a[:] b.sort() a1=[] l=0 for i in range(1,2*n+1): if(l<len(b) and i==b[l]): l+=1 else: a1.append(i) f=0 m={} for i in range(n): if(b[i]>a1[i]): f=1 m[b[i]]=a1[i] if(f): print('-1') else: for i in a: print(i,end=" ") for j in range(n): if(a1[j]>i): print(a1[j],end=" ") a1[j]=0 break print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) if 2*n in l: print(-1) else: t=[] x=[0]*(2*n+1) f=0 for i in l: x[i]=1 for i in l: if 0 not in x[i:]: f=1 break else: p=x[i:].index(0) t.extend([i,p+i]) x[p+i]=1 if f==1: print(-1) else: for i in t: 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
for _ in range(int(input())): n = int(input()) b = list(map(int, input().split())) a = [0 for i in range(2*n)] for i in range(n): a[2*i] = b[i] rec = {} # for i in range(n): # rec[b[i]] = i avail = [1 for i in range(1, 2*n + 1)] for i in b: avail[i - 1] = 0 glob = 1 for i in range(n): pos = 2*i + 1 flag = 0 for j in range(a[2*i], 2*n): if avail[j] == 1: flag = 1 a[pos] = j + 1 avail[j] = 0 break if flag == 0: glob = 0 break if glob == 0: print(-1) else: for i in a: 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
te = int(input()) while te > 0: te -= 1 n = int(input()) B = list(map(int, input().split())) D = {} for i in B: D[i] = 1 ans = [] i = 0 while i < n: ans.append(B[i]) k = B[i] while k < 2*n: k += 1 if D.get(k) is None: D[k] = 1 ans.append(k) break i += 1 if len(ans) == 2*n: for i in ans: print(i, end=" ") else: print(-1) # print(D)
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 int full = 205; int n; int b[full], a[full]; bool f[full]; int find(int s) { int x = 0; for (int i = s + 1; i <= 2 * n; i++) if (f[i] == false) { f[i] = true; x = i; break; } return x; } void process() { for (int i = 1; i <= 2 * n; i++) f[i] = false; for (int i = 1; i <= n; i++) { a[2 * i - 1] = b[i]; f[b[i]] = true; } int x; for (int i = 1; i <= n; i++) { x = find(b[i]); if (x == 0) { cout << "-1" << endl; return; } a[i * 2] = x; } for (int i = 1; i <= 2 * n; i++) cout << a[i] << " "; cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long int t; cin >> t; while (t--) { cin >> n; for (int i = 1; i <= n; i++) cin >> b[i]; process(); } 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 javax.swing.*; import java.awt.desktop.SystemSleepEvent; import java.util.*; import java.io.*; public class Main { public static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } } public class pair { int a; int b; pair(int p, int q) { a = p; b = q; } public int hashCode() { return new Integer(a).hashCode() * 31 + new Integer(b).hashCode(); } } public static int gcd(int a, int b) { if (a == 1 || b == 1) return 1; if (a == 0) return b; if (b == 0) return a; return gcd(b % a, a); } public static void main(String[] args) { FastReader s = new FastReader(); int t=s.nextInt(); while(t>0) { int n=s.nextInt(); int[] arr=new int[n]; int[] arr1=new int[2*n]; for(int i=0;i<n;i++) { arr[i] = s.nextInt(); } for(int i=0;i<2*n;i++) arr1[i]=i+1; int[] dif=new int[1000]; int c=0; for(int i=0;i<2*n;i++) { int temp = arr1[i]; if(!find(arr,temp)) { dif[c] = temp; c++; } } // System.out.println(c); int min=arr[0]; for(int i=0;i<n;i++) { if(arr[i]<=min) min=arr[i]; } int av=greater(dif,min); //System.out.println(); //System.out.println(av); if(av<n) { System.out.println(-1); } else { int[] visited=new int[n]; int[] ans=new int[2*n]; int temp1=0; int ct=0; int flag=0; for(int i=0;i<2*n-1;i++) { ans[i] = arr[ct]; int x = ans[i]; for(int j=0;j<c;j++) { if(dif[j]>x && visited[j]!=1) { flag=1; temp1=j; break; } } if(flag==0) break; else { ans[i+1] = dif[temp1]; visited[temp1]=1; i++; ct++; } } if(flag==0) System.out.println(-1); else { int flag1=0; int[] b = new int[1000]; for(int i=0;i<2*n;i++) { b[ans[i]]++; if(b[ans[i]]>1) { flag1=1; break; } } if(flag1==1) System.out.println(-1); else { for (int i = 0; i < 2 * n; i++) { System.out.print(ans[i] + " "); } System.out.println(); } } } t--; } } public static boolean find(int[] arr, int x) { int n=arr.length; for(int i=0;i<n;i++) { if(arr[i]==x) return true; } return false; } public static int greater(int[] arr, int x) { int n=arr.length; int temp=0; for(int i=0;i<n;i++) { if(arr[i]>x) { temp=i; break; } } return n-temp; } }
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; double tick() { static clock_t oldtick; clock_t newtick = clock(); double diff = 1.0 * (newtick - oldtick) / CLOCKS_PER_SEC; oldtick = newtick; return diff; } long long gcd(long long a, long long b) { if ((a == 0) || (b == 0)) { return a + b; } return gcd(b, a % b); } long long powMod(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a) % 1000000007; a = (a * a) % 1000000007; b >>= 1; } return (res % 1000000007); } long long pow2(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a); a = (a * a); b >>= 1; } return res; } bool isPrime(long long a) { for (long long i = 3; (i * i) <= a; i += 2) { if ((a % i) == 0) return false; } if ((a != 2) && ((a % 2) == 0)) { return false; } if (a == 1) { return false; } return true; } string conLlToStr(long long a) { stringstream mystr; mystr << a; return mystr.str(); } long long conStrToLl(string st) { long long numb = 0; long long len = st.size(), i, j = 0; for (long long i = len - 1; i >= 0; i--) { numb += (pow2(10, j) * (st[i] - '0')); j++; } return numb; } long long basenoToDecimal(string st, long long basee) { long long i = 0, j, anss = 0; for (long long j = (int)st.length() - 1; j >= 0; j--) { anss += (st[j] - '0') * pow2(basee, i); i++; } return anss; } string decimalToString(long long num, long long basee) { long long i = 0, j, opop; string anss = ""; vector<string> stri; stri.push_back("0"); stri.push_back("1"); stri.push_back("2"); stri.push_back("3"); stri.push_back("4"); stri.push_back("5"); stri.push_back("6"); stri.push_back("7"); stri.push_back("8"); stri.push_back("9"); stri.push_back("A"); stri.push_back("B"); stri.push_back("C"); stri.push_back("D"); stri.push_back("E"); stri.push_back("F"); if (num == 0) { return "0"; } while (num) { opop = (num % basee); anss += stri[opop]; num /= basee; } reverse(anss.begin(), anss.end()); return anss; } long long my_sqrt(long long x) { long long y = (long long)(sqrtl((long double)x) + 0.5); while (y * y < x) { y++; } while (y * y > x) { y--; } if (y * y == x) { return y; } return -1; } long long my_crt(long long x) { long long y = (long long)(powl((long double)x, 1.0 / 3.0) + 0.5); while (y * y * y < x) { y++; } while (y * y * y > x) { y--; } if (y * y * y == x) { return y; } return -1; } const int dx[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int dy[] = {1, 1, 0, -1, -1, -1, 0, 1}; long long arr[110]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long T; cin >> T; while (T--) { long long N; cin >> N; for (long long i = 1; i <= N; i++) { cin >> arr[i]; } set<long long> se; for (long long i = 1; i <= 2 * N; i++) { se.insert(i); } for (long long i = 1; i <= N; i++) { se.erase(arr[i]); } vector<long long> ans; for (long long i = 1; i <= N; i++) { auto it = se.lower_bound(arr[i]); if (it != se.end()) { ans.push_back(arr[i]); ans.push_back(*it); se.erase(it); } else { break; } } if (ans.size() == (2 * N)) { for (long long i = 0; i <= (int)ans.size() - 1; i++) { cout << ans[i] << " "; } } else { cout << "-1"; } cout << "\n"; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import sys input = sys.stdin.readline Q = int(input()) Query = [] for _ in range(Q): N = int(input()) A = list(map(int, input().split())) Query.append((N, A)) for N, A in Query: S = set(A) C = set() for n in range(1, 2*N+1): if not n in S: C.add(n) ans = [] for a in A: to = -1 for n in range(a+1, 2*N+1): if n in C: to = n break if to == -1: ans = [] break ans.append(a) ans.append(to) C.remove(to) if not ans: 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; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n), b(2 * n); for (int &i : a) cin >> i; set<int> s; for (int i = 1; i <= 2 * n; i++) s.insert(i); for (int i = 0; i < n; i++) { b[2 * i] = a[i]; auto it = s.find(a[i]); s.erase(it); } for (int i = 0; i < n; i++) { auto it = s.upper_bound(a[i]); if (it != s.end()) { b[2 * i + 1] = *it; s.erase(it); } } if (s.size()) cout << -1 << endl; else { for (int i = 0; i < n; i++) cout << b[2 * i] << " " << b[2 * i + 1] << " "; cout << endl; } } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in range(int(input())): n=int(input()) b=[int(x) for x in input().split()] a=[] for i in range(n): a.append(b[i]) k=b[i] while(k in a or k in b):k+=1 if k>2*n:print(-1);break a.append(k) else:print(*a)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const int siz = 1e5 + 7; const int siz2 = 2e5 + 7; int t, n; int a[222]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; A: while (t--) { cin >> n; map<int, int> mp; vector<int> Ans; for (int i = 0; i < n; ++i) { cin >> a[i]; mp[a[i]]++; } if (n > 1) { for (int i = 0; i < n; ++i) { for (int j = a[i]; j <= 2 * n; ++j) { if (!mp[j]) { mp[j]++; Ans.push_back(a[i]); Ans.push_back(j); break; } } } if (Ans.size() != 2 * n) { cout << -1 << endl; goto A; } } else { if (a[0] == 1) { cout << 1 << " " << 2 << endl; goto A; } else { cout << -1 << endl; goto A; } } for (auto it : Ans) cout << it << " "; cout << endl; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.io.*; import java.util.*; import java.text.DecimalFormat; import java.math.*; public class Main { static int mod = 1000000007; /*****************code By Priyanshu *******************************************/ 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; } } // this > other return +ve // this < other return -ve // this == other return 0 public static class Pair implements Comparable<Pair> { int st; int et; public Pair(int st, int et) { this.st = st; this.et = et; } public int compareTo(Pair other) { if (this.st != other.st) { return this.st - other.st; } else { return this.et - other.et; } } } static long fastPower(long a, long b, int n) { long res = 1; while ( b > 0) { if ( (b&1) != 0) { res = (res * a % n) % n; } a = (a % n * a % n) % n; b = b >> 1; } return res; } // function to find // the rightmost set bit static int PositionRightmostSetbit(int n) { // Position variable initialize // with 1 m variable is used to // check the set bit int position = 1; int m = 1; while ((n & m) == 0) { // left shift m = m << 1; position++; } return position; } // used for swapping ith and jth elements of array // public static void swap(int[] arr, int i, int j) { // int temp = arr[i]; // arr[i] = arr[j]; // arr[j] = temp; // } public static void swap(long i, long j) { long temp = i; i = j; j = temp; } static boolean palindrome(String s) { return new StringBuilder(s).reverse().toString().equals(s); } static int countDigit(long n) { return (int)Math.floor(Math.log10(n) + 1); } static boolean isPerfectSquare(double x) { double sr = Math.sqrt(x); return ((sr * sr) == x); } public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static int nod(long l) { if(l>=1000000000000000000l) return 19; if(l>=100000000000000000l) return 18; if(l>=10000000000000000l) return 17; if(l>=1000000000000000l) return 16; if(l>=100000000000000l) return 15; if(l>=10000000000000l) return 14; if(l>=1000000000000l) return 13; if(l>=100000000000l) return 12; if(l>=10000000000l) return 11; if(l>=1000000000) return 10; if(l>=100000000) return 9; if(l>=10000000) return 8; if(l>=1000000) return 7; if(l>=100000) return 6; if(l>=10000) return 5; if(l>=1000) return 4; if(l>=100) return 3; if(l>=10) return 2; return 1; } //Arrays.sort(a,(c,d) -> Integer.compare(c[0],d[0])); public static boolean areAllBitsSet(int n ) { // all bits are not set if (n == 0) return false; // if true, then all bits are set if (((n + 1) & n) == 0) return true; // else all bits are not set return false; } public static int[] radixSort2(int[] a) { int n = a.length; int[] c0 = new int[0x101]; int[] c1 = new int[0x101]; int[] c2 = new int[0x101]; int[] c3 = new int[0x101]; for(int v : a) { c0[(v&0xff)+1]++; c1[(v>>>8&0xff)+1]++; c2[(v>>>16&0xff)+1]++; c3[(v>>>24^0x80)+1]++; } for(int i = 0;i < 0xff;i++) { c0[i+1] += c0[i]; c1[i+1] += c1[i]; c2[i+1] += c2[i]; c3[i+1] += c3[i]; } int[] t = new int[n]; for(int v : a)t[c0[v&0xff]++] = v; for(int v : t)a[c1[v>>>8&0xff]++] = v; for(int v : a)t[c2[v>>>16&0xff]++] = v; for(int v : t)a[c3[v>>>24^0x80]++] = v; return a; } public static void main( String[] args) { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); } catch (Exception e) { System.err.println("Error"); } FastReader sc = new FastReader(); int t = Integer.parseInt(sc.next()); // int t = 1; while(t-->0){ int n = sc.nextInt(); HashSet<Integer> s = new HashSet<Integer>(); int a[] = new int[n]; for (int i = 0; i<n; i++) { a[i] = sc.nextInt(); s.add(a[i]); } int ans[] = new int [2*n]; boolean c = false; for (int i = 0; i<n; i++) { int start = 2*i; int end = start +1; int val = a[i]; int next = val +1; while(s.contains(next)){ next++; } if(next > 2*n){ System.out.println("-1"); System.out.flush(); c = true; break; } ans[start] = val; ans[end] = next; s.add(next); } if(!c){ for (int i : ans ) { System.out.print(i + " "); } System.out.println(); System.out.flush(); } } } } //code By Priyanshu
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()) test_cases = [(input(),input()) for i in range(t)] def solve(test_case): n = int(test_case[0]) b = list(map(int,test_case[1].split())) c = [i for i in range(1,2*n+1) if i not in b] a = [] for i in range(n): a.append(b[i]) if c[-1] > b[i]: tmp = min([c[j] for j in range(len(c)) if c[j] > b[i]]) a.append(tmp) c.remove(tmp) else: print(-1) return print(' '.join(map(str,a))) for test_case in test_cases: solve(test_case)
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 tc in range(int(input())): n = int(input()) arr = [] d = {} line = input().split() for i in range(1, (2*n)+1): d[i] = 1 for i in range(n): arr.append(int(line[i])) d[arr[i]] = 0 # print(d) b = [] flag = 0 for i in range(n): ele = arr[i]+1 plus = 0 while(ele <= (2*n)): plus += 1 if d[ele] == 1: d[ele] = 0 break ele += 1 if ele > (2*n): flag = 1 break b.append(arr[i]+plus) if flag == 1: print("-1") continue i = 0 while(i < n): print(arr[i], end=" ") print(b[i], end=" ") i += 1 print("") # print(arr, b)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const double pie = 3.14159265358979323846; const char nl = '\n'; const char spc = ' '; template <typename Type> istream &operator>>(istream &in, vector<Type> &v) { long long int n = v.size(); for (int i = 0; i < n; i++) in >> v[i]; return in; } template <typename Type> ostream &operator<<(ostream &out, vector<Type> &v) { for (auto value : v) out << value << ' '; out << nl; return out; } bool isPrime(long long int n) { if (n < 4) return true; if (n % 2 == 0) return false; long long int i = 3; while (i * i <= n) { if (n % i == 0) return false; i += 2; } return true; } void solve() { long long int n; cin >> n; vector<long long int> b(n); vector<long long int> cnt(300, 0); for (long long int i = 0; i < n; ++i) { cin >> b[i]; cnt[b[i]] = 1; } vector<long long int> a; for (long long int i = 0; i < n; ++i) { a.emplace_back(b[i]); long long int k = b[i] + 1; while (cnt[k]) ++k; a.emplace_back(k); cnt[b[i]] = cnt[k] = 1; } for (long long int i = 1; i <= 2 * n; ++i) { if (cnt[i] != 1) { cout << "-1" << nl; return; } } cout << a; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { solve(); } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.StringTokenizer; public class C{ 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; } } static class Pair { int x; int y; Pair(int x,int y) { this.x = x; this.y = y; } } /* static class SegmentTree { public int[] seg; public int N; public int size; public SegmentTree(int n) { int h = (int)Math.ceil((double)Math.log(n)/Math.log(2)); this.size = (int)Math.pow(2,h)*2-1; seg = new int[size]; } public int update(int l, int r, int i, int val) { if(l == r && l == val) { seg[i]++; return seg[i]; } if(l > r || l == r || val > r || val < l) return seg[i]; int m = (l+r) >>> 1; int left = update(l,m,2*i+1,val); int right = update(m+1,r,2*i+2,val); seg[i] = left+right; return seg[i]; } public int getValue(int l, int r, int i, int ql, int qr) { if (l > r || ql > qr || l > qr || r < ql) return 0; if (l == r) { return seg[i]; } if (l >= ql && r <= qr) { return seg[i]; } int m = (l + r) >>> 1; return getValue(l, m, 2 * i + 1, ql, qr) + getValue(m + 1, r, 2 * i + 2, ql, qr); } } public static int gcd(int a,int b) { if(a == 0) return b; if(b == 0) return a; return gcd(b, a%b); } */ public static long rec(int l,int r,int n) { if(l > r) return 0; if(l == r) return m[l]; int mini = queryST(l,r); if(mini < l || mini > r) { throw new ArithmeticException("Out of range"); } int minv = m[mini]; long sum = (long)(mini-l+1)*(long)minv; sum += rec(mini+1,r,n); long ans = sum; sum = (long)(r-mini+1)*(long)minv; sum += rec(l,mini-1,n); //System.out.println("Added "+l+" "+r); if(ans > sum) { hm.put((long)l*n + (long)r,0); return ans; } else { hm.put((long)l*n + (long)r,1); return sum; } } static int m[],logN[]; static int st[][]; static HashMap<Long,Integer> hm = new HashMap<>(); static void makeST(int n) { logN[1] = 0; for (int i = 2; i <= n; i++) logN[i] = logN[i/2] + 1; for (int i = 1; i <= n; i++) st[i][0] = i; for (int j = 1; j < 30; j++) { for (int i = 1; i <= n; i++) { st[i][j] = st[i][j-1]; if (i+(1<<(j-1)) <= n && m[st[i+(1<<(j-1))][j-1]] < m[st[i][j]]) st[i][j] = st[i+(1<<(j-1))][j-1]; } } } static int queryST(int s, int e) { int len = e - s + 1; int l = logN[len]; int res = st[s][l]; if (m[st[e-(1<<l)+1][l]] < m[res]) res = st[e-(1<<l)+1][l]; return res; } public static void main(String[] args) { OutputStream outputStream = System.out; FastReader sc = new FastReader(); PrintWriter out = new PrintWriter(outputStream); int t = sc.nextInt(); while(t-- > 0) { int n = sc.nextInt(); int b[] = new int[n]; int map[] = new int[2*n+1]; HashSet<Integer> hs = new HashSet<>(); for(int i = 0; i < n; i++) { b[i] = sc.nextInt(); map[b[i]] = i; hs.add(b[i]); } int a[] = new int[2*n]; Arrays.fill(a,0); boolean pos = true; for(int i = 0; i < n; i++) { int v = b[i]; a[2*i] = v; for(int j = v+1; j <= 2*n; j++) { if(!hs.contains(j)) { a[2*i+1] = j; hs.add(j); break; } } if(a[2*i+1] == 0) { pos = false; break; } } if(!pos) out.println(-1); else { for(int i = 0; i < 2*n; i++) { out.print(a[i]+" "); } 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
import java.util.*; import java.io.*; public class Main{ static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(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; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } 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(); } public void flush() { writer.flush(); } } static class IOUtils { public static int[] readIntArray(InputReader in, int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = in.nextInt(); return array; } } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static int mod = (int)(1e9+7); public static long pow(long a,long b) { long ans = 1; while(b> 0) { if((b & 1)==1){ ans = (ans*a) % mod; } a = (a*a) % mod; b = b>>1; } return ans; } public static void main(String[] args) { InputReader in = new InputReader(System.in); OutputWriter out = new OutputWriter(System.out); //IOUtils io = new IOUtils(); int t = in.nextInt(); while(t-- >0) { int n = in.nextInt(); int[] arr = new int[2*n]; TreeSet<Integer> set = new TreeSet<>(); boolean res = true; for(int i=1;i<=2*n;i++) { set.add(i); } for(int i=0;i<2*n;i+=2) { arr[i] = in.nextInt(); set.remove(arr[i]); } for(int i=1;i<2*n;i+=2) { Integer val = set.higher(arr[i-1]); if(val==null) { res = false; break; } arr[i] = val; set.remove(val); } if(!res) { out.printLine("-1"); } else{ for(int i=0;i<2*n;i++) { out.print(arr[i] + " "); } out.printLine(); } } out.flush(); out.close(); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
Q = int(input()) for _ in range(Q): n = int(input()) b = list(map(int, input().split())) s = {*(range(2*n + 1))} - {*b} a = [] try: for i in b: 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
t=int(input()) def f(): n=int(input()) b=list(map(int,input().split())) a=[b[i//2] if i % 2==0 else 0 for i in range(n*2)] used = 2*n*[0]+[0] for i in a: used[i] = 1 for i in range(1,2*n,2): l = a[i-1] j=l+1 while j<=2*n and used[j]: j+=1 if j>2*n: print(-1) return a[i] = j used[j]=1 print(*a) for tt in range(t): f()
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; inline long long read() { char c = getchar(); long long f = 1, x = 0; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ '0'); c = getchar(); } return x * f; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int b[1008], cnt[1008], t; vector<int> ans; int main() { scanf("%d", &t); while (t--) { int n, flag = 0; scanf("%d", &n); for (int i = 1; i <= 2 * n; i++) cnt[i] = 0; ans.clear(); for (int i = 1; i <= n; i++) { scanf("%d", &b[i]); cnt[b[i]]++; } for (int i = 1; i <= n; i++) { flag = 0; ans.push_back(b[i]); for (int j = b[i] + 1; j <= 2 * n; j++) { if (!cnt[j]) { cnt[j]++; ans.push_back(j); flag = 1; break; } } if (!flag) { printf("-1\n"); break; } } if (!flag) continue; for (int j = 0; j < ans.size(); j++) printf("%d ", ans[j]); printf("\n"); } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
# Enter your code here. Read input from STDIN. Print output to STDOUT# =============================================================================================== # importing some useful libraries. from __future__ import division, print_function from fractions import Fraction import sys import os from io import BytesIO, IOBase from itertools import * import bisect from heapq import * from math import ceil, floor from copy import * from collections import deque, defaultdict from collections import Counter as counter # Counter(list) return a dict with {key: count} from itertools import combinations # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)] from itertools import permutations as permutate from bisect import bisect_left as bl from operator import * # If the element is already present in the list, # the left most position where element has to be inserted is returned. from bisect import bisect_right as br from bisect import bisect # If the element is already present in the list, # the right most position where element has to be inserted is returned # ============================================================================================== # fast I/O region BUFSIZE = 8192 from sys import stderr 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) # inp = lambda: sys.stdin.readline().rstrip("\r\n") # =============================================================================================== ### START ITERATE RECURSION ### from types import GeneratorType def iterative(f, stack=[]): def wrapped_func(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) continue stack.pop() if not stack: break to = stack[-1].send(to) return to return wrapped_func #### END ITERATE RECURSION #### ########################### # Sorted list class SortedList: def __init__(self, iterable=[], _load=200): """Initialize sorted list instance.""" values = sorted(iterable) self._len = _len = len(values) self._load = _load self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)] self._list_lens = [len(_list) for _list in _lists] self._mins = [_list[0] for _list in _lists] self._fen_tree = [] self._rebuild = True def _fen_build(self): """Build a fenwick tree instance.""" self._fen_tree[:] = self._list_lens _fen_tree = self._fen_tree for i in range(len(_fen_tree)): if i | i + 1 < len(_fen_tree): _fen_tree[i | i + 1] += _fen_tree[i] self._rebuild = False def _fen_update(self, index, value): """Update `fen_tree[index] += value`.""" if not self._rebuild: _fen_tree = self._fen_tree while index < len(_fen_tree): _fen_tree[index] += value index |= index + 1 def _fen_query(self, end): """Return `sum(_fen_tree[:end])`.""" if self._rebuild: self._fen_build() _fen_tree = self._fen_tree x = 0 while end: x += _fen_tree[end - 1] end &= end - 1 return x def _fen_findkth(self, k): """Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`).""" _list_lens = self._list_lens if k < _list_lens[0]: return 0, k if k >= self._len - _list_lens[-1]: return len(_list_lens) - 1, k + _list_lens[-1] - self._len if self._rebuild: self._fen_build() _fen_tree = self._fen_tree idx = -1 for d in reversed(range(len(_fen_tree).bit_length())): right_idx = idx + (1 << d) if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]: idx = right_idx k -= _fen_tree[idx] return idx + 1, k def _delete(self, pos, idx): """Delete value at the given `(pos, idx)`.""" _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len -= 1 self._fen_update(pos, -1) del _lists[pos][idx] _list_lens[pos] -= 1 if _list_lens[pos]: _mins[pos] = _lists[pos][0] else: del _lists[pos] del _list_lens[pos] del _mins[pos] self._rebuild = True def _loc_left(self, value): """Return an index pair that corresponds to the first position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins lo, pos = -1, len(_lists) - 1 while lo + 1 < pos: mi = (lo + pos) >> 1 if value <= _mins[mi]: pos = mi else: lo = mi if pos and value <= _lists[pos - 1][-1]: pos -= 1 _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value <= _list[mi]: idx = mi else: lo = mi return pos, idx def _loc_right(self, value): """Return an index pair that corresponds to the last position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins pos, hi = 0, len(_lists) while pos + 1 < hi: mi = (pos + hi) >> 1 if value < _mins[mi]: hi = mi else: pos = mi _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value < _list[mi]: idx = mi else: lo = mi return pos, idx def add(self, value): """Add `value` to sorted list.""" _load = self._load _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len += 1 if _lists: pos, idx = self._loc_right(value) self._fen_update(pos, 1) _list = _lists[pos] _list.insert(idx, value) _list_lens[pos] += 1 _mins[pos] = _list[0] if _load + _load < len(_list): _lists.insert(pos + 1, _list[_load:]) _list_lens.insert(pos + 1, len(_list) - _load) _mins.insert(pos + 1, _list[_load]) _list_lens[pos] = _load del _list[_load:] self._rebuild = True else: _lists.append([value]) _mins.append(value) _list_lens.append(1) self._rebuild = True def discard(self, value): """Remove `value` from sorted list if it is a member.""" _lists = self._lists if _lists: pos, idx = self._loc_right(value) if idx and _lists[pos][idx - 1] == value: self._delete(pos, idx - 1) def remove(self, value): """Remove `value` from sorted list; `value` must be a member.""" _len = self._len self.discard(value) if _len == self._len: raise ValueError('{0!r} not in list'.format(value)) def pop(self, index=-1): """Remove and return value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) value = self._lists[pos][idx] self._delete(pos, idx) return value def bisect_left(self, value): """Return the first index to insert `value` in the sorted list.""" pos, idx = self._loc_left(value) return self._fen_query(pos) + idx def bisect_right(self, value): """Return the last index to insert `value` in the sorted list.""" pos, idx = self._loc_right(value) return self._fen_query(pos) + idx def count(self, value): """Return number of occurrences of `value` in the sorted list.""" return self.bisect_right(value) - self.bisect_left(value) def __len__(self): """Return the size of the sorted list.""" return self._len def __getitem__(self, index): """Lookup value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) return self._lists[pos][idx] def __delitem__(self, index): """Remove value at `index` from sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) self._delete(pos, idx) def __contains__(self, value): """Return true if `value` is an element of the sorted list.""" _lists = self._lists if _lists: pos, idx = self._loc_left(value) return idx < len(_lists[pos]) and _lists[pos][idx] == value return False def __iter__(self): """Return an iterator over the sorted list.""" return (value for _list in self._lists for value in _list) def __reversed__(self): """Return a reverse iterator over the sorted list.""" return (value for _list in reversed(self._lists) for value in reversed(_list)) def __repr__(self): """Return string representation of sorted list.""" return 'SortedList({0})'.format(list(self)) # =============================================================================================== # some shortcuts mod = 1000000007 def testcase(t): for p in range(t): solve() def pow(x, y, p): res = 1 # Initialize result x = x % p # Update x if it is more , than or equal to p if (x == 0): return 0 while (y > 0): if ((y & 1) == 1): # If y is odd, multiply, x with result res = (res * x) % p y = y >> 1 # y = y/2 x = (x * x) % p return res from functools import reduce def factors(n): return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))) def gcd(a, b): if a == b: return a while b > 0: a, b = b, a % b return a # discrete binary search # minimise: # def search(): # l = 0 # r = 10 ** 15 # # for i in range(200): # if isvalid(l): # return l # if l == r: # return l # m = (l + r) // 2 # if isvalid(m) and not isvalid(m - 1): # return m # if isvalid(m): # r = m + 1 # else: # l = m # return m # maximise: # def search(): # l = 0 # r = 10 ** 15 # # for i in range(200): # # print(l,r) # if isvalid(r): # return r # if l == r: # return l # m = (l + r) // 2 # if isvalid(m) and not isvalid(m + 1): # return m # if isvalid(m): # l = m # else: # r = m - 1 # return m ##############Find sum of product of subsets of size k in a array # ar=[0,1,2,3] # k=3 # n=len(ar)-1 # dp=[0]*(n+1) # dp[0]=1 # for pos in range(1,n+1): # dp[pos]=0 # l=max(1,k+pos-n-1) # for j in range(min(pos,k),l-1,-1): # dp[j]=dp[j]+ar[pos]*dp[j-1] # print(dp[k]) def prefix_sum(ar): # [1,2,3,4]->[1,3,6,10] return list(accumulate(ar)) def suffix_sum(ar): # [1,2,3,4]->[10,9,7,4] return list(accumulate(ar[::-1]))[::-1] def N(): return int(inp()) dx = [0, 0, 1, -1] dy = [1, -1, 0, 0] def YES(): print("YES") def NO(): print("NO") def Yes(): print("Yes") def No(): print("No") # ========================================================================================= from collections import defaultdict def numberOfSetBits(i): i = i - ((i >> 1) & 0x55555555) i = (i & 0x33333333) + ((i >> 2) & 0x33333333) return (((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) & 0xffffffff) >> 24 class MergeFind: def __init__(self, n): self.parent = list(range(n)) self.size = [1] * n self.num_sets = n # self.lista = [[_] for _ in range(n)] def find(self, a): to_update = [] while a != self.parent[a]: to_update.append(a) a = self.parent[a] for b in to_update: self.parent[b] = a return self.parent[a] def merge(self, a, b): a = self.find(a) b = self.find(b) if a == b: return if self.size[a] < self.size[b]: a, b = b, a self.num_sets -= 1 self.parent[b] = a self.size[a] += self.size[b] # self.lista[a] += self.lista[b] # self.lista[b] = [] def set_size(self, a): return self.size[self.find(a)] def __len__(self): return self.num_sets def lcm(a, b): return abs((a // gcd(a, b)) * b) # # # to find factorial and ncr # tot = 100005 # mod = 10**9 + 7 # fac = [1, 1] # finv = [1, 1] # inv = [0, 1] # # for i in range(2, tot + 1): # fac.append((fac[-1] * i) % mod) # inv.append(mod - (inv[mod % i] * (mod // i) % mod)) # finv.append(finv[-1] * inv[-1] % mod) def comb(n, r): if n < r: return 0 else: return fac[n] * (finv[r] * finv[n - r] % mod) % mod def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def out(var): sys.stdout.write(str(var)) # for fast output, always take string def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def fsep(): return map(float, inp().split()) def nextline(): out("\n") # as stdout.write always print sring. def arr1d(n, v): return [v] * n def arr2d(n, m, v): return [[v] * m for _ in range(n)] def arr3d(n, m, p, v): return [[[v] * p for _ in range(m)] for i in range(n)] def ceil(a, b): return (a + b - 1) // b # co-ordinate compression # ma={s:idx for idx,s in enumerate(sorted(set(l+r)))} # mxn=100005 # lrg=[0]*mxn # for i in range(2,mxn-3): # if (lrg[i]==0): # for j in range(i,mxn-3,i): # lrg[j]=i def solve(): n=N() ar=lis() s=set(ar) temp=[] for i in range(1,2*n+1): if i not in s: temp.append(i) ans=[] taken=set() for i in ar: ans.append(i) for j in temp: if j>i and j not in taken: ans.append(j) taken.add(j) break if (sorted(ans)==list(range(1,2*n+1))): print(*ans) else: print(-1) # solve() testcase(int(inp()))
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
import java.io.*; import java.util.Arrays; public class C { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringBuilder sb = new StringBuilder(); int t = Integer.parseInt(br.readLine()); for (int i = 0; i < t; i++) { int n = Integer.parseInt(br.readLine()); boolean[] check = new boolean[n * 2 + 1]; String[] strArr = br.readLine().split(" "); int[] bArr = new int[n]; for (int j = 0; j < n; j++) { bArr[j] = Integer.parseInt(strArr[j]); check[bArr[j]] = true; } boolean isPossible = true; int[] aArr = new int[2 * n]; for (int j = 0; j < n; j++) { aArr[j * 2] = bArr[j]; int tmp = aArr[j * 2]; do{ tmp++; if(tmp > n * 2){ isPossible = false; break; } } while(check[tmp]); if(tmp > n * 2){ isPossible = false; break; } aArr[j * 2 + 1] = tmp; check[tmp] = true; } if(isPossible){ for (int j = 0; j < n * 2; j++) { sb.append(aArr[j]).append(" "); } }else{ sb.append(-1); } sb.append("\n"); } bw.write(sb.toString()); bw.flush(); bw.close(); br.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
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<bool> a(2 * n + 1); vector<int> res(2 * n); bool good = true; for (int i = 0; i < n; i++) { int k; cin >> k; if (a[k] == false) { a[k] = true; res[2 * i] = k; } else { good = false; break; } } if (!good) { cout << -1 << endl; continue; } for (int i = 1; i < 2 * n; i += 2) { int ptr = -1; for (int j = res[i - 1] + 1; j <= 2 * n; j++) { if (!a[j]) { ptr = j; break; } } if (ptr == -1) { good = false; break; } a[ptr] = true; res[i] = ptr; } if (!good) { cout << -1 << endl; continue; } for (int i = 0; i < 2 * n; i++) cout << res[i] << ' '; cout << endl; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.*; import java.util.*; public class code { static PrintWriter pw; static boolean[] visit; static ArrayList<Pair>[] adj; static ArrayList<Integer> list; static long max=01; static void dfs(int i, long c) { visit[i]=true; max=Math.max(c, max); for(Pair p: adj[i]) { if(!visit[p.x-1]) { dfs(p.x-1, c+1l*p.y); } } } public static double dist(Pair a, Pair b) { return Math.sqrt((a.x-b.x)*(a.x-b.x)+ (a.y-b.y)*(a.y-b.y)); } static int large(int x, int[] arr) { int ans=-1; int start=0,end=arr.length-1; while(start<=end) { int mid=(start+end)/2; if(arr[mid]<=x) { start=mid+1; }else { ans=mid; end=mid-1; } } return ans; } static boolean pf(int n) { return Math.abs(Math.sqrt(n)*Math.sqrt(n)-n)<=1e-8; } static boolean sign(int n) { return n>0; } public static void main(String[] args) throws Exception { Scanner sc= new Scanner(System.in); pw = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int[] arr=new int[n]; HashSet<Integer> taken=new HashSet<Integer>(); for(int i=0; i<n; i++) { arr[i]=sc.nextInt(); taken.add(arr[i]); } int[] arr2=new int[n]; int j=0; for(int i=1; i<=2*n; i++) { if(!taken.contains(i)) { arr2[j]=i; j++; } } boolean valid=true; ArrayList<Integer> list=new ArrayList<Integer>(); for(int i=0; i<n-1; i++) { int idx=large(arr[i],arr2); if(idx==-1) { valid=false; break; }else { list.add(arr[i]); list.add(arr2[idx]); arr2[idx]=0; Arrays.sort(arr2); } } int idx=large(arr[n-1],arr2); if(idx==-1) { valid=false; }else { list.add(arr[n-1]); list.add(arr2[idx]); } if(!valid) { pw.println(-1); }else { for(int i=0; i<2*n-1; i++) { pw.print(list.get(i)+" "); } pw.println(list.get(2*n-1)); } } pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextInt(); return arr; } public Integer[] nextsort(int n) throws IOException{ Integer[] arr=new Integer[n]; for(int i=0; i<n; i++) arr[i]=nextInt(); return arr; } public boolean ready() throws IOException { return br.ready(); } } static class Pair implements Comparable<Pair>{ int x; int y; public Pair(int a, int b) { this.x=a; this.y=b; } public int compareTo(Pair other) { return this.y-other.y; } } static class Triple implements Comparable<Triple>{ int x; int y; boolean d; public Triple(int a, int b, boolean t) { this.x=a; this.y=b; this.d=t; } public int compareTo(Triple other) { if(this.d && !other.d) { return 1; }else if(!this.d && other.d) { return -1; }else { return this.y-other.y; } } } }
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
// practice with kaiboy import java.io.*; import java.util.*; public class CF1315C extends PrintWriter { CF1315C() { super(System.out); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1315C o = new CF1315C(); o.main(); o.flush(); } void main() { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] bb = new int[n]; boolean[] used = new boolean[n * 2 + 1]; for (int i = 0; i < n; i++) { int b = sc.nextInt(); bb[i] = b; used[b] = true; } int[] aa = new int[n * 2]; boolean yes = true; for (int i = 0; i < n; i++) { int b = bb[i]; int c = b + 1; while (c <= n * 2 && used[c]) c++; if (c > n * 2) { yes = false; break; } used[c] = true; aa[i << 1] = b; aa[i << 1 | 1] = c; } if (yes) { for (int i = 0; i < n * 2; i++) print(aa[i] + " "); println(); } else 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.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class Main 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; } public static void main(String args[]) throws Exception { new Thread(null, new Main(),"Main",1<<27).start(); } public void run() { InputReader sc = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); int b[] = new int[n]; int a[] = new int[2*n]; int k = 0; boolean f = false; HashSet<Integer> hs = new HashSet<Integer>(); for(int i=0;i<n;i++){ b[i] = sc.nextInt(); if(b[i]==2*n) f = true; hs.add(b[i]); a[k] = b[i]; k += 2; } if(f){ System.out.println(-1); continue; } for(int i=1;i<2*n;i+=2){ f = false; for(int j=a[i-1]+1;j<=2*n;j++){ if(!hs.contains(j)){ a[i] = j; hs.add(j); f = true; break; } } if(!f){ break; } } if(!f){ System.out.println(-1); } else{ for(int i=0;i<2*n;i++){ if(i==2*n-1) System.out.print(a[i]); else System.out.print(a[i] + " "); } System.out.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
#include <bits/stdc++.h> using namespace std; inline long long gcd(long long a, long long b) { if (a % b == 0) return b; else { return gcd(b, a % b); } } inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); } inline long long gaus(long long a, long long b) { return (a + b) * (b - a + 1) / 2; } template <class T> ostream& operator<<(ostream& os, vector<T> v) { os << "["; int cnt = 0; for (auto vv : v) { os << vv; if (++cnt < v.size()) os << ","; } return os << "]"; } template <class L, class R> ostream& operator<<(ostream& os, map<L, R> v) { os << "["; int cnt = 0; for (auto vv : v) { os << vv; if (++cnt < v.size()) os << ","; } return os << "]"; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> p) { return os << "(" << p.first << "," << p.second << ")"; } int v[201]; void solve() { int t; cin >> t; while (t--) { int n; cin >> n; memset(v, 0, sizeof(v)); vector<int> a(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; v[a[i]] = 1; } bool tof = true; vector<int> ans; for (int i = 1; i <= n; i++) { for (int j = 1; j <= 2 * n; j++) { if (v[j] == 0) { if (a[i] < j && min(a[i], j) == a[i]) { ans.push_back(a[i]); ans.push_back(j); v[j] = 1; break; } } } } for (int i = 1; i <= 2 * n; i++) { if (v[i] == 0) tof = false; } if (tof) { for (int i = 0; i < ans.size(); i++) cout << ans[i] << " "; cout << '\n'; } else cout << -1 << '\n'; } } int main() { cin.tie(0), ios_base::sync_with_stdio(false); 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
#include <bits/stdc++.h> using namespace std; signed main() { long long int t = 1; cin >> t; while (t--) { long long int n; cin >> n; vector<long long int> b(n); for (auto &x : b) cin >> x; vector<bool> use(2 * n + 1, 0); vector<long long int> a(2 * n + 1, 0); for (auto x : b) use[x] = 1; bool ok = 1; for (long long int i = 1; i <= n; i++) { a[2 * i - 1] = b[i - 1]; use[b[i - 1]] = 1; bool f = 0; for (long long int j = b[i - 1] + 1; j <= 2 * n; j++) { if (use[j] == 0) { a[2 * i] = j; use[j] = 1; f = 1; break; } } if (!f) { ok = 0; break; } } if (!ok) cout << -1 << endl; else { for (long long int i = 1; i <= 2 * n; i++) cout << a[i] << " "; cout << endl; } } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; long long t, n, m, k, ccnt, pos = 0, sum = 0, minn2 = INT_MAX, sum2, cnt2 = 0, cnt3 = 0, cnt4 = 0, cnt5 = 0, cnt6 = 0, cnt7 = 0, vis[100005], x1, y7, x2, y2, x3, y3, aaa, bbb; long long a[1000005], b[1000005], c[1000005], c3[1000005], c4[1000005], kk[1000005], cnt = 0, x, y, z, d = 0, minn = LONG_LONG_MAX, maxx = -LONG_LONG_MAX; long long pos1 = 3, pos2 = 3, tem, tem2, l, r, ans = 0, ans2, mod = 1000000007, tw[100005], th[105], dd, ee, out[1000005], dp[5005][5005]; string s1, s2, s; char cc[1005][1005], ch; int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); tw[0] = 1; for (int i = 1; i <= 55; i++) { tw[i] = tw[i - 1] * 2; } cin >> t; while (t--) { d = 1; cin >> n; for (int i = 0; i <= 2 * n; i++) { b[i] = 0; } for (int i = 0; i < n; i++) { cin >> a[i]; b[a[i]] = 1; } for (int i = 0; i < n; i++) { c[2 * i] = a[i]; ans = -1; for (int j = a[i] + 1; j <= 2 * n; j++) { if (b[j] == 0) { ans = j; break; } } if (ans == -1) { d = 0; break; } else { c[2 * i + 1] = ans; b[ans] = 1; } } if (d) { for (int i = 0; i < 2 * n; i++) { cout << c[i] << " "; } cout << '\n'; } 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
t=int(input()) for _ in range(t): n=int(input()) b=[int(x) for x in input().split()] v=set(list(range(1,2*n+1))) for x in b: v.remove(x) l=list(v) l.sort() ans=[-1 for _a in range(2*n)] ok=True for i in range(n): ans[2*i]=b[i] ok=False for j in range(len(l)): if l[j]>b[i]: ans[2*i+1]=l[j] l.pop(j) ok=True break if ok==False: break #impossible. could not find larger number if ok==False: print(-1) else: print(' '.join([str(x) for x in 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()) arr = list(map(int,input().split())) s = set() for i in range(1,2*n +1): s.add(i) for i in arr: s.discard(i) a = [] b = True for i in arr: if i+1>2*n: b = False else: if i+1 in s: a+=[i+1] s.discard(i+1) else: c = False for t in s: if t>i: a+=[t] s.discard(t) c = True break if not c: b = False break if not b: print(-1) else: for i in range(n): print(arr[i],a[i],end=" ")
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import sys from collections import defaultdict, deque inp = sys.stdin.readline read = lambda: list(map(int, inp().split())) def a(): ans = "" for _ in range(read()[0]): a, b, x, y = read() x += 1 y += 1 ans += str(max(((a-x)*(b)), ((x-1)*(b)), ((a)*(b-y)), ((a)*(y-1)))) + "\n" print(ans) def c(): # ans = "" for _ in range(read()[0]): flag = 0 n = int(inp()) dic = dict() for ind, elem in enumerate(read()): dic[elem] = (ind*2, ind*2+1) arr = [None]*(2*n) left = set(i for i in range(1, 2*n+1)).difference(dic.keys()) for i in dic.keys(): elem = dic[i] mins = [y for y in left if y > i] # print(mins) if mins:#len(mins) > 0: sup = min(mins) arr[elem[0]], arr[elem[1]] = i, sup left.remove(sup) else: flag = 1 break if flag: print("-1") else: print(*arr) # flag = 0 # n = int(inp()) # dic = dict()#{elem : (2*next(ind), 2*ind+1) for elem in read()} # for ind, elem in enumerate(read()): # dic[elem] = (ind*2, ind*2+1) # ind += 1 # left = deque(set(i for i in range(1, 2*n+1)).difference(dic.keys())) # # left.sort() # # left = (left) # arr = [None]*(2*n) # keys = list(dic.keys()) # keys.sort() # for i in keys: # t = i # while not (t+1 in left): # t += 1 # if t == len(left): # flag = 1 # break # if flag: # arr[dic[i][0]], arr[dic[i][1]] = i, left.popleft() # if i < left[0]: # arr[dic[i][0]], arr[dic[i][1]] = i, left.popleft() # else: # flag = 1 # break # if flag: # ans += "-1\n" # else: # ans += (" ").join(map(str, arr)) + "\n" # print(ans) if __name__ == "__main__": # a() # b() c() # 1 2 3 4 5 6 7 8 # 2 1 3 6 4 7 5 8 # 1 - 1, 2 - 3 # 2 - 7, 8 - 4 # 5 - 3, 4 - 6 # 7 - 5, 6 - 9 # 8 - 9, 10 - 10 # 1 3 5 6 7 9 2 4 8 10 # 3 # 4 # 6 # 9 # 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.*; public class Restoring_Permutation { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); for(int i=0;i<a;i++) { int n=sc.nextInt(); int data[] = new int[n]; int man[] = new int[2*n]; for(int j=0;j<n;j++) { data[j] = sc.nextInt(); man[data[j]-1]=1; } //System.out.println(man[2*n-1]); if(man[2*n-1]==1) System.out.println(-1); else { int c[] = new int[n]; for(int d=0;d<n;d++) c[d]=data[d]; Arrays.sort(c); int cnt=0; for(int j=c[0]-1;j<2*n;j++) { if(man[j]==0) cnt++; } if(cnt<n) System.out.println(-1); else { int abc[] = new int[2*n]; for(int d=0;d<2*n;d++) abc[d]=man[d]; int app=0; for(int j=0;j<n;j++) { app=0; for(int k=data[j];k<2*n;k++) { if(abc[k]==0) { app++; abc[k]=1; break; } } if(app==0) break; } if(app==0) System.out.println(-1); else { for(int j=0;j<n;j++) { for(int k=data[j];k<2*n;k++) { if(man[k]==0) { System.out.print(data[j]+" "); System.out.print(k+1+" "); man[k]=1; break; } } }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
def get_rank(input): indices = list(range(len(input))) indices.sort(key=lambda x: input[x]) output = [0] * len(indices) for i, x in enumerate(indices): output[x] = i return output cases = int(raw_input().strip()) for case in range(cases): n = int(raw_input().strip()) b = map(int, raw_input().strip().split(" ")) a = [0 for i in xrange(2 * n)] for i in range(n): a[i * 2] = b[i] perm = sorted(list(set(xrange(1, 2 * n + 1)) - set(b))) rank = get_rank(b) for i in range(n): a[i * 2 + 1] = perm[rank[i]] possible = True for i in range(n): if b[i] != min(a[i * 2], a[i * 2 + 1]): possible = False break if not possible: print "-1" else: for i in range(n): min_possible = a[i * 2 + 1] min_j = i for j in range(i + 1, n): if a[j * 2 + 1] > a[i * 2] and min_possible > a[j * 2 + 1]: min_possible = a[j * 2 + 1] min_j = j if min_j != i: a[i * 2 + 1], a[min_j * 2 + 1] = a[min_j * 2 + 1], a[i * 2 + 1] print ' '.join(map(str, a))
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
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = LLONG_MAX; int main() { int t; cin >> t; while (t--) { int n, x, flag, m; cin >> n; x = 2 * n; int a[x]; for (long long i = 0; i < (long long)x; i++) a[i] = i + 1; vector<int> s; int b[n]; for (long long i = 0; i < (long long)n; i++) { cin >> b[i]; a[b[i] - 1] = 0; } for (long long i = 0; i < (long long)n; i++) { flag = 0; s.push_back(b[i]); for (long long j = b[i]; j < (long long)x; j++) { if (a[j] > b[i]) { flag = 1; m = a[j]; a[j] = 0; break; } } if (flag == 1) s.push_back(m); else break; } if (flag == 0) cout << "-1\n"; else { for (long long i = 0; i < (long long)s.size(); i++) cout << s[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 i in range(int(input())): n=int(input()) b=list(map(int,input().split())) a=[] flag=0 for j in range(n): a.append(b[j]) k=b[j] while k in a or k in b: k+=1 if k>2*n: print(-1) flag=1 break else: a.append(k) if flag==0: 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; const int INF = 0x3f3f3f3f; const int eps = 1e-8; const long long mod = 1e9 + 7; const long long maxn = 200 + 10; const unsigned long long modd = 212370440130137957ll; const long long Mod = 998244353; int lowbit(int x) { return x & -x; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long quick_power(long long a, long long b, long long c) { long long ans = 1; a = a % c; while (b) { if (b & 1) ans = (ans * a) % c; b = b >> 1; a = (a * a) % c; } return ans; } bool isprime(int x) { int flag = 1; for (int i = 2; i * i <= x; i++) { if (x % i == 0) { flag = 0; break; } } if (flag) return 1; else return 0; } int t; int b[maxn]; int flag[maxn]; int flag1[maxn]; int a[maxn]; int ans[maxn]; int maxx; int minn; int main(int argc, char const *argv[]) { cin >> t; while (t--) { int n; cin >> n; minn = INF; maxx = 0; memset(flag, 0, sizeof(flag)); memset(flag1, 0, sizeof(flag1)); for (int i = 1; i <= n; i++) { cin >> b[i]; flag[b[i]] = 1; maxx = max(maxx, b[i]); minn = min(minn, b[i]); } if (maxx == 2 * n || minn != 1) { cout << -1 << endl; continue; } int k = 1; for (int i = 1; i <= 2 * n; i++) { if (!flag[i]) { a[k] = i; k++; } } sort(a + 1, a + 1 + n); int tmp = 1; int flag4 = 0; for (int i = 1; i <= n; i++) { int flag2 = 0; ans[tmp++] = b[i]; for (int j = 1; j <= n; j++) { if (a[j] > b[i] && !flag1[a[j]]) { ans[tmp++] = a[j]; flag2 = 1; flag1[a[j]] = 1; break; } } if (flag2 == 0) { flag4 = 1; cout << -1; break; } } if (flag4 == 0) for (int i = 1; i <= 2 * n; i++) { cout << ans[i] << " "; } else 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()) b = list(map(int, input().split())) 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.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.*; public class TP { public static PrintWriter out; public static FastReader in; public static void main(String[] args) throws IOException { long s = System.currentTimeMillis(); in = new FastReader(); out = new PrintWriter(System.out); // out = new PrintWriter(new BufferedWriter(new FileWriter("whereami.out"))); int t = 1; t = ni(); while (t-- > 0) solve(); out.flush(); tr(System.currentTimeMillis() - s + "ms"); } private static void solve() { int n = ni(); int[] a = na(n); TreeSet<Integer> set = new TreeSet<>(); for (int i = 1; i <= 2 * n; i++) set.add(i); for (int x : a) set.remove(x); int[] ans = new int[2 * n]; int j = 0; for (int i = 0; i < n; i++) { if (set.size() == 0) break; int x = a[i]; Integer y = set.ceiling(x); if (y == null) { out.println(-1); return; } set.remove(y); ans[j] = x; ans[j + 1] = y; j += 2; } print(ans); } private static int[][] uniqCount(int[] a) { int n = a.length; int p = 0; int[][] b = new int[n][]; for (int i = 0; i < n; i++) { if (i == 0 || a[i] != a[i - 1]) b[p++] = new int[]{a[i], 0}; b[p - 1][1]++; } return Arrays.copyOf(b, p); } static int[] reverse(int a[]) { int n = a.length; int[] b = new int[n]; int j = n; for (int k : a) { b[j - 1] = k; j = j - 1; } return b; } private static void print(int[] a) { for (int x : a) out.print(x + " "); out.println(); } private static void print(long[] a) { for (long x : a) out.print(x + " "); out.println(); } public static int lowerBound(int[] a, int v) { int low = -1, high = a.length; while (high - low > 1) { int h = high + low >>> 1; if (a[h] >= v) { high = h; } else { low = h; } } return high; } private static boolean sorted(char[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) return false; } return true; } private static long lcm(long a, long b) { return a * b / gcd(a, b); } public static int[][] enumPrimesAndLPF(int n) { int[] lpf = new int[n + 1]; double lu = Math.log(n + 32); int tot = 0; int[] primes = new int[(int) ((n + 32) / lu + (n + 32) / lu / lu * 1.5)]; for (int i = 2; i <= n; i++) { lpf[i] = i; } for (int p = 2, tmp; p <= n; p++) { if (lpf[p] == p) { primes[tot++] = p; } for (int i = 0; i < tot && primes[i] <= lpf[p] && (tmp = primes[i] * p) <= n; i++) { lpf[tmp] = primes[i]; } } return new int[][]{Arrays.copyOf(primes, tot), lpf}; } static boolean isPrime(int n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } static boolean sorted(int[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i] > a[i + 1]) return false; } return true; } public static long gcd(long a, long b) { while (b != 0) { a %= b; long t = a; a = b; b = t; } return a; } private static int ni() { return in.nextInt(); } private static String ns() { return in.next(); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static long nl() { return in.nextLong(); } private float nf() { return (float) in.nextDouble(); } private static double nd() { return in.nextDouble(); } public static int[] facs(int n, int[] primes) { int[] ret = new int[9]; int rp = 0; for (int p : primes) { if (p * p > n) break; int i; i = 0; while (n % p == 0) { n /= p; i++; } if (i > 0) ret[rp++] = p; } if (n != 1) ret[rp++] = n; return Arrays.copyOf(ret, rp); } public static int[] sieveEratosthenes(int n) { if (n <= 32) { int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int i = 0; i < primes.length; i++) { if (n < primes[i]) { return Arrays.copyOf(primes, i); } } return primes; } int u = n + 32; double lu = Math.log(u); int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)]; ret[0] = 2; int pos = 1; int[] isnp = new int[(n + 1) / 32 / 2 + 1]; int sup = (n + 1) / 32 / 2 + 1; int[] tprimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31}; for (int tp : tprimes) { ret[pos++] = tp; int[] ptn = new int[tp]; for (int i = (tp - 3) / 2; i < tp << 5; i += tp) ptn[i >> 5] |= 1 << (i & 31); for (int j = 0; j < sup; j += tp) { for (int i = 0; i < tp && i + j < sup; i++) { isnp[j + i] |= ptn[i]; } } } // 3,5,7 // 2x+3=n int[] magic = { 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 }; int h = n / 2; for (int i = 0; i < sup; i++) { for (int j = ~isnp[i]; j != 0; j &= j - 1) { int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27]; int p = 2 * pp + 3; if (p > n) break; ret[pos++] = p; if ((long) p * p > n) continue; for (int q = (p * p - 3) / 2; q <= h; q += p) isnp[q >> 5] |= 1 << q; } } return Arrays.copyOf(ret, pos); } private static final boolean oj = System.getProperty("ONLINE_JUDGE") != null; private static void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() throws FileNotFoundException { br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new FileReader("whereami.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()); } } static class Pair<K, V> { private K key; private V value; public Pair(K key, V value) { this.key = key; this.value = value; } public K getKey() { return key; } public V getValue() { return value; } public void setKey(K key) { this.key = key; } public void setValue(V value) { this.value = value; } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
from sys import stdin input=stdin.readline for _ in range(int(input())): n=int(input());m=2*n;v=[0]*m a=list(map(int,input().split())) if max(a)==m:print(-1) else: k=1;d=[] for i in a:v[i-1]=1 for i in range(n): d.append(a[i]);p=a[i]-1 while v[p]!=0: p+=1 if p==m:print(-1);k=0;break if not k:break v[p]=1 d.append(p+1) if k: print(*d)
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; import java.util.TreeSet; public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); while(tc-- > 0) { int n = sc.nextInt(); TreeSet<Integer> set = new TreeSet<>(); for(int i = 1; i<=2*n; i++) { set.add(i); } int[] a = new int[n]; for(int i = 0; i<n; i++) { int ai = sc.nextInt(); set.remove(ai); a[i] = ai; } int[] b = new int[2*n]; boolean foundAns = true; for(int i = 0; i<n; i++) { Integer ceiling = set.ceiling(a[i]); if(ceiling != null) { b[2*i] = a[i]; b[2*i + 1] = ceiling; set.remove(ceiling); } else { foundAns = false; break; } } if(foundAns) { for(int i = 0; i<2*n; i++) System.out.print(b[i] + " "); 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 sys import bisect input = sys.stdin.readline ins = lambda: input().rstrip() ini = lambda: int(input().rstrip()) inm = lambda: map(int, input().rstrip().split()) inl = lambda: list(map(int, input().split())) out = lambda x: print('\n'.join(map(str, x))) t = ini() for _ in range(t): n = ini() b = inl() a = [i for i in range(1, n*2+1) if i not in b] out = True for index, number in enumerate(sorted(b)): if number > a[index]: out = False print(-1) break if not out: continue ans = [] for i in b: x = bisect.bisect_right(a, i) ans.append(i) ans.append(a[x]) a.pop(x) 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; bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } void solve() { int n; cin >> n; vector<pair<int, int>> m; map<int, int> mp; for (int j = 0; j < n; j++) { int x; cin >> x; m.push_back({x, j * 2}); mp.insert({x, j * 2}); } set<int> v; for (int i = 1; i < 2 * n + 1; i++) { if (mp.find(i) == mp.end()) { v.insert(i); } } for (int i = 0; i < n; i++) { auto it = v.upper_bound(m[i].first); m.push_back({*it, m[i].second + 1}); v.erase(*it); } sort(m.begin(), m.end(), sortbysec); for (int i = 0; i < n; i++) { if (m[2 * i + 1].first <= m[2 * i].first) { cout << "-1\n"; return; } } for (auto u : m) { cout << u.first << " "; } cout << "\n"; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { 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
#!/usr/bin/env python from __future__ import division, print_function import os import 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 class SortedList: def __init__(self, iterable=[], _load=200): """Initialize sorted list instance.""" values = sorted(iterable) self._len = _len = len(values) self._load = _load self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)] self._list_lens = [len(_list) for _list in _lists] self._mins = [_list[0] for _list in _lists] self._fen_tree = [] self._rebuild = True def _fen_build(self): """Build a fenwick tree instance.""" self._fen_tree[:] = self._list_lens _fen_tree = self._fen_tree for i in range(len(_fen_tree)): if i | i + 1 < len(_fen_tree): _fen_tree[i | i + 1] += _fen_tree[i] self._rebuild = False def _fen_update(self, index, value): """Update `fen_tree[index] += value`.""" if not self._rebuild: _fen_tree = self._fen_tree while index < len(_fen_tree): _fen_tree[index] += value index |= index + 1 def _fen_query(self, end): """Return `sum(_fen_tree[:end])`.""" if self._rebuild: self._fen_build() _fen_tree = self._fen_tree x = 0 while end: x += _fen_tree[end - 1] end &= end - 1 return x def _fen_findkth(self, k): """Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`).""" _list_lens = self._list_lens if k < _list_lens[0]: return 0, k if k >= self._len - _list_lens[-1]: return len(_list_lens) - 1, k + _list_lens[-1] - self._len if self._rebuild: self._fen_build() _fen_tree = self._fen_tree idx = -1 for d in reversed(range(len(_fen_tree).bit_length())): right_idx = idx + (1 << d) if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]: idx = right_idx k -= _fen_tree[idx] return idx + 1, k def _delete(self, pos, idx): """Delete value at the given `(pos, idx)`.""" _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len -= 1 self._fen_update(pos, -1) del _lists[pos][idx] _list_lens[pos] -= 1 if _list_lens[pos]: _mins[pos] = _lists[pos][0] else: del _lists[pos] del _list_lens[pos] del _mins[pos] self._rebuild = True def _loc_left(self, value): """Return an index pair that corresponds to the first position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins lo, pos = -1, len(_lists) - 1 while lo + 1 < pos: mi = (lo + pos) >> 1 if value <= _mins[mi]: pos = mi else: lo = mi if pos and value <= _lists[pos - 1][-1]: pos -= 1 _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value <= _list[mi]: idx = mi else: lo = mi return pos, idx def _loc_right(self, value): """Return an index pair that corresponds to the last position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins pos, hi = 0, len(_lists) while pos + 1 < hi: mi = (pos + hi) >> 1 if value < _mins[mi]: hi = mi else: pos = mi _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value < _list[mi]: idx = mi else: lo = mi return pos, idx def add(self, value): """Add `value` to sorted list.""" _load = self._load _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len += 1 if _lists: pos, idx = self._loc_right(value) self._fen_update(pos, 1) _list = _lists[pos] _list.insert(idx, value) _list_lens[pos] += 1 _mins[pos] = _list[0] if _load + _load < len(_list): _lists.insert(pos + 1, _list[_load:]) _list_lens.insert(pos + 1, len(_list) - _load) _mins.insert(pos + 1, _list[_load]) _list_lens[pos] = _load del _list[_load:] self._rebuild = True else: _lists.append([value]) _mins.append(value) _list_lens.append(1) self._rebuild = True def discard(self, value): """Remove `value` from sorted list if it is a member.""" _lists = self._lists if _lists: pos, idx = self._loc_right(value) if idx and _lists[pos][idx - 1] == value: self._delete(pos, idx - 1) def remove(self, value): """Remove `value` from sorted list; `value` must be a member.""" _len = self._len self.discard(value) if _len == self._len: raise ValueError('{0!r} not in list'.format(value)) def pop(self, index=-1): """Remove and return value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) value = self._lists[pos][idx] self._delete(pos, idx) return value def bisect_left(self, value): """Return the first index to insert `value` in the sorted list.""" pos, idx = self._loc_left(value) return self._fen_query(pos) + idx def bisect_right(self, value): """Return the last index to insert `value` in the sorted list.""" pos, idx = self._loc_right(value) return self._fen_query(pos) + idx def count(self, value): """Return number of occurrences of `value` in the sorted list.""" return self.bisect_right(value) - self.bisect_left(value) def __len__(self): """Return the size of the sorted list.""" return self._len def __getitem__(self, index): """Lookup value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) return self._lists[pos][idx] def __delitem__(self, index): """Remove value at `index` from sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) self._delete(pos, idx) def __contains__(self, value): """Return true if `value` is an element of the sorted list.""" _lists = self._lists if _lists: pos, idx = self._loc_left(value) return idx < len(_lists[pos]) and _lists[pos][idx] == value return False def __iter__(self): """Return an iterator over the sorted list.""" return (value for _list in self._lists for value in _list) def __reversed__(self): """Return a reverse iterator over the sorted list.""" return (value for _list in reversed(self._lists) for value in reversed(_list)) def __repr__(self): """Return string representation of sorted list.""" return 'SortedList({0})'.format(list(self)) def main(): t = int(input()) for _ in range(t): n = int(input()) b = [int(i) for i in input().split()] a = [b[i // 2] for i in range(2 * n)] s = SortedList(set(range(1, 2 * n + 1)) - set(b)) for i in range(1, 2 * n, 2): idx = (s.bisect_left(a[i])) if idx < len(s): a[i] = s[idx] else: a = [-1] break s.remove(a[i]) print(*a) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") 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()
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
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, flag = 1; cin >> n; int a[201] = {0}, b[n], ans[2 * n]; for (int i = 0; i < n; i++) { cin >> b[i]; a[b[i]] = 1; } if (a[1] == 0 || a[2 * n] == 1) { cout << "-1\n"; continue; } for (int i = 0, j = 0; i < n; i++, j++) { ans[j] = b[i]; j++; b[i]++; while (a[b[i]] != 0) { b[i]++; } if (b[i] > 2 * n) { cout << "-1"; flag = 0; break; } ans[j] = b[i]; a[b[i]] = 1; } if (flag) for (int i = 0; i < 2 * n; i++) cout << ans[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 = (int)100; const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1}; const int dy[] = {0, 1, 0, -1, -1, 1, -1, 1}; char dir[] = {'L', 'D', 'R', 'U'}; const long long int mod = (long long int)1e9 + 7; const long long int inf = 1e18 + 100; int BIT[100005], n; long long int prime[17], spf[N]; template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <typename T> T min3(T a, T b, T c) { return min(a, min(b, c)); } template <typename T> T max3(T a, T b, T c) { return max(a, max(b, c)); } bool isVowel(char ch) { ch = toupper(ch); if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E') return true; return false; } long long int mInv(long long int a, long long int m) { long long int m0 = m; long long int y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long long int q = a / m; long long int t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } long long int power(long long int x, long long int y) { long long int res = 1; x = x % mod; while (y > 0) { if (y & 1) res = (res * x) % mod; y = y >> 1; x = (x * x) % mod; } return res; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long Extendedgcd(long long a, long long b, long long *x, long long *y) { if (!a) { *x = 0; *y = 1; return b; } long long nx, ny; long long d = Extendedgcd(b % a, a, &nx, &ny); long long k = b / a; *y = nx; *x = -k * nx + ny; return d; } void update(int x, int delta) { if (!x) { BIT[x] += delta; return; } for (; x <= N; x += x & -x) BIT[x] += delta; } long long int query(int x) { long long int sum = BIT[0]; for (; x > 0; x -= x & -x) sum += BIT[x]; return sum; } void Sieve() { for (int i = 1; i <= N; i++) prime[i] = 1; prime[0] = 0; prime[1] = 0; for (long long int p = 2; p * p <= N; p++) { if (prime[p] == 1) { for (long long int i = p * p; i <= N; i += p) prime[i] = 0; } } for (int p = 3; p <= N; p++) prime[p] += prime[p - 1]; } void sieve() { spf[1] = -1; for (int i = 2; i < N; i++) spf[i] = i; for (int i = 4; i < N; i += 2) spf[i] = 2; for (long long int i = 3; i * i < N; i++) { if (spf[i] == i) { for (long long int j = i * i; j < N; j += i) if (spf[j] == j) spf[j] = i; } } } struct cmp { bool operator()(const pair<int, int> &x, const pair<int, int> &y) const { if (x.first != y.first) return x.first < y.first; return x.second > y.second; } }; void gInput(int m, vector<int> adj[]) { int u, v; for (int i = 0; i < m; i++) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } } vector<long long int> fac; void factorial() { int N = (int)1e6; fac.resize(N); fac[0] = 1; for (long long int i = 1; i <= N; i++) { fac[i] = ((fac[i - 1] % mod) * i) % mod; } } long long int nCr(long long int n, long long int r) { long long int x = fac[n]; long long int y = fac[r]; long long int z = fac[n - r]; long long int pr = ((y % mod) * (z % mod)) % mod; pr = mInv(pr, mod); long long int ncr = ((x % mod) * (pr % mod)) % mod; return ncr; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) { int n; cin >> n; int a[n + 1]; int b[2 * n + 1]; int vis[2 * n + 1]; memset(vis, 0, sizeof(vis)); memset(b, 0, sizeof(b)); for (int i = 1; i <= n; i++) { cin >> a[i]; vis[a[i]] = 1; b[2 * i - 1] = a[i]; } int f = 0; for (int i = 1; i <= 2 * n; i++) { if (b[i] == 0) { int g = 0; for (int j = 1; j <= 2 * n; j++) { if (!vis[j] and j > b[i - 1]) { b[i] = j; vis[j] = 1; g = 1; break; } } if (!g) f = 1; } } if (f) cout << -1; else for (int i = 1; i <= 2 * n; 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
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; vector<long long> line(n); for (long long& i : line) cin >> i; set<long long> av; for (long long i = 1; i <= 2 * n; i++) av.insert(i); for (long long i : line) av.erase(i); vector<long long> ans(2 * n); for (long long i = 0; i < n; i++) { ans[2 * i] = line[i]; auto it = av.lower_bound(line[i]); if (it == av.end()) { cout << -1 << endl; return; } ans[2 * i + 1] = *it; av.erase(it); } for (long long i : ans) cout << i << " "; cout << endl; } signed main() { long long q = 1; cin >> q; while (q--) { 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
# Work hard. Complain harder. Unknown # by : Blue Edge - Create some chaos for _ in range(int(input())): n=int(input()) b=list(map(int,input().split())) a=[1]*(2*n+1) for x in b: a[x]=0 p=[] res=True for x in b: p.append(x) f=0 i=x+1 while i<2*n+1: if a[i]: a[i]=0 p.append(i) f=1 break i+=1 if not f: res=False if res: print(*p) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
def solution(): for _ in range(int(input())): n = int(input()) arr = [int(i) for i in input().split()] maximal = max(arr) minimal = min(arr) mandatory = set(range(1, minimal+maximal+1)) if len(mandatory) > 2*n or minimal > 1: print(-1) continue if len(mandatory) != 2*n: mandatory.update(set(range(len(mandatory)+1, (2*n)+1))) mandatory.difference_update(set(arr)) ans = [0]*n for i in range(0,n,1): if arr[i]+1 in mandatory: ans[i] = arr[i]+1 mandatory.remove(ans[i]) else: for j in mandatory: if j > arr[i]: ans[i] = j mandatory.remove(j) break if 0 in ans: print(-1) continue from functools import reduce from operator import add print(*reduce(add, zip(arr, ans))) solution()
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 debug_out() { cerr << '\n'; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}; const int maxn = 105; long long n, m, k; long long a[maxn * 2], b[maxn], c[maxn * 2]; int main(void) { ios_base::sync_with_stdio(0), cin.tie(0); int tc; cin >> tc; while (tc--) { memset(c, 0, sizeof(c)); cin >> n; for (int i = 1; i <= n; ++i) { cin >> b[i]; ++c[b[i]]; } bool valid = true; set<int> s; for (int i = 1; i <= n * 2; ++i) { if (!c[i]) s.insert(i); if (c[i] > 1) valid = false; } for (int i = 1; i <= n; ++i) { auto it = s.lower_bound(b[i]); if (it == s.end()) { valid = false; break; } a[i * 2 - 1] = b[i]; a[i * 2] = *it; s.erase(it); } if (!valid) cout << -1 << '\n'; else { for (int i = 1; i <= n * 2; ++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
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class RestoringPermutation implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { int t = in.ni(); while (t-- > 0) { int n = in.ni(); int[] result = new int[2 * n]; boolean[] taken = new boolean[2 * n + 1]; for (int i = 0; i < 2 * n; i += 2) { int next = in.ni(); result[i] = next; taken[next] = true; } boolean possible = true; for (int i = 1; i < 2 * n; i += 2) { int min = firstFreeBiggerThan(taken, result[i - 1]); if (min == -1) { possible = false; break; } taken[min] = true; result[i] = min; } if (possible) { for (int i = 0; i < 2 * n; i++) { out.print(result[i]); out.print(' '); } out.println(); } else { out.println(-1); } } } private int firstFreeBiggerThan(boolean[] taken, int limit) { for (int i = limit + 1; i < taken.length; i++) { if (!taken[i]) { return i; } } return -1; } @Override public void close() throws IOException { in.close(); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (RestoringPermutation instance = new RestoringPermutation()) { instance.solve(); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Collections; import java.util.TreeSet; public class Permutation { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); PrintWriter out = new PrintWriter(System.out); StringBuilder k = new StringBuilder(""); while(t-->0){ int n = Integer.parseInt(br.readLine()); String[] arr = br.readLine().split(" "); int b[] = new int[n]; int[] c = new int[n]; TreeSet<Integer> s1 = new TreeSet<>(); for(int i=1;i<=2*n;i++){ s1.add(i); } for(int i=0;i<n;i++){ b[i] = Integer.parseInt(arr[i]); c[i]=b[i]; s1.remove(b[i]); } boolean isPossible = true; Arrays.sort(b); for(int j=0;j<n;j++){ if(b[j]>=(j+1)*2){ isPossible = false; break; } } if(!isPossible) { k.append(-1 + "\n"); }else{ for(int i=0;i<n;i++){ if(i!=0){ k.append(" "); } k.append(c[i]+" "+getNextGreater(c[i], s1)); } k.append("\n"); } } out.print(k); out.flush(); } private static int getNextGreater(int i, TreeSet<Integer> s1) { for(int p:s1){ if(p>i){ s1.remove(p); return p; } } return 0; } }
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.*; import java.io.*; import static java.lang.Math.*; public class Main implements Runnable { boolean multiple = true; long MOD; @SuppressWarnings("Duplicates") void solve() throws Exception { int n = sc.nextInt(); TreeSet<Integer> remaining = new TreeSet<>(); for (int i = 1; i <= 2 * n; i++) remaining.add(i); int[] arr = new int[n]; int[] ans = new int[n]; for (int i = 0; i < n; i++) { int next = sc.nextInt(); remaining.remove(next); arr[i] = next; } for (int i = 0; i < n; i++) { Integer next = remaining.higher(arr[i]); if (next == null) { System.out.println(-1); return; } ans[i] = next; remaining.remove(next); } for (int i = 0; i < n; i++) System.out.print(arr[i] + " " + ans[i] + " "); System.out.println(); } class Node implements Comparable<Node> { long t, u, v; Node(long T, long U, long V) { t = T; u = U; v = V; } @Override public int compareTo(Node o) { return (int)(t - o.t); } } long inv(long a, long b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; } long gcd(long a, long b) { return a == 0 ? b : gcd(b % a, a); } long lcm(long a, long b) { return (a * b) / gcd(a , b); } class SegNode { int max; int L, R; SegNode left = null, right = null; int query(int queryL, int queryR) { if (queryL == L && queryR == R) return max; int leftAns = Integer.MIN_VALUE, rightAns = Integer.MIN_VALUE; if (left != null && queryL <= left.R) leftAns = left.query(queryL, min(queryR, left.R)); if (right != null && queryR >= right.L) rightAns = right.query(max(queryL, right.L), queryR); return max(leftAns, rightAns); } SegNode(int[] arr, int l, int r) { L = l; R = r; max = arr[l]; if (l == r) return; int mid = (l + r) / 2; left = new SegNode(arr, l, mid); right = new SegNode(arr, mid + 1, r); max = max(left.max, right.max); } } void print(long[] arr) { for (int i = 0; i < arr.length; i++) System.out.print(arr[i] + " "); System.out.println(); } @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); if (multiple) { int q = sc.nextInt(); for (int i = 0; i < q; i++) solve(); } else solve(); } catch (Throwable uncaught) { Main.uncaught = uncaught; } finally { out.close(); } } public static void main(String[] args) throws Throwable { Thread thread = new Thread(null, new Main(), "", (1 << 26)); thread.start(); thread.join(); if (Main.uncaught != null) { throw Main.uncaught; } } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(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
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n); set<int> s; for (int i = 0; i < 2 * n; i++) s.insert(i + 1); for (int i = 0; i < n; i++) { cin >> a[i]; s.erase(a[i]); } vector<int> res(n, -1); set<int>::iterator it; for (int i = 0; i < n; i++) { it = s.lower_bound(a[i]); if (it != s.end()) res[i] = *it, s.erase(it); else break; } if (res.back() == -1) cout << -1 << endl; else { for (int i = 0; i < n; i++) cout << a[i] << " " << res[i] << " "; cout << endl; } } }
CPP