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() { ios_base::sync_with_stdio(false); cin.tie(0); int n, t; cin >> t; while (t--) { cin >> n; int b[n + 1], a[2 * n + 1]; bool marked[2 * n + 1]; memset(a, -1, sizeof a); memset(marked, true, sizeof marked); bool yes = true; for (int i = 1; i <= n; i++) { cin >> b[i]; if (marked[b[i]] == false) yes = false; marked[b[i]] = false; a[2 * i - 1] = b[i]; } for (int i = 1; i < 2 * n + 1; i++) { if (a[i] == -1) { bool got = false; int j = a[i - 1] + 1; while (j < 2 * n + 1 && marked[j] == false) { j++; } if (j < 2 * n + 1 && marked[j]) { marked[j] = false; a[i] = j; got = true; } if (!got) yes = false; } } if (!yes) cout << -1 << '\n'; else { for (int i = 1; i < 2 * n + 1; 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
t = int(input()) res = [] for i in range (t) : n = input() n = int(n) b = input().split() a=[0]*(2*n) for j in range (n) : b[j] = int(b[j]) for j in range (n) : a[2*j]=b[j] index=1 fuck = True for j in range (1,2*n+1) : if not j in a : flag = True for k in range (n) : if a[2*k+1]==0 and a[2*k]<j : a[2*k+1]=j flag = False break if flag : fuck = False break if fuck : res.append(a) else : res.append([-1]) for i in range (t) : for j in range (len(res[i])) : print(res[i][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
t=int(input()) for _ in range(t): n=int(input()) B=[int(i) for i in input().split()] BB=set(B) Chk=True Ans=[] Used=[True]*(2*n) for i in range(2*n): if i+1 in BB: Used[i]=False for b in B: Ans.append(b) for i in range(b,2*n): if Used[i]: Ans.append(i+1) Used[i]=False break else: Chk=False if Chk: print(*Ans) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) for ii in range(t): n=int(input()) flag=True a=[int(i) for i in input().split()] t=n*2 b=[0]*(t+1) c=[] for i in a: if i==t: flag=False break b[i]=1 if flag==False: print(-1) else: for i in a: c.append(i) j=i+1 while b[j]==1: if j==t and b[j]==1: flag=False break j+=1 if flag==False: print(-1) break c.append(j) b[j]=1 for i in c: if flag==True : print(i,end=' ') if flag==True:print()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long b[n]; for (long long i = 0; i < n; i++) cin >> b[i]; bool c[2 * n + 1]; memset(c, true, 2 * n + 1); long long a[2 * n]; memset(a, -1, 2 * n); for (long long i = 0; i < n; i++) { a[2 * i] = b[i]; c[b[i]] = false; } bool T = true; for (long long i = 1; i < 2 * n; i += 2) { long long j = a[i - 1]; while (c[j] == false && j <= 2 * n) { j++; } if (j > 2 * n) { T = false; break; } a[i] = j; c[j] = false; } if (!T) { cout << -1 << endl; continue; } for (long long i = 0; i < 2 * n; i++) { 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
import java.io.*; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Scanner; import java.text.*; import java.math.*; import java.util.*; public class Main implements Runnable { public int getMin(boolean isUsed[], int val, int n){ int min=-1; for(int v=val;v<=2*n;v++){ if(isUsed[v]==false){ min=v; isUsed[v]=true; break; } } return min; } public void solve() throws Exception { int t=iread(); for(int cases=1;cases<=t;cases++){ int n=iread(); int []b= new int[n+1]; boolean isUsed[]= new boolean[2*n+1]; Arrays.fill(isUsed, false); boolean hasResult=true; for(int i=1;i<=n;i++){ b[i]= iread(); if(b[i]>2*n || isUsed[b[i]]) hasResult=false; else isUsed[b[i]]=true; } if(!hasResult){ System.out.println("-1"); continue; } int []result= new int[2*n+1]; Arrays.fill(isUsed, false); for(int i=1;i<=n;i++){ result[2*i-1]=b[i]; isUsed[b[i]]=true; } for(int i=2;i<=2*n;i+=2){ int x= getMin(isUsed, result[i-1], n); if(x==-1){ hasResult=false; break; } else result[i]=x; } if(!hasResult){ System.out.println("-1"); } else{ for(int i=1;i<=2*n;i++) System.out.print(result[i]+" "); } } } public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new BufferedWriter(new OutputStreamWriter(System.out)); // in = new BufferedReader(new FileReader(filename+".in")); // out = new BufferedWriter(new FileWriter(filename+".out")); solve(); out.flush(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } public int iread() throws Exception { return Integer.parseInt(readword()); } public double dread() throws Exception { return Double.parseDouble(readword()); } public long lread() throws Exception { return Long.parseLong(readword()); } BufferedReader in; BufferedWriter out; public String readword() throws IOException { StringBuilder b = new StringBuilder(); int c; c = in.read(); while (c >= 0 && c <= ' ') c = in.read(); if (c < 0) return ""; while (c > ' ') { b.append((char) c); c = in.read(); } return b.toString(); } public static void main(String[] args) { try { Locale.setDefault(Locale.US); } catch (Exception e) { } // new Thread(new Main()).start(); new Thread(null, new Main(), "1", 1 << 25).start(); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) while(t>0): n=int(input()) C=list(map(int,input().split())) A=[] for i in range(0,n): A.append(C[i]) for j in range(C[i]+1,1000): if(j not in A and j not in C): A.append(j) break D=[] s=2*n for i in range(0,s): D.append(A[i]) D.sort() a=-1 for j in range(0,s): if D[i]!=i+1: a=0 break if(a==0): print("-1") else: print(*A) t=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
/* package codechef; // don't place package name! */ // for(i=0;i<n;i++) // for(j=0;j<n;j++) // int n=sc.nextInt(); // int a[]=new int[n]; // a[i]=sc.nextInt(); // ArrayList<Integer> ar=new ArrayList<Integer>(); // System.out.println(); // HashMap<String, Integer> map // = new HashMap<>(); import java.util.*; import java.lang.*; import java.io.*; public class Codeforces { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); while(t-->0) {int i; int n =sc.nextInt(); ArrayList <Integer> ar=new ArrayList<Integer>(); for(i=0;i<2*n;i++) ar.add(i+1); // System.out.println(ar); int a[]=new int[n]; int found=0,find=0; for(i=0;i<n;i++) { a[i]=sc.nextInt(); if(a[i]==1) find=1; if(a[i]==2*n) found=1; if(ar.contains(a[i])); ar.remove(ar.indexOf(a[i])); } // System.out.println(ar); if(found==1||find==0){ System.out.println(-1); continue; } if(n==1 && a[0]==1) { System.out.println(1+" "+2); continue; } // Collections.sort(ar); String s=""; int c=0; for(i=0;i<n;i++) { int x=a[i]; for(int j=0;j<ar.size();j++){ if(ar.get(j)>x){ s=s+x+" "+ar.get(j)+" "; c++; // System.out.println(s); ar.remove(ar.indexOf(ar.get(j))); // System.out.println(ar); break; } } } if(c!=n) System.out.println(-1); else System.out.println(s); } } }
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 from math import sqrt, factorial, ceil from collections import Counter, defaultdict from heapq import heapify, heapreplace, heappush, heappop from bisect import bisect_left t=int(stdin.readline()) while t: t-=1 n=int(stdin.readline()) b=list(map(int,stdin.readline().split())) a=[0]*(2*n) j=0 r=[0]*(2*n+1) ans=1 for i in range(0,2*n,2): a[i]=b[j] j+=1 r[a[i]]=1 for i in range(1,2*n,2): temp=a[i-1]+1 while temp<=2*n: if r[temp]==0: a[i]=temp r[temp]=1 break temp+=1 if a[i]==0: ans=-1 break if ans==1: print(*a) else: print(ans)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; void faster() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); if (0) { freopen( "input" "sum.in", "r", stdin); freopen( "input" "sum.out", "w", stdout); } } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long fact(int n) { long long f = 1; if ((n == 0) || (n == 1)) f = 1; else for (int i = 1; i <= n; i++) f *= i; return f; } int fib(int n) { int a = 0, b = 1; for (int i = 0; i < n; ++i) { b = b + a; a = b - a; } return b; } int n; void solve() { cin >> n; bool b = true; std::vector<int> v(n); for (int i = 0; i < n; ++i) cin >> v[i]; std::vector<int> v1; for (int i = 1; i <= 2 * n; ++i) { if (find(v.begin(), v.end(), i) == v.end()) v1.push_back(i); } sort(v1.begin(), v1.end()); std::vector<int> v2(2 * n); for (int i = 0; i < n; ++i) { v2[2 * i] = v[i]; auto kos = upper_bound(v1.begin(), v1.end(), v[i]); if (kos == v1.end()) { b = false; break; } else { v2[2 * i + 1] = *kos; v1.erase(kos); } } if (!b) cout << -1; else { for (int i = 0; i < 2 * n; ++i) cout << v2[i] << ' '; } } int main() { faster(); int t = 1; cin >> t; while (t--) { solve(); 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 compute() { int n; cin >> n; vector<int> ft(n); vector<int> res(2 * n); for (int i = 0; i < n; ++i) { cin >> ft[i]; ft[i]--; } set<int> st; for (int i = 0; i < 2 * n; ++i) { st.insert(i); } for (int i = 0; i < n; ++i) { st.erase(st.find(ft[i])); } for (int i = 0; i < n; ++i) { res[2 * i] = ft[i]; set<int>::iterator it = st.upper_bound(ft[i]); if (it == st.end()) { printf("%d\n", -1); return; } res[2 * i + 1] = *it; st.erase(it); } for (int i = 0; i < 2 * n; ++i) { printf("%d ", res[i] + 1); } printf("\n"); } int main() { int t; cin >> t; for (int tc = 0; tc < t; ++tc) { compute(); } 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
public class p1315C { public static void main(String[] args) { var sc = new java.util.Scanner(System.in); l: for(int t=sc.nextInt();t-->0;) { var sb= new StringBuilder(); int n=sc.nextInt(), j, a[]=new int[n],idx[]=new int[2*n+1]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); idx[a[i]]++; } for(int i:a) { j=i; while(idx[j]!=0) if(j+1<idx.length) j++; else {System.out.println("-1"); continue l;} idx[j]=1; sb.append(i+" "+j+" "); } System.out.println(sb); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.*; import java.util.*; public class Abc { static int m[]; public static void main(String[] args) throws Exception { // Scanner sc = new Scanner(System.in); FastReader sc = new FastReader(); int t=sc.nextInt(); while (t-->0){ int n=sc.nextInt(); int arr[]=new int[n]; boolean vis[]=new boolean[2*n + 1]; for (int i=0;i<n;i++){ arr[i]=sc.nextInt(); vis[arr[i]]=true; } TreeSet<Integer> set=new TreeSet<>(); for (int i=1;i<=2*n;i++){ if (!vis[i])set.add(i); } StringBuilder sb=new StringBuilder(); boolean f=false; for (int j=0;j<n;j++) { Integer x=set.ceiling(arr[j]); if (x==null){ f=true; break; } sb.append(arr[j]+" "+x+" "); set.remove(x); } if (f) System.out.println(-1); else System.out.println(sb); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; import java.io.*; public class Solution{ public static void main (String args[])throws IOException{ BufferedReader ob = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(ob.readLine()); while(t --> 0){ int n = Integer.parseInt(ob.readLine()); int []ar = new int[n]; int N = 2*n; LinkedList<Integer>queue = new LinkedList<>(); StringTokenizer st = new StringTokenizer(ob.readLine()); TreeSet<Integer>set = new TreeSet<>(); for(int i = 0; i < n; i++){ ar[i] = Integer.parseInt(st.nextToken()); set.add(ar[i]); } TreeSet<Integer>set1 = new TreeSet<>(); for(int i = 1; i <= N; i++){ if(!set.contains(i)){ set1.add(i); } } ArrayList<Integer>list = new ArrayList<>(); for(int i = 0; i < n; i++){ list.add(ar[i]); for(int a : set1){ if(a > ar[i]){ list.add(a); set1.remove(a); break; } } } if(list.size() < 2*n){ System.out.print(-1); }else{ for(int a : list){ System.out.print(a+" "); } } System.out.println(); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; import java.lang.management.*; import static java.lang.Math.*; @SuppressWarnings("unchecked") public class P1315C { public void run() throws Exception { for (int T = nextInt(); T > 0; T--) { int n = nextInt(), a [] = new int [n]; TreeSet<Integer> s = new TreeSet(); for (int i = n << 1; i > 0; s.add(i), i--); for (int i = 0; i < n; a[i] = nextInt(), s.remove(a[i]), i++); StringBuilder ans = new StringBuilder(300); for (int i = 0; i < n; i++) { Integer ha = s.higher(a[i]); if (ha == null) { ans = new StringBuilder("-1"); break; } else { ans.append(a[i]).append(' ').append(ha).append(' '); s.remove(ha); } } println(ans); } } public static void main(String... args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedOutputStream(System.out)); new P1315C().run(); br.close(); pw.close(); System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]"); long gct = 0, gcc = 0; for (GarbageCollectorMXBean garbageCollectorMXBean : ManagementFactory.getGarbageCollectorMXBeans()) { gct += garbageCollectorMXBean.getCollectionTime(); gcc += garbageCollectorMXBean.getCollectionCount(); } System.err.println("[GC time : " + gct + " ms, count = " + gcc + "]"); System.err.println("[JIT time : " + ManagementFactory.getCompilationMXBean().getTotalCompilationTime() + " ms]"); } static long startTime = System.currentTimeMillis(); static BufferedReader br; static PrintWriter pw; StringTokenizer stok; String nextToken() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } void print(byte b) { print("" + b); } void print(int i) { print("" + i); } void print(long l) { print("" + l); } void print(double d) { print("" + d); } void print(char c) { print("" + c); } void print(Object o) { if (o instanceof int[]) { print(Arrays.toString((int [])o)); } else if (o instanceof long[]) { print(Arrays.toString((long [])o)); } else if (o instanceof char[]) { print(Arrays.toString((char [])o)); } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o)); } else if (o instanceof short[]) { print(Arrays.toString((short [])o)); } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o)); } else if (o instanceof float[]) { print(Arrays.toString((float [])o)); } else if (o instanceof double[]) { print(Arrays.toString((double [])o)); } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o)); } else { print("" + o); } } void printsp(int [] a) { for (int i = 0, n = a.length; i < n; print(a[i] + " "), i++); } void printsp(long [] a) { for (int i = 0, n = a.length; i < n; print(a[i] + " "), i++); } void print(String s) { pw.print(s); } void println() { println(""); } void println(byte b) { println("" + b); } void println(int i) { println("" + i); } void println(long l) { println("" + l); } void println(double d) { println("" + d); } void println(char c) { println("" + c); } void println(Object o) { print(o); println(); } void println(String s) { pw.println(s); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) (br.read()); } String next() throws IOException { return nextToken(); } String nextLine() throws IOException { return br.readLine(); } int [] readInt(int size) throws IOException { int [] array = new int [size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long [] readLong(int size) throws IOException { long [] array = new long [size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } double [] readDouble(int size) throws IOException { double [] array = new double [size]; for (int i = 0; i < size; i++) { array[i] = nextDouble(); } return array; } String [] readLines(int size) throws IOException { String [] array = new String [size]; for (int i = 0; i < size; i++) { array[i] = nextLine(); } return array; } int gcd(int a, int b) { if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a); a = Math.abs(a); b = Math.abs(b); int az = Integer.numberOfTrailingZeros(a), bz = Integer.numberOfTrailingZeros(b); a >>>= az; b >>>= bz; while (a != b) { if (a > b) { a -= b; a >>>= Integer.numberOfTrailingZeros(a); } else { b -= a; b >>>= Integer.numberOfTrailingZeros(b); } } return (a << Math.min(az, bz)); } long gcd(long a, long b) { if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a); a = Math.abs(a); b = Math.abs(b); int az = Long.numberOfTrailingZeros(a), bz = Long.numberOfTrailingZeros(b); a >>>= az; b >>>= bz; while (a != b) { if (a > b) { a -= b; a >>>= Long.numberOfTrailingZeros(a); } else { b -= a; b >>>= Long.numberOfTrailingZeros(b); } } return (a << Math.min(az, bz)); } boolean isPrime(int v) { if (v <= 1) { return false; } if (v <= 3) { return true; } if (((v & 1) == 0) || ((v % 3) == 0)) { return false; } for (int m = 5, n = (int)sqrt(v); m <= n; m += 6) { if (((v % m) == 0) || ((v % (m + 2)) == 0)) { return false; } } return true; } int [] rshuffle(int [] a) { // RANDOM shuffle Random r = new Random(); for (int i = a.length - 1, j, t; i >= 0; j = r.nextInt(a.length), t = a[i], a[i] = a[j], a[j] = t, i--); return a; } int [] qshuffle(int [] a) { // QUICK shuffle int m = new Random().nextInt(10) + 2; for (int i = 0, n = a.length, j = m % n, t; i < n; t = a[i], a[i] = a[j], a[j] = t, i++, j = (i * m) % n); return a; } long [] shuffle(long [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); long t = a[i]; a[i] = a[j]; a[j] = t; } return a; } void shuffle(Object [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); Object t = a[i]; a[i] = a[j]; a[j] = t; } } int [] sort(int [] a) { final int SHIFT = 16, MASK = (1 << SHIFT) - 1, SIZE = (1 << SHIFT) + 1; int n = a.length, ta [] = new int [n], ai [] = new int [SIZE]; for (int i = 0; i < n; ai[(a[i] & MASK) + 1]++, i++); for (int i = 1; i < SIZE; ai[i] += ai[i - 1], i++); for (int i = 0; i < n; ta[ai[a[i] & MASK]++] = a[i], i++); int [] t = a; a = ta; ta = t; ai = new int [SIZE]; for (int i = 0; i < n; ai[(a[i] >> SHIFT) + 1]++, i++); for (int i = 1; i < SIZE; ai[i] += ai[i - 1], i++); for (int i = 0; i < n; ta[ai[a[i] >> SHIFT]++] = a[i], i++); return ta; } void flush() { pw.flush(); } void pause() { flush(); System.console().readLine(); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 9; const int M = 1e5 + 9; inline int read() { int op = 1, x = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') op = -1; c = getchar(); } while (isdigit(c)) x = x * 10 + c - 48, c = getchar(); return x * op; } unordered_map<int, int> mp; int vis[N], b[N]; struct node { int x, pos; friend bool operator<(node A, node B) { return A.x > B.x; } } a[N]; int main() { int T, n; while (cin >> T) { while (T--) { memset(vis, 0, sizeof(vis)); cin >> n; int mark = 0; for (int i = 1; i <= n; ++i) { cin >> a[i].x; if (a[i].x == 2 * n) mark = 1; vis[a[i].x] = 1; a[i].pos = i; b[i * 2 - 1] = a[i].x; } if (mark) { puts("-1"); continue; } for (int i = 1; i <= n; ++i) { int ff = 0; for (int j = a[i].x + 1; j <= 2 * n; ++j) if (!vis[j]) { ff = 1; vis[j] = 1; b[i * 2] = j; break; } if (ff == 0) { mark = 1; break; } } if (mark) { puts("-1"); continue; } for (int i = 1; i < 2 * n; ++i) cout << b[i] << " "; cout << b[2 * n] << '\n'; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
from bisect import bisect_left t = int(input()) for _ in range(t): n = int(input()) b = list(map(int, input().split())) dif = [0] * (n - 1) if 1 not in b: print(-1) elif max(b) >= (2 * n): print(-1) else: ans = [0] * (2 * n) base = list(range(1, (n*2)+1)) for i in range(n): ans[i*2] = b[i] base.remove(b[i]) for i in range(n): index = bisect_left(base, ans[i*2]) if index == len(base): print(-1) break ans[i*2+1] = base.pop(index) # print(base) else: print(*ans)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.*; import java.util.*; public class RestoringPermutation { private static StreamTokenizer st; private static int nextInt()throws IOException{ st.nextToken(); return (int)st.nval; } public static void main(String[] args) throws IOException{ st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); int t = nextInt(); TreeSet<Integer> ts = new TreeSet<>(); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); for(int x = 0; x < t; ++x) { int n = nextInt(); boolean used[] = new boolean[2*n]; int b[] = new int[n]; for(int i = 0; i < n; ++i) { b[i] = nextInt()-1; used[b[i]] = true; } ts.clear(); for(int i = 0; i < 2*n; ++i) if(!used[i])ts.add(i); int a[] = new int[2*n]; boolean valid = true; for(int i = 0; i < n; ++i) { a[i*2] = b[i]+1; if(ts.higher(b[i]) == null) { valid = false; break; } int ret = ts.higher(b[i]); a[i*2+1] = ret+1; ts.remove(ret); } if(!valid)pw.println(-1); else { for(int i = 0; i < 2*n-1; ++i)pw.print(a[i] +" "); pw.println(a[2*n-1]); } } pw.close(); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
from sys import stdin import math from bisect import bisect_right def inp(): return stdin.readline().strip() def binSearch(a, d): res = bisect_right(a, d) if res >= len(a): return(-1) else: return(a[res]) t = int(inp()) for _ in range(t): n = int(inp()) b = [int(x) for x in inp().split()] ar = [int(x+1) for x in range(2*n) if x+1 not in b] ans = [] for i in b: c = binSearch(ar, i) if c == -1: print(-1) break else: ar.remove(c) ans.append(min(i, c)) ans.append(max(i, c)) else: print(*ans)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; import java.io.*; import java.math.BigInteger; public class C1315{ static int gcd(int a, int b) { if (a == 0) return b; if (b == 0) return a; int k; for (k = 0; ((a | b) & 1) == 0; ++k) { a >>= 1; b >>= 1; } while ((a & 1) == 0) a >>= 1; do { while ((b & 1) == 0) b >>= 1; if (a > b) { int temp = a; a = b; b = temp; } b = (b - a); } while (b != 0); return a << k; } static int lcm(int a,int b){ return a*b/gcd(a,b); } public static int binary(int arr[],int n, int a,int beg,int end){ while(beg<=end){ int mid=beg+(end-beg)/2; if(arr[mid]==a) return mid; if(arr[mid]<a) end=mid-1; else beg=mid+1; } return -1; } public static int binaryless(int arr[],int n, int a,int beg,int end){ while(beg<=end){ int mid=beg+(end-beg)/2; if(arr[mid]>=a){ if(mid==1||arr[mid-1]<a) return mid; end=mid-1; } else{ beg=mid+1; } } return -1; } public static void sieve(int t){ boolean srr[]=new boolean[t+1]; for(int i=2;i*i<=t;i++) { if(!srr[i]) { for(int k=i*i;k<=t;k+=i) srr[k]=true; } } for(int i=2;i<t+1;i++){ if(!srr[i]) System.out.println(i); } } static int power(int x, int y, int p) { int res = 1; x = x % p; while (y > 0) { if((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static class Custom implements Comparable<Custom>{ int d; int f; public Custom(int d,int f){ this.d=d; this.f=f; } public int compareTo(Custom t){ return this.d-t.d; } } void solve() throws IOException { int t=nextInt(); outer: while(t--!=0){ int n=nextInt(); int ck[]=new int[n]; boolean arr[]=new boolean[2*n+1]; for(int i=0;i<n;i++){ ck[i]=nextInt(); arr[ck[i]]=true;; } int pt=0,upt=0; for(int i=2*n;i>0;i--){ if(arr[i]) pt++; else upt++; if(pt>upt){ pw.println(-1); continue outer; } } for(int i:ck){ pw.print(i+" "); for(int j=i+1;j<=2*n;j++){ if(!arr[j]){ arr[j]=true; pw.print(j+" "); break; } } } pw.println(); } } String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } String nextLine()throws IOException{ return br.readLine(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } StringTokenizer st; BufferedReader br; PrintWriter pw; Scanner sc; void run() throws IOException { sc=new Scanner(new BufferedReader(new InputStreamReader(System.in))); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); long time = System.currentTimeMillis(); solve(); System.err.println("time = " + (System.currentTimeMillis() - time)); pw.close(); } public static void main(String[] args) throws IOException { new C1315().run(); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import bisect import sys input=sys.stdin.readline from collections import defaultdict as dd t=int(input()) while t: n=int(input()) l=list(map(int,input().split())) rem=[] for i in range(1,2*n+1): if i not in l: rem.append(i) ans=[] lol=0 for i in l: ans.append(i) ind=bisect.bisect_left(rem,i) if(ind==len(rem)): lol=1 break else: ans.append(rem[ind]) rem.pop(ind) if(lol): print(-1) else: print(*ans) 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
import java.io.BufferedReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.Scanner; import java.util.Set; import java.util.StringTokenizer; import java.util.Vector; public class ForLooops { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public Character charAt(int i) { // TODO Auto-generated method stub return null; } public BigInteger nextBigInteger() { // TODO Auto-generated method stub return null; } } static int gcd(int a , int b) { if(b==0)return a; return gcd(b,a%b); } /** * @param args * @throws IOException */ static int m =(int) (1e9+7); static long mod(long x) { return ((x%m+m)%m); } static long add(long x,long y) { return mod((mod(x)+mod(y))); } static long mul(long x,long y) { return mod((mod(x)*mod(y))); } public static void main(String[] args)throws IOException { FastReader s=new FastReader(); int t = s.nextInt(); while(t-->0) { int n = s.nextInt(); List<Integer> l = new ArrayList<>(); List<Integer> a = new ArrayList<>(); int flag=0; for(int i=0;i<n;i++) { l.add(s.nextInt()); if(l.get(i)==2*n)flag=1; } if(flag==1)System.out.println("-1"); else { for(int i=0;i<n;i++) { int k = l.get(i); k+=1; while(l.contains(k) || a.contains(k)) { k++; } if(k>2*n) { System.out.println("-1"); flag=1; break; } a.add(k); } if(flag==1)continue; for(int i=0;i<n;i++) { System.out.print(l.get(i)+" "+a.get(i)+" "); } System.out.println(); } } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; void solve() { long long int t, m, i, j, k, sum = 0; long long int n; cin >> n; vector<pair<long long int, long long int>> a(n); vector<long long int> viz(2 * n + 5, 0); for (i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i; viz[a[i].first] = 1; } sort(a.begin(), a.end(), greater<pair<long long int, long long int>>()); vector<long long int> ans(2 * n + 1); for (i = 0; i < n; i++) { long long int flag = 0; for (j = 2 * n; j > a[i].first; j--) { if (!viz[j]) { viz[j] = 1; ans[2 * a[i].second] = a[i].first; ans[2 * a[i].second + 1] = j; flag = 1; break; } } if (!flag) { cout << -1 << '\n'; return; } } for (i = 1; i < 2 * n; i += 2) { for (j = i + 2; j < 2 * n; j += 2) { if (ans[i] > ans[j] && ans[j] > ans[i - 1] && ans[i] > ans[j - 1]) { swap(ans[i], ans[j]); } } } for (i = 0; i < 2 * n; i++) { cout << ans[i] << ' '; } cout << '\n'; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int q; 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
//package learning; import java.util.*; import java.io.*; import java.lang.*; import java.text.*; import java.math.*; import java.util.regex.*; public class NitsLocal { static ArrayList<String> s1; static boolean[] prime; static int n = (int)1e7; static void sieve() { Arrays.fill(prime , true); prime[0] = prime[1] = false; for(int i = 2 ; i * i <= n ; ++i) { if(prime[i]) { for(int k = i * i; k<= n ; k+=i) { prime[k] = false; } } } } public static void main(String[] args) { InputReader sc = new InputReader(System.in); n *= 2; prime = new boolean[n + 1]; //sieve(); prime[1] = false; /* int n = sc.ni(); int k = sc.ni(); int []vis = new int[n+1]; int []a = new int[k]; for(int i=0;i<k;i++) { a[i] = i+1; } int j = k+1; int []b = new int[n+1]; for(int i=1;i<=n;i++) { b[i] = -1; } int y = n - k +1; int tr = 0; int y1 = k+1; while(y-- > 0) { System.out.print("? "); for(int i=0;i<k;i++) { System.out.print(a[i] + " "); } System.out.flush(); int pos = sc.ni(); int a1 = sc.ni(); b[pos] = a1; for(int i=0;i<k;i++) { if(a[i] == pos) { a[i] = y1; y1++; } } } ArrayList<Integer> a2 = new ArrayList<>(); if(y >= k) { int c = 0; int k1 = 0; for(int i=1;i<=n;i++) { if(b[i] != -1) { c++; a[k1] = i; a2.add(b[i]); k1++; } if(c==k) break; } Collections.sort(a2); System.out.print("? "); for(int i=0;i<k;i++) { System.out.print(a[i] + " "); } System.out.println(); System.out.flush(); int pos = sc.ni(); int a1 = sc.ni(); int ans = -1; for(int i=0;i<a2.size();i++) { if(a2.get(i) == a1) { ans = i+1; break; } } System.out.println("!" + " " + ans); System.out.flush(); } else { int k1 = 0; a = new int[k]; for(int i=1;i<=n;i++) { if(b[i] != -1) { a[k1] = i; a2.add(b[i]); k1++; } } for(int i=1;i<=n;i++) { if(b[i] == -1) { a[k1] = i; k1++; if(k1==k) break; } } int ans = -1; while(true) { System.out.print("? "); for(int i=0;i<k;i++) { System.out.print(a[i] + " "); } System.out.println(); System.out.flush(); int pos = sc.ni(); int a1 = sc.ni(); int f = 0; if(b[pos] != -1) { Collections.sort(a2); for(int i=0;i<a2.size();i++) { if(a2.get(i) == a1) { ans = i+1; f = 1; System.out.println("!" + " " + ans); System.out.flush(); break; } } if(f==1) break; } else { b[pos] = a1; a = new int[k]; a2.add(a1); for(int i=1;i<=n;i++) { if(b[i] != -1) { a[k1] = i; a2.add(b[i]); k1++; } } for(int i=1;i<=n;i++) { if(b[i] == -1) { a[k1] = i; k1++; if(k1==k) break; } } } } } */ int t = sc.ni(); while(t-- > 0) { int n = sc.ni(); boolean []vis = new boolean[2*n+1]; ArrayList<Integer> []a1 = new ArrayList[n]; for(int i=0;i<n;i++) { a1[i] = new ArrayList<>(); int p = sc.ni(); a1[i].add(p); vis[p] = true; } int f = 0; for(int i=0;i<n;i++) { int temp = a1[i].get(0); for(int j=temp+1;j<=2*n;j++) { if(!vis[j]) { a1[i].add(j); vis[j] = true; break; } } } for(int i=0;i<n;i++) { if(a1[i].size() != 2) { f=1; break; } } if(f==1) w.println("-1"); else { for(int i=0;i<n;i++) { w.print(a1[i].get(0) + " " + a1[i].get(1) + " "); } w.println(); } } w.close(); } static class Student{ int time; int l; int h; Student(int time,int l,int h) { this.time = time; this.l = l; this.h = h; } } static int upperBound(ArrayList<Integer> a, int low, int high, int element){ while(low < high){ int middle = low + (high - low)/2; if(a.get(middle) >= element) high = middle; else low = middle + 1; } return low; } static long func(long t,long e,long h,long a, long b) { if(e*a >= t) return t/a; else { return e + Math.min(h,(t-e*a)/b); } } public static int upperBound(int []arr,int n,int num) { int l = 0; int r = n; while(l < r) { int mid = (l+r)/2; if(num >= arr[mid]) { l = mid +1; } else r = mid; } return l; } public static int countSetBits(int number){ int count = 0; while(number>0){ ++count; number &= number-1; } return count; } static HashSet<Integer> fac; public static void primeFactors(int n) { // Print the number of 2s that divide n int t = 0; while (n%2==0) { fac.add(2); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i*i <= n; i+= 2) { // While i divides n, print i and divide n while (n%i == 0) { fac.add(i); n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n > 2) fac.add(n); } static long modexp(long x,long n,long M) { long power = n; long result=1; x = x%M; while(power>0) { if(power % 2 ==1) result=(result * x)%M; x=(x*x)%M; power = power/2; } return result; } static long modInverse(long A,long M) { return modexp(A,M-2,M); } static long gcd(long a,long b) { if(a==0) return b; else return gcd(b%a,a); } static class Temp{ int a; int b; int c; int d; Temp(int a,int b,int c,int d) { this.a = a; this.b = b; this.c =c; this.d = d; //this.d = d; } } static long sum1(int t1,int t2,int x,int []t) { int mid = (t2-t1+1)/2; if(t1==t2) return 0; else return sum1(t1,mid-1,x,t) + sum1(mid,t2,x,t); } static String replace(String s,int a,int n) { char []c = s.toCharArray(); for(int i=1;i<n;i+=2) { int num = (int) (c[i] - 48); num += a; num%=10; c[i] = (char) (num+48); } return new String(c); } static String move(String s,int h,int n) { h%=n; char []c = s.toCharArray(); char []temp = new char[n]; for(int i=0;i<n;i++) { temp[(i+h)%n] = c[i]; } return new String(temp); } public static int ip(String s){ return Integer.parseInt(s); } static class multipliers implements Comparator<Long>{ public int compare(Long a,Long b) { if(a<b) return 1; else if(b<a) return -1; else return 0; } } static class multipliers1 implements Comparator<Student>{ public int compare(Student a,Student b) { if(a.time<b.time) return -1; else if(b.time<a.time) return 1; else { if(a.l < b.l) return -1; else if(b.l<a.l) return 1; else { if(a.h < b.h) return -1; else if(b.h<a.h) return 1; else return 0; } //return 0; } } } // Java program to generate power set in // lexicographic order. static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { 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 ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nia(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String rs() { 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 String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } static PrintWriter w = new PrintWriter(System.out); static class Student1 { int id; //int x; int b; //long z; Student1(int id,int b) { this.id = id; //this.x = x; //this.s = s; this.b = b; // this.z = z; } } }
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 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 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.*; public class temp{ public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); StringBuilder sb = new StringBuilder(); for(int i=0;i<4;i++){ } A: while (t-- > 0) { int n = scn.nextInt(); int b[] = new int[n + 1]; for(int i=0;i<4;i++){ } HashSet<Integer> hs = new HashSet<>(); for (int i = 1; i <= n; i++) { b[i] = scn.nextInt(); hs.add(b[i]); } int a[] = new int[2 * n + 1]; int val = 1; for (int i = 1; i <= n; i++) { int v = b[i]; a[2 * i - 1] = v; val = v + 1; while (hs.contains(val)) val++; if (val > 2 * n) { sb.append(-1 + "\n"); continue A; } hs.add(val); a[2 * i] = val; } for (int i = 1; i < a.length; i++) sb.append(a[i] + " "); sb.append("\n"); } System.out.print(sb); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=tf-8 # """ """ from operator import itemgetter from collections import Counter def solve(n, b): numset = set(range(1,2*n)) sb = set(b) if len(sb) != n or not(numset >= sb): print(-1) else: used = [0]*(2*n+1) for bb in b: used[bb] = 1 ans = [] for bb in b: flag = True ans.append(bb) for i in range(bb+1,2*n+1): if used[i] == 0: used[i] = 1 ans.append(i) flag = False break if flag: print(-1) return 0 print(" ".join(map(str,ans))) def main(): t= int(input()) for i in range(t): n = int(input()) b = list(map(int,input().split())) solve(n, b) if __name__ == "__main__": main()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const int Max = 1e5 + 10; const int MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); int t; cin >> t; while (t--) { int n, x, mx = -1; vector<int> v, ans; map<int, int> mp, pay; bool done = false; cin >> n; for (int i = 0; i < n; i++) { cin >> x; v.push_back(x); pay[x] = 1; } for (int i = 0; i < n; i++) { ans.push_back(v[i]); for (int j = v[i] + 1;; j++) { if (!mp[j] and !pay[j]) { ans.push_back(j); mp[j] = 1; break; } } } for (int i = 0; i < 2 * n; i++) if (ans[i] > 2 * n) { done = true; break; } if (done) cout << -1 << endl; else { 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; int powmod(int a, int b, int m) { int res = 1; while (b > 0) { if (b & 1) { res = (res * 1ll * a) % m; } a = (a * 1ll * a) % m; b >>= 1; } return res; } int discrete_log(int a, int b, int m) { b %= (m - 1); int n = (int)sqrt(m + .0) + 1; map<int, int> vals; for (int p = n; p >= 1; --p) vals[powmod(a, p * n, m)] = p; for (int q = 0; q <= n; ++q) { int cur = (powmod(a, q, m) * 1ll * b) % m; if (vals.count(cur)) { int ans = vals[cur] * n - q; return ans; } } return -1; } int generate_primitive_root(int p) { vector<int> fact; int phi = p - 1, n = phi; for (int i = 2; i * i <= n; ++i) if (n % i == 0) { fact.push_back(i); while (n % i == 0) n /= i; } if (n > 1) fact.push_back(n); for (int res = 2; res <= p; ++res) { bool ok = true; for (size_t i = 0; i < fact.size() && ok; ++i) ok &= powmod(res, phi / fact[i], p) != 1; if (ok) return res; } return -1; } vector<vector<int>> mat_mul(vector<vector<int>> a, vector<vector<int>> b) { int N = a.size(); vector<vector<int>> ans(N, vector<int>(N, 0)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { ans[i][j] += a[i][k] + a[k][j]; ans[i][j] %= (int)(1e9 + 7); } } } return ans; } int main() { int q; cin >> q; while (q--) { int n; cin >> n; vector<int> a(2 * n + 2); set<int> st; for (int i = 1; i <= 2 * n; i++) st.insert(i); for (int i = 1; i <= n; i++) { cin >> a[2 * i - 1]; st.erase(a[2 * i - 1]); } bool ok = true; for (int i = 1; i <= n; i++) { auto it = st.upper_bound(a[2 * i - 1]); if (it == st.end()) { ok = false; break; } else { int val = *it; st.erase(val); a[2 * i] = val; } } if (!ok) cout << -1 << endl; else { for (int i = 1; i <= 2 * n; i++) cout << a[i] << " "; cout << endl; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; import java.lang.Math; public class Test { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t=s.nextInt(); for(int i=0;i<t;i++) { int n=s.nextInt(); List<Integer> r1 = new ArrayList<Integer>(); List<Integer> r2 = new ArrayList<Integer>(); List<Integer> r3 = new ArrayList<Integer>(); for(int j=0;j<n;j++) { r1.add(s.nextInt()); } for(int j=1;j<2*n+1;j++) { if(!r1.contains(j)) r2.add(j); } Collections.sort(r2); int count=0; boolean bool=true; while(!r1.isEmpty()) { boolean came=false; for(int j=0;j<r2.size();j++) { if(r2.get(j)>r1.get(0)) { r3.add(r1.get(0)); r3.add(r2.get(j)); r1.remove(0); r2.remove(j); came=true; break; } } if(came==true) continue; System.out.println(-1); bool=false; break; } if(bool==true) { for(int j=0;j<2*n;j++) { System.out.print(r3.get(j)+" "); } 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
#include <bits/stdc++.h> using namespace std; int main() { int n, i, test; cin >> test; int tc = 0; while (tc < test) { cin >> n; int arr[n], f[2 * n]; for (i = 0; i < n; cin >> arr[i], i++) ; vector<int> mark; mark.assign(2 * n + 1, 0); for (i = 0; i < n; i++) { f[2 * i] = arr[i]; f[(2 * i) + 1] = arr[i]; mark[arr[i]] = 1; } for (i = 0; i < n; i++) { int num = f[2 * i + 1]; while (num <= 2 * n) { if (mark[num] == 0) { f[(2 * i) + 1] = num; mark[num] = 1; break; } num++; } } int status = 1; for (i = 1; i <= 2 * n; i++) { if (mark[i] == 0) { status = 0; } } if (status == 1) for (i = 0; i < 2 * n; cout << f[i] << " " << endl, i++) ; else cout << "-1" << endl; tc++; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
from math import * zzz = int(input()) for zz in range(zzz): n = int(input()) b = [int(i) for i in input().split()] a = [0] * (2 * n) sl = set([i + 1 for i in range(2*n)]) ha = True for i in range(n): a[2*i] = b[i] try: sl.remove(b[i]) except: ha = False break if ha: sl = list(sl) sl.sort() for i in range(n): j = 0 while j < len(sl) and sl[j] < b[i]: j += 1 if j == len(sl): ha = False break else: a[2*i + 1] = sl.pop(j) if ha: print(' '.join(map(str, a))) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for i in range(int(input())): n = int(input()) s = list(map(int, input().split())) elems = [0 for i in range(2 * n + 1)] ans = [] for i in range(len(s)): elems[s[i]] = 1 for i in range(len(s)): ans.append(s[i]) ch = s[i] + 1 while ch <= 2 * n and elems[ch] != 0: ch += 1 if ch > 2 * n: ans = -1 break else: elems[ch] = 1 ans.append(ch) if ans == -1: print(-1) else: print(*ans)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<bool> used(2 * n + 1, 0); vector<int> ind(2 * n + 1, -1); vector<bool> ind_used(2 * n + 1, 0); for (int i = 0; i < n; i++) { int b; cin >> b; used[b] = 1; ind[b] = (i + 1) * 2; ind_used[(i + 1) * 2] = 1; } bool possible = true; for (int i = 2 * n; i >= 1; i--) { if (used[i]) { continue; } int idx = -1; for (int j = i - 1; j >= 1; j--) { if (ind[j] % 2 == 0 && !ind_used[ind[j] - 1]) { idx = ind[j] - 1; break; } } if (idx == -1) { possible = false; break; } else { ind[i] = idx; ind_used[idx] = 1; } } if (!possible) { cout << "-1\n"; } else { vector<int> ans(2 * n); for (int i = 0; i < 2 * n; i++) { ans[ind[i + 1] - 1] = i + 1; } for (int i = 0; i < 2 * n; i += 2) { for (int j = i + 2; j < 2 * n; j += 2) { if (ans[i] > ans[j] && ans[i + 1] < ans[j] && ans[j + 1] < ans[i]) { swap(ans[i], ans[j]); } } } for (int i = 0; i < 2 * n; i += 2) { if (ans[i] > ans[i + 1]) { swap(ans[i], ans[i + 1]); } } for (int i = 0; i < 2 * n; i++) { cout << ans[i] << " "; } cout << endl; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import static java.lang.Integer.numberOfTrailingZeros; import static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Short.parseShort; import static java.lang.Byte.parseByte; import java.io.*; import java.util.*; public class Contest { static int nextInt() throws IOException { return parseInt(next()); } static long nextLong() throws IOException { return parseLong(next()); } static short nextShort() throws IOException { return parseShort(next()); } static byte nextByte() throws IOException { return parseByte(next()); } static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) tok = new StringTokenizer(in.readLine()); return tok.nextToken(); } static String nextLine() throws IOException { return in.readLine(); } private static BufferedReader in; private static StringTokenizer tok; private static PrintWriter out = new PrintWriter(System.out); private static Utilities doThis = new Utilities(out); private static MyMath doMath = new MyMath(); public static void main(String[] args) throws Exception { in = new BufferedReader(new InputStreamReader(System.in)); /*--------------------SolutionStarted-------------------*/ solve(); /*--------------------SolutionEnded--------------------*/ in.close(); out.close(); } static int n, m, k, count = 0, length = 0, sum = 0, q, t, idx, temp; static char[] s, s1; static int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE; static long lMax = Long.MAX_VALUE, lMin = Long.MIN_VALUE, lSum = 0, lCount; static int[] nums; static boolean flag; static void solve() throws IOException { t = nextInt(); whl: while(t-->0){ int[] b = new int[nextInt()]; HashSet<Integer> set = new HashSet<>(); for (int i = 0; i < b.length; i++) set.add(b[i]=nextInt()); int[]a =new int[1+(b.length*2)]; for (int i = 0; i < b.length; i++) { temp = i+1; a[(temp*2)-1] = b[i]; for (int j = b[i]; true; j++) { if(!set.contains(j)){ set.add(j); a[(temp*2)]=j; break; } } } for (int i = 1; i <=b.length*2; i++) { if(!set.contains(i)){ doThis.bad(); continue whl; } } for (int i = 1; i <a.length; i++) { out.print(a[i]+" "); } out.println(); } } } class Pair implements Comparable<Pair>{ int k,v; boolean taken; Pair(int k,int v){ this.k = k; this.v =v; } @Override public int compareTo(Pair o) { return v-o.v; } } class CharPair{ char ch; int value; CharPair(char c, int v){ ch =c; value=v; } char get(){ return ch; } } class IntPair implements Comparable<IntPair> { int key, value; IntPair(int key, int value) { this.key = key; this.value = value; } @Override public int compareTo(IntPair o) { if (value == o.value) return key - o.key; return value - o.value; } public String toString() { return key + " " + value; } } class BooleanPair { boolean key, value; BooleanPair() { } BooleanPair(boolean key, boolean value) { this.key = key; this.value = value; } } class Utilities { PrintWriter out; Utilities(PrintWriter out){ this.out = out; } //int arrays start int lowerBound(int[] arr, int low, int high, int element) { int middle; while (low < high) { middle = low + (high - low) / 2; if (element > arr[middle]) low = middle + 1; else high = middle; } if (arr[low] < element) return -1; return low; } int upperBound(int[] arr, int low, int high, int element) { int middle; while (low < high) { middle = low + (high - low) / 2; if (arr[middle] > element) high = middle; else low = middle + 1; } if (arr[low] <= element) return -1; return low; } //int arrays end //long arrays start int lowerBound(long[] arr, int low, int high, long element) { int middle; while (low < high) { middle = low + (high - low) / 2; if (element > arr[middle]) low = middle + 1; else high = middle; } if (arr[low] < element) return -1; return low; } int upperBound(long[] arr, int low, int high, long element) { int middle; while (low < high) { middle = low + (high - low) / 2; if (arr[middle] > element) high = middle; else low = middle + 1; } if (arr[low] <= element) return -1; return low; } //long arrays end //double arrays start int lowerBound(double[] arr, int low, int high, double element) { int middle; while (low < high) { middle = low + (high - low) / 2; if (element > arr[middle]) low = middle + 1; else high = middle; } if (arr[low] < element) return -1; return low; } int upperBound(double[] arr, int low, int high, double element) { int middle; while (low < high) { middle = low + (high - low) / 2; if (arr[middle] > element) high = middle; else low = middle + 1; } if (arr[low] <= element) return -1; return low; } //double arrays end int arrUp(int [][]arr,int i, int j){ if(i==0) return Integer.MIN_VALUE; return arr[i-1][j]; } int arrDown(int [][]arr,int i, int j){ if(i==arr.length-1) return Integer.MIN_VALUE; return arr[i+1][j]; } int arrLeft(int [][]arr,int i, int j){ if(j==0) return Integer.MIN_VALUE; return arr[i][j-1]; } int arrRight(int [][]arr,int i, int j){ if(j==arr[i].length-1) return Integer.MIN_VALUE; return arr[i][j+1]; } int count(char[] s, char c){ int count = 0; for (int i = 0; i < s.length; i++) { if (s[i]==c) count++; } return count; } void swap(char[] s, int a,int b){ char temp =s[b]; s[b]= s[a]; s[a] = temp; } boolean odd(int a){ return a%2==1; } boolean even(int a){ return a%2==0; } void yes(){ out.println("YES"); } void no() { out.println("NO"); } void bad(){ out.println(-1); } void printBinary(int num){ out.println(Integer.toBinaryString(num)); } } class MyMath { boolean isCoPrime(int a, int b) { return gcd(a, b) == 1; } int gcd(int a, int b) { if (a == 0) return b; while (b != 0) if (a > b) a = a - b; else b = b - a; return a; } int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } boolean[] sieveOfEratosthenes(int n) { // Create a boolean array "primes[0..n]" and initialize // all entries it as true. A value in primes[i] will // finally be false if i is Not a primes, else true. boolean primes[] = new boolean[n+1]; for(int i=0;i<n;i++) primes[i] = true; for(int p = 2; p*p <=n; p++) { // If primes[p] is not changed, then it is a primes if(primes[p]) { // Update all multiples of p for(int i = p*p; i <= n; i += p) primes[i] = false; } } return primes; } } class Bitmasks{ static byte andBitmask(byte state, byte... masks){ byte temp=0; for (int i = 0; i < masks.length; i++) { temp|=masks[i]; } return (byte) (state&temp); } static byte toggleBitmask(byte state ,byte... masks){ return state; } static byte removeBitmask(byte state, byte... masks){ return state; } static byte addBitmask(byte state ,byte... masks){ return state; } }
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.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.StringTokenizer; import java.util.TreeSet; public class Main { static ArrayList<Integer> adj[]; static PrintWriter out = new PrintWriter(System.out); public static int mod = 1000000007; static int notmemo[][][][]; static int k; static int a[]; static int b[]; static int m; static char c[]; static int trace[]; static int h; static int x; static int ans1; static int ans2; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int t=sc.nextInt(); label:while(t-->0) { int n=sc.nextInt(); int b[]=new int[n]; HashSet<Integer> set=new HashSet<>(); int a[]=new int[n*2]; int j=0; for (int i = 0; i < b.length; i++) { b[i]=sc.nextInt(); set.add(b[i]); a[j]=b[i]; j+=2; } int idx=0; for (int i = 0; i < a.length; i++) { if(a[i]!=0) { continue; } for (int num = 1; num<=2*n; num++) { if(set.contains(num)||Math.min(a[i-1], num)==num) { continue; } set.add(num); a[i]=num; break; } } for (int i = 0; i < a.length; i++) { if(a[i]==0) { System.out.println(-1); continue label; } } for (int i = 0; i < a.length; i++) { System.out.print(a[i]+" "); } System.out.println(); } } static class book implements Comparable<book> { int idx; int score; public book(int i, int s) { idx = i; score = s; } @Override public int compareTo(book o) { return o.score - score; } } static class library implements Comparable<library> { int numofbooks; int signup; int shiprate; int idx; public library(int a, int b, int c, int idx) { numofbooks = a; signup = b; shiprate = c; this.idx = idx; } @Override public int compareTo(library o) { if(signup==o.signup) { return o.numofbooks-numofbooks; } return signup - o.signup; } } static long dp(int i, int state) { if (i < 0) { return state; } if (memo[i][state] != -1) return memo[i][state]; int x = Integer.parseInt(c[i] + ""); x += state; int newc = x / 10; x = x % 10; return memo[i][state] = Math.min(dp(i - 1, newc) + x, x == 0 ? (long) 1e15 : dp(i - 1, newc + 1) + 10 - x); } static boolean isPal(String s) { int j = s.length() - 1; c = s.toCharArray(); Arrays.sort(c); for (int i = 0; i < c.length; i++) { } return true; } static boolean dfs(int u, int color) { vis[u] = true; boolean f = true; for (int v : adj[u]) { if (u != ans1 && a[v] != a[u] && v != ans1) { // System.out.println(u+" "+v+" "+a[u]+" "+a[v]+" "+ans1); return false; } if (!vis[v]) { f &= dfs(v, a[v]); } } return f; } static String s; static boolean isOn(int S, int j) { return (S & 1 << j) != 0; } static int y, d; static void extendedEuclid(int a, int b) { if (b == 0) { x = 1; y = 0; d = a; return; } extendedEuclid(b, a % b); int x1 = y; int y1 = x - a / b * y; x = x1; y = y1; } static boolean f = true; static class SegmentTree { // 1-based DS, OOP int N; // the number of elements in the array as a power of 2 (i.e. after padding) int[] array, sTree, lazy; SegmentTree(int[] in) { array = in; N = in.length - 1; sTree = new int[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero lazy = new int[N << 1]; build(1, 1, N); } void build(int node, int b, int e) // O(n) { if (b == e) sTree[node] = array[b]; else { int mid = b + e >> 1; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); sTree[node] = sTree[node << 1] + sTree[node << 1 | 1]; } } void update_point(int index, int val) // O(log n) { index += N - 1; sTree[index] += val; while (index > 1) { index >>= 1; sTree[index] = Math.max(sTree[index << 1], sTree[index << 1 | 1]); } } void update_range(int i, int j, int val) // O(log n) { update_range(1, 1, N, i, j, val); } void update_range(int node, int b, int e, int i, int j, int val) { if (i > e || j < b) return; if (b >= i && e <= j) { sTree[node] += (e - b + 1) * val; lazy[node] += val; } else { int mid = b + e >> 1; propagate(node, b, mid, e); update_range(node << 1, b, mid, i, j, val); update_range(node << 1 | 1, mid + 1, e, i, j, val); sTree[node] = sTree[node << 1] + sTree[node << 1 | 1]; } } void propagate(int node, int b, int mid, int e) { lazy[node << 1] += lazy[node]; lazy[node << 1 | 1] += lazy[node]; sTree[node << 1] += (mid - b + 1) * lazy[node]; sTree[node << 1 | 1] += (e - mid) * lazy[node]; lazy[node] = 0; } int query(int i, int j) { return query(1, 1, N, i, j); } int query(int node, int b, int e, int i, int j) // O(log n) { if (i > e || j < b) return 0; if (b >= i && e <= j) return sTree[node]; int mid = b + e >> 1; // propagate(node, b, mid, e); int q1 = query(node << 1, b, mid, i, j); int q2 = query(node << 1 | 1, mid + 1, e, i, j); return Math.max(q1, q2); } } static long memo[][]; static long nCr1(int n, int k) { if (n < k) return 0; if (k == 0 || k == n) return 1; if (comb[n][k] != -1) return comb[n][k]; if (n - k < k) return comb[n][k] = nCr1(n, n - k); return comb[n][k] = ((nCr1(n - 1, k - 1) % mod) + (nCr1(n - 1, k) % mod)) % mod; } static class UnionFind { int[] p, rank, setSize; int numSets; int max[]; public UnionFind(int N) { max = new int[N]; p = new int[numSets = N]; rank = new int[N]; setSize = new int[N]; for (int i = 0; i < N; i++) { p[i] = i; setSize[i] = 1; max[i] = i; } } public int findSet(int i) { return p[i] == i ? i : (p[i] = findSet(p[i])); } public boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); } public void unionSet(int i, int j) { if (isSameSet(i, j)) return; numSets--; int x = findSet(i), y = findSet(j); if (rank[x] > rank[y]) { p[y] = x; setSize[x] += setSize[y]; max[x] = Math.max(max[x], max[y]); } else { p[x] = y; setSize[y] += setSize[x]; if (rank[x] == rank[y]) rank[y]++; max[y] = Math.max(max[x], max[y]); } } private int getmax(int j) { return max[findSet(j)]; } public int numDisjointSets() { return numSets; } public int sizeOfSet(int i) { return setSize[findSet(i)]; } } /** * private static void trace(int i, int time) { if(i==n) return; * * * long ans=dp(i,time); * if(time+a[i].t<a[i].burn&&(ans==dp(i+1,time+a[i].t)+a[i].cost)) { * * trace(i+1, time+a[i].t); * * l1.add(a[i].idx); return; } trace(i+1,time); * * } **/ static class incpair implements Comparable<incpair> { int a; long b; int idx; incpair(int a, long dirg, int i) { this.a = a; b = dirg; idx = i; } public int compareTo(incpair e) { return (int) (b - e.b); } } static class decpair implements Comparable<decpair> { int a; long b; int idx; decpair(int a, long dirg, int i) { this.a = a; b = dirg; idx = i; } public int compareTo(decpair e) { return (int) (e.b - b); } } static long allpowers[]; static class Quad implements Comparable<Quad> { int u; int v; char state; int turns; public Quad(int i, int j, char c, int k) { u = i; v = j; state = c; turns = k; } public int compareTo(Quad e) { return (int) (turns - e.turns); } } static long dirg[][]; static Edge[] driver; static int n; static class Edge implements Comparable<Edge> { int node; long cost; Edge(int a, long dirg) { node = a; cost = dirg; } public int compareTo(Edge e) { return (int) (cost - e.cost); } } static long manhatandistance(long x, long x2, long y, long y2) { return Math.abs(x - x2) + Math.abs(y - y2); } static long fib[]; static long fib(int n) { if (n == 1 || n == 0) { return 1; } if (fib[n] != -1) { return fib[n]; } else return fib[n] = ((fib(n - 2) % mod + fib(n - 1) % mod) % mod); } static class Pair { int a, b; Pair(int x, int y) { a = x; b = y; } } static long[][] comb; static class Triple implements Comparable<Triple> { int l; int r; long cost; int idx; public Triple(int a, int b, long l1, int l2) { l = a; r = b; cost = l1; idx = l2; } public int compareTo(Triple x) { if (l != x.l || idx == x.idx) return l - x.l; return -idx; } } static TreeSet<Long> primeFactors(long N) // O(sqrt(N) / ln sqrt(N)) { TreeSet<Long> factors = new TreeSet<Long>(); // take abs(N) in case of -ve integers int idx = 0, p = primes.get(idx); while (p * p <= N) { while (N % p == 0) { factors.add((long) p); N /= p; } if (primes.size() > idx + 1) p = primes.get(++idx); else break; } if (N != 1) // last prime factor may be > sqrt(N) factors.add(N); // for integers whose largest prime factor has a power of 1 return factors; } static boolean visited[]; /** * static int bfs(int s) { Queue<Integer> q = new LinkedList<Integer>(); * q.add(s); int count=0; int maxcost=0; int dist[]=new int[n]; dist[s]=0; * while(!q.isEmpty()) { * * int u = q.remove(); if(dist[u]==k) { break; } for(Pair v: adj[u]) { * maxcost=Math.max(maxcost, v.cost); * * * * if(!visited[v.v]) { * * visited[v.v]=true; q.add(v.v); dist[v.v]=dist[u]+1; maxcost=Math.max(maxcost, * v.cost); } } * * } return maxcost; } **/ public static boolean FindAllElements(int n, int k) { int sum = k; int[] A = new int[k]; Arrays.fill(A, 0, k, 1); for (int i = k - 1; i >= 0; --i) { while (sum + A[i] <= n) { sum += A[i]; A[i] *= 2; } } if (sum == n) { return true; } else return false; } static boolean vis2[][]; static boolean f2 = false; static long[][] matMul(long[][] a2, long[][] b, int p, int q, int r) // C(p x r) = A(p x q) x (q x r) -- O(p x q x // r) { long[][] C = new long[p][r]; for (int i = 0; i < p; ++i) { for (int j = 0; j < r; ++j) { for (int k = 0; k < q; ++k) { C[i][j] = (C[i][j] + (a2[i][k] % mod * b[k][j] % mod)) % mod; C[i][j] %= mod; } } } return C; } static ArrayList<Pair> a1[]; static int memo1[]; static boolean vis[]; static TreeSet<Integer> set = new TreeSet<Integer>(); static long modPow(long ways, long count, long mod) // O(log e) { ways %= mod; long res = 1; while (count > 0) { if ((count & 1) == 1) res = (res * ways) % mod; ways = (ways * ways) % mod; count >>= 1; } return res % mod; } static long gcd(long ans, long b) { if (b == 0) { return ans; } return gcd(b, ans % b); } static int[] isComposite; static int[] valid; static ArrayList<Integer> primes; static ArrayList<Integer> l; static void sieve(int N) // O(N log log N) { isComposite = new int[N + 1]; isComposite[0] = isComposite[1] = 1; // 0 indicates a prime number primes = new ArrayList<Integer>(); l = new ArrayList<>(); valid = new int[N + 1]; for (int i = 2; i <= N; ++i) // can loop till i*i <= N if primes array is not needed O(N log log sqrt(N)) if (isComposite[i] == 0) // can loop in 2 and odd integers for slightly better performance { primes.add(i); l.add(i); valid[i] = 1; for (int j = i * 2; j <= N; j += i) { // j = i * 2 will not affect performance too much, may alter in // modified sieve isComposite[j] = 1; } } for (int i = 0; i < primes.size(); i++) { for (int j : primes) { if (primes.get(i) * j > N) { break; } valid[primes.get(i) * j] = 1; } } } public static int[] schuffle(int[] a2) { for (int i = 0; i < a2.length; i++) { int x = (int) (Math.random() * a2.length); int temp = a2[x]; a2[x] = a2[i]; a2[i] = temp; } return a2; } static int V; static long INF = (long) 1E16; static class Edge2 { int node; long cost; long next; Edge2(int a, int c, Long long1) { node = a; cost = long1; next = c; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
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; signed main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; auto solve = []() { int n; cin >> n; vector<pair<int, int>> a(n); for (int i = 0; i < n; i++) { cin >> a[i].first; a[i].first--; } vector<int> ans(n + n); vector<int> used(n + n); for (int i = 0; i < n; i++) { ans[i * 2] = a[i].first; a[i].second = i * 2; used[a[i].first] = 1; } for (int i = 1; i < n + n; i += 2) { int x = ans[i - 1]; int j; for (j = x + 1; j < n + n && used[j]; j++) ; if (j == n + n) { cout << -1 << '\n'; return 0; } used[j] = 1; ans[i] = j; } for (int i = 0; i < n + n; i++) { if (i > 0) cout << " "; cout << ans[i] + 1; } cout << '\n'; }; 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
// package com.company; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Array; import java.sql.SQLOutput; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner s = new Scanner(System.in); //BufferedReader s=new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb1 = new StringBuilder();//int n=Integer.parseInt(s.readLine()); int t=s.nextInt(); while(t-->0){ int n=s.nextInt(); int[] a=new int[n+1]; int[] b=new int[2*n+1]; int[] ind=new int[2*n+1]; Arrays.fill(ind,-1); int flag=0; for(int i=1;i<=n;i++){ a[i]=s.nextInt(); b[2*i-1]=a[i]; ind[a[i]]=2*i-1; }Queue<Integer> q=new LinkedList<Integer>(); for(int i=1;i<=2*n;i++){flag=-1; if(b[i]==0) { for (int j = b[i-1] + 1; j <= 2 * n; j++) { if (ind[j] == -1) { flag = 0; b[i] = j; ind[j] = i; break; } } if (flag == -1) break; }} if(flag!=-1){ for(int i=1;i<=2*n;i++){ sb1.append(b[i]+" "); }}else sb1.append(-1); sb1.append("\n"); } System.out.println(sb1.toString()); }static int min=Integer.MAX_VALUE;static long ans=0;static long count=0; static void dfs(int i,int[] vis,HashMap<Integer,Integer> h,int [] a,int[] c,int ts){ //if(vis[i]!= vis1[i]) return; if(vis[i]>0&&(!h.containsKey(i)||h.get(i)!=vis[i])) return; if(vis[i]==2) return; if(h.containsKey(i)&&h.get(i)==2) return; if(h.containsKey(i)) h.put(i,h.get(i)+1); else h.put(i,1); vis[i]+=1; //vis1[i]+=1; if(h.get(i)==2) min=Math.min(min,c[i]) ; // if(vis[i]==1) min=c[i]; dfs(a[i],vis,h,a,c,ts); } static void computeLPSArray(String pat, int M, int lps[]) { int len = 0; int i = 1; lps[0] = 0; while (i < M) { if (pat.charAt(i) == pat.charAt(len)) { len++; lps[i] = len; i++; } else // (pat[i] != pat[len]) { if (len != 0) { len = lps[len - 1]; } else // if (len == 0) { lps[i] = len; i++; } } } } static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static double power(double x, long y, int p) { double res = 1; x = x % p; while (y > 0) { if ((y & 1) == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } public static void fracion(double x) { String a = "" + x; String spilts[] = a.split("\\."); // split using decimal int b = spilts[1].length(); // find the decimal length int denominator = (int) Math.pow(10, b); // calculate the denominator int numerator = (int) (x * denominator); // calculate the nerumrator Ex // 1.2*10 = 12 int gcd = (int)gcd((long)numerator, denominator); // Find the greatest common // divisor bw them String fraction = "" + numerator / gcd + "/" + denominator / gcd; // System.out.println((denominator/gcd)); long x1=modInverse(denominator / gcd,998244353 ); // System.out.println(x1); System.out.println((((numerator / gcd )%998244353 *(x1%998244353 ))%998244353 ) ); }static int anst=Integer.MAX_VALUE; static StringBuilder sb1=new StringBuilder(); public static int bfs(int i1, ArrayList<Integer>[] h, int[] vis, int n,int val1) { Queue<Integer> q = new LinkedList<Integer>(); q.add(i1);Queue<Integer> aq=new LinkedList<Integer>(); aq.add(0); while(!q.isEmpty()){ int i=q.poll(); int val=aq.poll(); if(i==n){ return val; } if(h[i]!=null){ for(Integer j:h[i]){ if(vis[j]==0){ q.add(j);vis[j]=1; aq.add(val+1);} } } }return -1; } static int i(String a) { return Integer.parseInt(a); } static long l(String a) { return Long.parseLong(a); } static String[] inp() throws IOException { BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); return s.readLine().trim().split("\\s+"); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long modInverse(int a, int m) { { // If a and m are relatively prime, then modulo inverse // is a^(m-2) mode m return (power(a, m - 2, m)); } } // To compute x^y under modulo m static long power(int x, int y, int m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (p * p) % m; if (y % 2 == 0) return p; else return (x * p) % m; } // Function to return gcd of a and b } class Student { int l;int r; public Student(int l, int r) { this.l = l; this.r = r; } // Constructor // Used to print student details in main() public String toString() { return this.l+" "; } } class Sortbyroll implements Comparator<Student> { // Used for sorting in ascending order of // roll number public int compare(Student a, Student b){ return a.l-b.l; } } class Sortbyroll1 implements Comparator<Student> { // Used for sorting in ascending order of // roll number public int compare(Student a, Student b){ if(a.r==b.r) return a.l-b.l; return a.r-b.r; } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import sys import math def read_line(): return sys.stdin.readline()[:-1] def read_int(): return int(sys.stdin.readline()) def read_int_line(): return [int(v) for v in sys.stdin.readline().split()] t = read_int() for i in range(t): n = read_int() b = read_int_line() l = [i+1 for i in range(2*n)] for i in b: l.remove(i) l.sort() a = [] for i in range(n): a.append(b[i]) for j in l: if b[i]<j: a.append(j) l.remove(j) break if len(a)<2*n: print(-1) else: print(*a)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> int p[222], vis[222], x[111]; int main() { int T; scanf("%d", &T); while (T--) { memset(vis, 0, sizeof(vis)); int n, i, e; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &e); x[i] = e; vis[e] = 1; } int j, f = 0; for (i = 1; i <= n; i++) { p[2 * i - 1] = x[i]; f = 0; for (j = x[i] + 1; j <= 2 * n; j++) { if (vis[j] == 0) { f = 1; p[2 * i] = j; vis[j] = 1; break; } } if (f == 0) { break; } } if (f == 0) printf("-1\n"); else { for (i = 1; i <= 2 * n; i++) { printf("%d", p[i]); if (i == 2 * n) printf("\n"); else printf(" "); } } } 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
// @uthor -: puneet // TimeStamp -: 23/02/20 import java.io.*; import java.util.*; public class R622Q1 implements Runnable { private boolean testCases=true; private boolean console=false; public void solve() { int n=in.ni(); ArrayList<Integer> list=new ArrayList<>(); TreeSet<Integer> set=new TreeSet<>(); TreeSet<Integer> tset=new TreeSet<>(); boolean isok=true; int[] arr=new int[n]; for(int i=0;i<n;++i){ int num=in.ni(); arr[i]=num; tset.add(arr[i]); } for(int i=0;i<n;++i){ int num=arr[i]; if(set.contains(num)){ isok=false; }else { list.add(num); set.add(num); for(int j=num+1;j<500;++j){ if(!set.contains(j) && !tset.contains(j)){ set.add(j); list.add(j); break; } } } if(set.last()>2*n){ isok=false; } } if(!isok){ out.print("-1"); }else { for(int i:list){ out.print(i+" "); } } out.println(); } /* -------------------- Templates and Input Classes -------------------------------*/ @Override public void run() { try { init(); } catch (FileNotFoundException e) { e.printStackTrace(); } int t= testCases ? in.ni() : 1; while (t-->0) { solve(); out.flush(); } } private FastInput in; private PrintWriter out; public static void main(String[] args) throws Exception { new R622Q1().run(); // new Thread(null, new R622Q1(), "Main", 1 << 27).start(); } private void init() throws FileNotFoundException { InputStream inputStream = System.in; OutputStream outputStream = System.out; try { if (!console && System.getProperty("user.name").equals("puneet")) { outputStream = new FileOutputStream("/home/puneet/Desktop/output.txt"); inputStream = new FileInputStream("/home/puneet/Desktop/input.txt"); } } catch (Exception ignored) { } out = new PrintWriter(outputStream); in = new FastInput(inputStream); } private void sort(int[] arr) { List<Integer> list = new ArrayList<>(); for (int object : arr) list.add(object); Collections.sort(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } private void sort(long[] arr) { List<Long> list = new ArrayList<>(); for (long object : arr) list.add(object); Collections.sort(list); for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i); } public long ModPow(long x, long y, long MOD) { long res = 1L; x = x % MOD; while (y >= 1) { if ((y & 1) > 0) res = (res * x) % MOD; x = (x * x) % MOD; y >>= 1; } return res; } public int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } public long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } static class FastInput { InputStream obj; public FastInput(InputStream obj) { this.obj = obj; } byte inbuffer[] = new byte[1024]; int lenbuffer = 0, ptrbuffer = 0; int readByte() { if (lenbuffer == -1) throw new InputMismatchException(); if (ptrbuffer >= lenbuffer) { ptrbuffer = 0; try { lenbuffer = obj.read(inbuffer); } catch (IOException e) { throw new InputMismatchException(); } } if (lenbuffer <= 0) return -1;return inbuffer[ptrbuffer++]; } String ns() { int b = skip();StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) // when nextLine, (isSpaceChar(b) && b!=' ') { sb.appendCodePoint(b);b = readByte(); }return sb.toString();} int ni() { int num = 0, b;boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true;b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; }b = readByte(); }} long nl() { long num = 0;int b;boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true;b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10L + (b - '0'); } else { return minus ? -num : num; }b = readByte(); } } boolean isSpaceChar(int c) { return (!(c >= 33 && c <= 126)); } int skip() { int b;while ((b = readByte()) != -1 && isSpaceChar(b)) ;return b; } float nf() {return Float.parseFloat(ns());} double nd() {return Double.parseDouble(ns());} char nc() {return (char) skip();} } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const int MAX_N = 105; long long t, n, a[MAX_N], b; vector<int> ans; set<int> second; int main() { cin >> t; while (t--) { cin >> n; second.clear(); b = 1; ans.resize(2 * n + 2); fill(ans.begin(), ans.end(), 0); for (int i = 1; i <= 2 * n; ++i) { second.insert(i); } for (int i = 1; i <= n; ++i) { cin >> a[i]; ans[2 * i - 1] = a[i]; second.erase(a[i]); } for (int i = 1; i < 2 * n; i += 2) { auto it = second.lower_bound(ans[i]); if (it == second.end()) { b = false; break; } ans[i + 1] = *it; second.erase(it); } if (!b) { cout << -1 << endl; continue; } for (int i = 1; i < ans.size() - 1; ++i) { cout << ans[i] << " "; } cout << endl; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 210; bool vis[maxn]; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; vector<long long> v; memset(vis, 0, sizeof(vis)); for (int i = 0; i < n; i++) { long long x; cin >> x; v.push_back(x); vis[x] = 1; } bool flag = 1; vector<long long> res; for (int i = 0; i < n; i++) { bool f = 0; res.push_back(v[i]); for (int j = v[i] + 1; j <= 2 * n; j++) { if (!vis[j]) { res.push_back(j); vis[j] = 1; f = 1; break; } } if (!f) { flag = 0; cout << -1 << endl; break; } } if (flag) { for (int i = 0; i < res.size(); i++) cout << res[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
import java.io.*; import java.text.DecimalFormat; import java.util.*; import java.util.function.BinaryOperator; public class Main { private final static long mod = 1000000007; private final static int MAXN = 1000001; private static long power(long x, long y, long m) { long temp; if (y == 0) return 1; temp = power(x, y / 2, m); temp = (temp * temp) % m; if (y % 2 == 0) return temp; else return ((x % m) * temp) % m; } private static long power(long x, long y) { return power(x, y, mod); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static int nextPowerOf2(int a) { return 1 << nextLog2(a); } static int nextLog2(int a) { return (a == 0 ? 0 : 32 - Integer.numberOfLeadingZeros(a - 1)); } private static long modInverse(long a, long m) { long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long q = a / m; long t = m; m = a % m; a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } private static int[] getLogArr(int n) { int arr[] = new int[n + 1]; for (int i = 1; i < n + 1; i++) { arr[i] = (int) (Math.log(i) / Math.log(2) + 1e-10); } return arr; } private static int log[] = getLogArr(MAXN); private static int getLRSpt(int st[][], int L, int R, BinaryOperator<Integer> binaryOperator) { int j = log[R - L + 1]; return binaryOperator.apply(st[L][j], st[R - (1 << j) + 1][j]); } private static int[][] getSparseTable(int array[], BinaryOperator<Integer> binaryOperator) { int k = log[array.length+1] + 1; int st[][] = new int[array.length+1][k + 1]; for (int i = 0; i < array.length; i++) st[i][0] = array[i]; for (int j = 1; j <= k; j++) { for (int i = 0; i + (1 << j) <= array.length; i++) { st[i][j] = binaryOperator.apply(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); } } return st; } static class Subset { int parent; int rank; @Override public String toString() { return "" + parent; } } static int find(Subset[] Subsets, int i) { if (Subsets[i].parent != i) Subsets[i].parent = find(Subsets, Subsets[i].parent); return Subsets[i].parent; } static void union(Subset[] Subsets, int x, int y) { int xroot = find(Subsets, x); int yroot = find(Subsets, y); if (Subsets[xroot].rank < Subsets[yroot].rank) Subsets[xroot].parent = yroot; else if (Subsets[yroot].rank < Subsets[xroot].rank) Subsets[yroot].parent = xroot; else { Subsets[xroot].parent = yroot; Subsets[yroot].rank++; } } private static int maxx(Integer... a) { return Collections.max(Arrays.asList(a)); } private static int minn(Integer... a) { return Collections.min(Arrays.asList(a)); } private static long maxx(Long... a) { return Collections.max(Arrays.asList(a)); } private static long minn(Long... a) { return Collections.min(Arrays.asList(a)); } private static class Pair<T, U> implements Comparable<Pair<T, U>>{ T a; U b; public Pair(T a, U b) { this.a = a; this.b = b; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return a.equals(pair.a) && b.equals(pair.b); } @Override public int hashCode() { return Objects.hash(a, b); } @Override public String toString() { return "(" + a + "," + b + ')'; } @Override public int compareTo(Pair<T, U> o) { return (int)(this.a.equals(o.a)? this.b.equals(o.b)?0:((Long)this.b-(Long)o.b)/Math.abs((Long)this.b-(Long)o.b): ((Long)this.a-(Long)o.a)/Math.abs((Long)this.a-(Long)o.a)); } } private static boolean isPrimeAKS(long n) { if(n%mod==0||n<2) return false; if(n>2&&n%2==0)return false; return (power((mod-1),n, n)==(power(mod,n,n)-1+n)%n); } public static int upperBound(List<Integer> list, int value) { int low = 0; int high = list.size(); while (low < high) { final int mid = (low + high) / 2; if (value >= list.get(mid)) { low = mid + 1; } else { high = mid; } } return low; } private static int[] getLPSArray(String pattern) { int i,j,n=pattern.length(); int[] lps = new int[pattern.length()]; lps[0]=0; for(i=1,j=0;i<n;){ if(pattern.charAt(i)==pattern.charAt(j)) { lps[i++]=++j; } else if(j>0){ j=lps[j-1]; } else { lps[i++]=0; } } return lps; } private static List<Integer> findPattern(String text, String pattern) { List<Integer> matchedIndexes = new ArrayList<>(); if(pattern.length()==0) { return matchedIndexes; } int[] lps = getLPSArray(pattern); int i=0,j=0,n=text.length(),m=pattern.length(); while(i<n) { if(text.charAt(i)==pattern.charAt(j)) { i++; j++; } if (j == m) { matchedIndexes.add(i - j); j = lps[j - 1]; } if(i<n&&text.charAt(i)!=pattern.charAt(j)) { if (j > 0) { j = lps[j - 1]; } else { i++; } } } return matchedIndexes; } private static Set<Long> getDivisors(long n) { Set<Long> divisors = new HashSet<>(); divisors.add(1L); divisors.add(n); for (long i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { divisors.add(i); divisors.add(n / i); } } return divisors; } private static long getLCM(long a, long b) { return (Math.max(a,b)/gcd(a,b))*Math.min(a,b); } private static int getState(int grid[][], int i) { if(i==0||i>=grid[0].length)return 0; int x= grid[0][i-1]+ 2*(grid[1][i-1]+2*(grid[0][i]+ 2*(grid[1][i]))); return x; } static long fac[]=new long[2000005]; static long ifac[]=new long[2000005]; private static void preCompute(){ fac[0]=ifac[0]=fac[1]=ifac[1]=1; int i; for(i=2;i<2000005;i++){ fac[i]=(i*fac[i-1])%mod; ifac[i]=(power(i,mod-2)*ifac[i-1])%mod; } } private static long C(int n,int r){ return (fac[n]*((ifac[r]*ifac[n-r])%mod))%mod; } private static void reverse(int []arr, int l,int r){ while(l<r){ int tmp=arr[l]; arr[l]=arr[r]; arr[r]=tmp; l++; r--; } } public static void main(String[] args) throws Exception { long START_TIME = System.currentTimeMillis(); try (FastReader in = new FastReader(); FastWriter out = new FastWriter()) { int t, i, j, n, k, l, r,m, ti, tidx,gm,mx1,mn,q,g; for (t = in.nextInt(), tidx = 1; tidx <= t; tidx++) { long w,x=0, y=0, z=0,sum=0,bhc=0,ans=0; //out.print(String.format("Case #%d: ", tidx)); n=in.nextInt(); int b[]=in.nextIntArr(n); int a[]=new int[2*n]; TreeSet<Integer> set = new TreeSet<>(); for(i=1;i<=2*n;i++){ set.add(i); } boolean ok=true; for(i=0;i<n;i++){ a[2*i]=b[i]; if(!set.contains(b[i])){ ok=false; break; } else{ set.remove(b[i]); } } for(j=0;j<n;j++) { Integer h=set.higher(b[j]); if(h!=null){ a[2*j+1]=h; set.remove(h); } else { ok=false; } } if(!ok){ out.println(-1); } else { for(i=0;i<2*n;i++){ out.print(a[i]+" "); } out.println(); } } if(args.length>0 && "ex_time".equals(args[0])) { out.print("\nTime taken: "); out.println(System.currentTimeMillis()-START_TIME); } out.commit(); } catch (Exception e){ e.printStackTrace(); } } public static class FastReader implements Closeable { private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer st; String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception 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; } int[] nextIntArr(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } double[] nextDoubleArr(int n) { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = nextDouble(); } return arr; } long[] nextLongArr(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } String[] nextStrArr(int n) { String[] arr = new String[n]; for (int i = 0; i < n; i++) { arr[i] = next(); } return arr; } long[][] nextLongArr2(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { arr[i] = nextLongArr(m); } return arr; } @Override public void close() throws IOException { br.close(); } } public static class FastWriter implements Closeable { BufferedWriter bw; StringBuilder sb = new StringBuilder(); List<String> list = new ArrayList<>(); Set<String> set = new HashSet<>(); public FastWriter() { bw = new BufferedWriter(new OutputStreamWriter(System.out)); } <T> void commit() throws IOException { bw.write(sb.toString()); bw.flush(); sb = new StringBuilder(); } public <T> void print(T obj) throws IOException { sb.append(obj.toString()); commit(); } public void println() throws IOException { print("\n"); } public <T> void println(T obj) throws IOException { print(obj.toString() + "\n"); } <T> void printArrLn(T[] arr) throws IOException { for (int i = 0; i < arr.length - 1; i++) { print(arr[i] + " "); } println(arr[arr.length - 1]); } <T> void printArr2(T[][] arr) throws IOException { for (int j = 0; j < arr.length; j++) { for (int i = 0; i < arr[j].length - 1; i++) { print(arr[j][i] + " "); } println(arr[j][arr.length - 1]); } } <T> void printColl(Collection<T> coll) throws IOException { for (T e : coll) { print(e + " "); } println(); } void printCharN(char c, int n) throws IOException { for (int i = 0; i < n; i++) { print(c); } } void printIntArr2(int[][] arr) throws IOException { for (int j = 0; j < arr.length; j++) { for (int i = 0; i < arr[j].length - 1; i++) { print(arr[j][i] + " "); } println(arr[j][arr.length - 1]); } } @Override public void close() throws IOException { bw.close(); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
from collections import defaultdict as dd, Counter, OrderedDict as od, deque from itertools import combinations as comb, permutations as perm from functools import reduce from math import log, ceil, floor from fractions import gcd import os, sys #sys.stdin = open("pc.txt", "r") raw_input = sys.stdin.readline int_input = lambda : int(raw_input()) list_input = lambda : list(raw_input().strip()) explode_input = lambda sep :raw_input().split(sep) format_input = lambda object : map(object, raw_input().split()) t = int_input() for t_itr in xrange(t): n = int_input() b = format_input(int) ans = [] used = Counter(b) for n_itr in xrange(n): x, y = b[n_itr], b[n_itr] + 1 if used.get(x, -1) != -1: used[x] -= 1 if used[x] < 1: used.pop(x) for x_itr in xrange(x, 2 * n + 1): if used.get(x_itr, -1) == -1: ans.append(x_itr) used[x_itr] = 1 y = x_itr + 1 break for y_itr in xrange(y, 2 * n + 1): if used.get(y_itr, -1) == -1: ans.append(y_itr) used[y_itr] = 1 break m = len(ans) t = m != 2 * n for x in ans: if x > 2 * n: t = 1 break if t == 1: print "-1" else: print " ".join(map(str, ans))
PYTHON
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; import java.io.*; public class Task{ // taking inputs static BufferedReader s; static BufferedWriter out; static String read() throws IOException{String line="";while(line.length()==0){line=s.readLine();continue;}return line;} static int int_v (String s1){return Integer.parseInt(s1);} static long long_v(String s1){return Long.parseLong(s1);} static int[] int_arr() throws IOException{String[] a=read().split(" ");int[] b=new int[a.length];for(int i=0;i<a.length;i++){b[i]=int_v(a[i]);}return b;} static long[] long_arr() throws IOException{String[] a=read().split(" ");long[] b=new long[a.length];for(int i=0;i<a.length;i++){b[i]=long_v(a[i]);}return b;} static void assign(){s=new BufferedReader(new InputStreamReader(System.in));out=new BufferedWriter(new OutputStreamWriter(System.out));} // ends //......................................@uthor_Alx.............................................. public static void main(String[] args) throws IOException{ assign(); int t=int_v(read()),c=1; while(t--!=0){ int n=int_v(read()); int[] a1=int_arr(); boolean b1=true; boolean[] b=new boolean[2*n+1]; int[][] res=new int[n][2]; for(int i=0;i<n;i++){res[i][0]=a1[i];} for(int x:a1){b[x]=true;} for(int i=0;i<n;i++){ boolean bb=false; for( int j=1;j<=2*n;j++){ if(!b[j]&&j>a1[i]){ bb=true;res[i][1]=j; b[j]=true;break; } } if(!bb){out.write("-1\n");b1=false;break;} } if(!b1){continue;} for(int i=0;i<n;i++){ out.write(res[i][0]+" "+res[i][1]+" "); } out.write("\n"); } out.flush(); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> b(2 * n + 1); unordered_set<int> m; for (int i = 1; i <= 2 * n; i += 2) { cin >> b[i]; m.insert(b[i]); } int i; for (i = 2; i <= 2 * n; i += 2) { int j; j = b[i - 1] + 1; for (j; j <= 2 * n; j++) { if (m.find(j) == m.end()) { b[i] = j; m.insert(b[i]); break; } } if (j > 2 * n) break; } if (i > 2 * n) { for (int j = 1; j <= 2 * n; j++) cout << b[j] << " "; } else cout << "-1"; cout << endl; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t = int(input()) for i in range(t): n = int(input()) b = list(map(int, input().split())) m = [1 for j in range(2 * n)] for k in b: m[k - 1] = 0 a = [] mega_bul = True for el in b: a.append(el) ind = 0 bul = False while ind < 2 * n: if ind + 1 > el and m[ind] == 1: m[ind] = 0 a.append(ind + 1) bul = True break ind += 1 if not bul: mega_bul = False break if mega_bul: print(" ".join(map(str, a))) else: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
def solve(num, n): res = [None] * (2 * n) for i, x in enumerate(num): res[i*2] = x # use back tracking to fill in x used = set(num) for i in range(len(num)): for x in range(num[i] +1, 2*n + 1): if x not in used: used.add(x) res[2*i + 1] = x break return ' '.join([str(x) for x in res]) if None not in res else -1 n = int(input()) for _ in range(n): l = int(input()) num = list(map(int, input().split())) print(solve(num, 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 heapq as hq t = int(input()) for _ in range(t): n = int(input()) b = list(map(int, input().split())) sb = sorted([(b[i]-1, i) for i in range(n)]) + [(2*n+1, -1)] a = [0]*(2*n) pos = [] ind = 0 for i in range(2*n): if i < sb[ind][0]: if len(pos) == 0: a = ["-1"] break p = hq.heappop(pos) a[p] = str(i+1) else: p = 2*sb[ind][1] a[p] = str(i+1) hq.heappush(pos, p+1) ind += 1 print(" ".join(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
/*input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 */ import java.math.*; import java.io.*; import java.util.*; public class Main { static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } static Reader sc=new Reader(); static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String args[])throws IOException { /* * For integer input: int n=inputInt(); * For long input: long n=inputLong(); * For double input: double n=inputDouble(); * For String input: String s=inputString(); * Logic goes here * For printing without space: print(a+""); where a is a variable of any datatype * For printing with space: printSp(a+""); where a is a variable of any datatype * For printing with new line: println(a+""); where a is a variable of any datatype */ int t=inputInt(); while(t>0){ int n=inputInt(); HashSet<Integer> hs = new HashSet<>(); List<Integer> ls = new ArrayList<>(); int min=2*n,max=0; for(int i=0;i<n;i++) { int x=inputInt(); ls.add(x); if(x<2*n) hs.add(x); if(x<min) min=x; if(x>max) max=x; } if(hs.size()==n && min==1 && max<2*n) { String s=""; int flag=0; for(int i=0;i<n;i++) { int x=ls.get(i)+1; while(hs.contains(x)) { x++; } if(x>2*n) { flag=1; break; } s+=ls.get(i)+" "+x+" "; hs.add(x); } if(flag==0) println(s+""); else println("-1"); } else println("-1"); t--; } bw.flush(); bw.close(); } public static int inputInt()throws IOException { return sc.nextInt(); } public static long inputLong()throws IOException { return sc.nextLong(); } public static double inputDouble()throws IOException { return sc.nextDouble(); } public static String inputString()throws IOException { return sc.readLine(); } public static void print(String a)throws IOException { bw.write(a); } public static void printSp(String a)throws IOException { bw.write(a+" "); } public static void println(String a)throws IOException { bw.write(a+"\n"); } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
def is_in_array(value, array): l = 0 r = len(array) - 1 while l < r: mid = (l + r) // 2 if array[mid] == value: return True elif array[mid] > value: l = mid + 1 else: r = mid - 1 return False t = int(input()) for _ in range(t): n = int(input()) values = [int(i) for i in input().split()] sorted_values = sorted(values) # for binary search taken = [False] * (2 * n + 1) for x in values: taken[x] = True ans = [None] * 2 * n for i in range(n): ans[2 * i] = values[i] for j in range(values[i] + 1, 2 * n + 1): if not taken[j]: ans[2 * i + 1] = j taken[j] = True break else: print(-1) break else: print(" ".join(map(str,ans)))
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
//package codeforces.div2C; import java.io.*; import java.util.*; //Think through the entire logic before jump into coding! //If you are out of ideas, take a guess! It is better than doing nothing! //Read both C and D, it is possible that D is easier than C for you! //Be aware of integer overflow! //If you find an answer and want to return immediately, don't forget to flush before return! public class C1315 { static InputReader in; static PrintWriter out; public static void main(String[] args) { //initReaderPrinter(true); initReaderPrinter(false); solve(in.nextInt()); //solve(1); } static void solve(int testCnt) { for (int testNumber = 0; testNumber < testCnt; testNumber++) { int n = in.nextInt(); int[] b = new int[n + 1]; boolean[] used = new boolean[n * 2 + 1]; boolean exist = true; for(int i = 1; i <= n; i++) { int v = in.nextInt(); if(used[v]) { exist = false; } used[v] = true; b[i] = v; } int[] ans = new int[n * 2 + 1]; if(exist) { for(int i = 1; i <= n; i++) { ans[i * 2 - 1] = b[i]; } TreeSet<Integer> ts = new TreeSet<>(); for(int v = 1; v <= n * 2; v++) { if(!used[v]) ts.add(v); } for(int i = 2; i <= n * 2; i += 2) { Integer v = ts.higher(ans[i - 1]); if(v == null) { exist = false; break; } ans[i] = v; ts.remove(v); } } if(!exist) out.println(-1); else { for(int i = 1; i <= n * 2; i++) { out.print(ans[i] + " "); } out.println(); } } out.close(); } static void initReaderPrinter(boolean test) { if (test) { try { in = new InputReader(new FileInputStream("src/input.in")); out = new PrintWriter(new FileOutputStream("src/output.out")); } catch (IOException e) { e.printStackTrace(); } } else { in = new InputReader(System.in); out = new PrintWriter(System.out); } } static class InputReader { BufferedReader br; StringTokenizer st; InputReader(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream), 32768); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { 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; } Integer[] nextIntArray(int n) { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitive(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } int[] nextIntArrayPrimitiveOneIndexed(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } Long[] nextLongArray(int n) { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitive(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } long[] nextLongArrayPrimitiveOneIndexed(int n) { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } String[] nextStringArray(int n) { String[] g = new String[n]; for (int i = 0; i < n; i++) g[i] = next(); return g; } } }
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() { long long int tc; cin >> tc; while (tc--) { long long int n; cin >> n; long long int arr[2 * n + 1]; long long int e = 0; vector<long long int> v; long long int ee = 0; for (long long int i = 1; i < n + 1; i++) { cin >> arr[2 * i - 1]; if (arr[2 * i - 1] > 2 * n) e = 1; v.push_back(arr[2 * i - 1]); } if (e == 1) { cout << -1 << endl; continue; } sort(v.begin(), v.end()); vector<long long int> v1; for (long long int i = 1; i < 2 * n + 1; i++) { long long int b = lower_bound(v.begin(), v.end(), i) - v.begin(); if (v[b] == i) continue; else v1.push_back(i); } sort(v1.begin(), v1.end()); for (long long int i = 1; i < n + 1; i++) { long long int bb = lower_bound(v1.begin(), v1.end(), arr[2 * i - 1]) - v1.begin(); if (bb >= v1.size()) { ee = 1; break; } else { arr[2 * i] = v1[bb]; v1.erase(bb + v1.begin()); } } if (ee == 1) cout << -1 << endl; else { for (long long int i = 1; i < 2 * n + 1; i++) cout << arr[i] << " "; cout << endl; } } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import sys # sys.setrecursionlimit(10**6) from sys import stdin, stdout import bisect #c++ upperbound import math import heapq def modinv(n,p): return pow(n,p-2,p) def cin(): return map(int,sin().split()) def ain(): #takes array as input return list(map(int,sin().split())) def sin(): return input() def inin(): return int(input()) import math def Divisors(n) : l = [] for i in range(1, int(math.sqrt(n) + 1)) : if (n % i == 0) : if (n // i == i) : l.append(i) else : l.append(i) l.append(n//i) return l def heapPermutation(a, size, n): global x # if size becomes 1 then prints the obtained # permutation if (size == 1): # printArr(a,n) x.append(a[:]) return for i in range(size): heapPermutation(a,size-1,n); # if size is odd, swap first and last # element # else If size is even, swap ith and last element if size&1: a[0], a[size-1] = a[size-1],a[0] else: a[i], a[size-1] = a[size-1],a[i] """*******************************************************""" x=[] def main(): t=inin() for _ in range(t): n=inin() a=ain() b=[0]*(2*n+1) x=[] for i in a: b[i]=1 # print(b) for i in a: x.append(i) for j in range(i,len(b)): if(b[j]==0): x.append(j) b[j]=1 break if(len(x)==2*n): print(*x) else: print(-1) ######## Python 2 and 3 footer by Pajenegod and c1729 # Note because cf runs old PyPy3 version which doesn't have the sped up # unicode strings, PyPy3 strings will many times be slower than pypy2. # There is a way to get around this by using binary strings in PyPy3 # but its syntax is different which makes it kind of a mess to use. # So on cf, use PyPy2 for best string performance. py2 = round(0.5) if py2: from future_builtins import ascii, filter, hex, map, oct, zip range = xrange import os, sys from io import IOBase, BytesIO BUFSIZE = 8192 class FastIO(BytesIO): newlines = 0 def __init__(self, file): self._file = file self._fd = file.fileno() self.writable = "x" in file.mode or "w" in file.mode self.write = super(FastIO, self).write if self.writable else None def _fill(self): s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0]) return s def read(self): while self._fill(): pass return super(FastIO,self).read() def readline(self): while self.newlines == 0: s = self._fill(); self.newlines = s.count(b"\n") + (not s) self.newlines -= 1 return super(FastIO, self).readline() def flush(self): if self.writable: os.write(self._fd, self.getvalue()) self.truncate(0), self.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable if py2: self.write = self.buffer.write self.read = self.buffer.read self.readline = self.buffer.readline else: self.write = lambda s:self.buffer.write(s.encode('ascii')) self.read = lambda:self.buffer.read().decode('ascii') self.readline = lambda:self.buffer.readline().decode('ascii') sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip('\r\n') # Cout implemented in Python import sys class ostream: def __lshift__(self,a): sys.stdout.write(str(a)) return self cout = ostream() endl = '\n' # Read all remaining integers in stdin, type is given by optional argument, this is fast def readnumbers(zero = 0): conv = ord if py2 else lambda x:x A = []; numb = zero; sign = 1; i = 0; s = sys.stdin.buffer.read() try: while True: if s[i] >= b'0' [0]: numb = 10 * numb + conv(s[i]) - 48 elif s[i] == b'-' [0]: sign = -1 elif s[i] != b'\r' [0]: A.append(sign*numb) numb = zero; sign = 1 i += 1 except:pass if s and s[-1] >= b'0' [0]: A.append(sign*numb) return A if __name__== "__main__": main()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import sys from collections import Counter def solve(N, B): counts = Counter(B) if any(v > 1 for v in counts.values()): return -1 # Want smallest permutation that is element-wise greater than B C = [] for x in range(1, 2 * N + 1): if x not in counts: C.append(x) # Sorted B and C must be pairable if any(b > c for b, c in zip(sorted(B), C)): return -1 # Brute force with early exit to find min permutation # This passed but I am not sure if it's the intended solution? def gen(index=0): if index == N: yield C return for c, i in sorted(zip(C[index:N], range(index, N))): if B[index] < c: C[index], C[i] = C[i], C[index] yield from gen(index + 1) # Turns out this is never reached! The first permutation will always succeed assert False C[index], C[i] = C[i], C[index] for sol in gen(): A = [] for b, c in zip(B, C): A.append(str(b)) A.append(str(c)) return " ".join(A) return -1 if __name__ == "__main__": input = sys.stdin.readline T = int(input()) for t in range(T): N, = map(int, input().split()) B = [int(x) for x in input().split()] ans = solve(N, B) print(ans)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
def printA(b,n): a = [] if 1 not in b: print("-1") return i = 0 while i != n: curr = b[i] if curr not in a: a.append(curr) nextE = curr+1 condN = True while condN == True: if nextE in b or nextE in a: condN = True else: condN = False break while nextE in b: nextE+=1 while nextE in a: nextE+=1 a.append(nextE) #print(curr, nextE) i+=1 newa = sorted(a) #print(a) for i in range(1,(2*n)+1): if i != newa[i-1]: print("-1") return print(*a) if __name__ == "__main__": t = int(input()) for i in range(t): n = int(input()) b = list(map(int, input().split())) printA(b,n)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
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
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * * @author pttrung */ public class C_Round_623_Div2 { public static long MOD = 998244353; static long[][][] dp; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int t = in.nextInt(); for (int z = 0; z < t; z++) { int n = in.nextInt(); TreeSet<Integer> set = new TreeSet<>(); for (int i = 0; i < 2 * n; i++) { set.add(i); } int[] data = new int[n]; int[] result = new int[2 * n]; boolean ok = true; for (int i = 0; i < n; i++) { data[i] = in.nextInt() - 1; set.remove(data[i]); } for (int i = 0; i < n && ok; i++) { Integer v = set.higher(data[i]); if (v != null) { result[2 * i] = data[i] + 1; result[2 * i + 1] = v + 1; set.remove(v); } else { ok = false; } } if (ok) { for (int i : result) { out.print(i + " "); } out.println(); } else { out.println(-1); } } out.close(); } static boolean check(int x1, int y1, int x2, int y2, int sx, int sy) { int X1 = sx - x1; int Y1 = sy - y1; int X2 = sx - x2; int Y2 = sy - y2; return (X1 * X2 == 0 && Y1 * Y2 > 0) || (X1 * X2 > 0 && Y1 * Y2 == 0); } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return Integer.compare(x, o.x); } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, long b, long MOD) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2, MOD); if (b % 2 == 0) { return val * val % MOD; } else { return val * (val * a % MOD) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
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.Scanner; public class R623_C { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); for (int r = 0; r < t; r++) { int n = in.nextInt(); int[] b = new int[n + 1]; int[] a = new int[2*n + 1]; for (int i = 1; i < n + 1; i++) { int num = in.nextInt(); b[i] = num; a[num] = 1; } int[] res = new int[2*n + 1]; int ans = 0; for (int i = 1; i < n + 1; i++) { res[2*i - 1] = b[i]; int num = b[i] + 1; while (num <= 2*n) { if (a[num] != 1) { res[2*i] = num; a[num]++; break; } num++; } if (num == 2*n + 1) { ans = -1; break; } } if (ans == -1) { System.out.print(-1); } else { for (int i = 1; i < 2 * n + 1; i++) System.out.print(res[i] + " "); } System.out.println(); } in.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.awt.Desktop; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; import java.sql.Array; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.Set; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeSet; import java.util.Vector; public class codechef3 { static class comp implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { if(Math.abs(o1)>Math.abs(o2)) return -1; else return 1; } } static class FastReader {BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static String reverse(String s1) { String s2=""; int n=s1.length(); for(int i=n-1;i>=0;i--) { s2+=s1.charAt(i); } return s2; } public static void main(String[] args) { FastReader s=new FastReader(); int t=s.nextInt(); while(t-->0) { int n=s.nextInt(); ArrayList<Integer> l=new ArrayList<>(); int max=Integer.MIN_VALUE; for(int i=0;i<n;i++) { int x=s.nextInt(); l.add(x); } ArrayList<Integer> l1=new ArrayList<>(); int flag=0; for(int i=0;i<n;i++) { int x=l.get(i); l1.add(x); x+=1; while(1>0) { if(x>2*n) { flag=1; break; }else if(!l.contains(x)&&!l1.contains(x)) { l1.add(x); break; } else x++; } if(flag==1) break; } if(flag==1) System.out.println("-1"); else { for(int i=0;i<2*n;i++) System.out.print(l1.get(i)+" "); System.out.println(); } } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) while(t): t-=1 n=int(input()) a=list(map(int,input().split())) b=[0]*(2*n) f=set() flag=0 a1=a[:] for i in range(n): if(a[i] not in f): f.add(a[i]) b[(2*(i+1))-2]=a[i] else: flag=1 a.sort() if(flag==1): print(-1) continue c=[] for i in range(2*n): if(i+1 not in f): c.append(i+1) c.sort() for i in range(n): for j in range(len(c)): if(c[j]>a1[i]): b[2*(i+1)-1]=c[j] c.remove(c[j]) break if(b.count(0)==0): print(*b) 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 foo(): size=int(input()) b=list(map(int,input().split())) d={} biggest=0 for i in b: d[i]=True answer=[] saved=0 c=True q=[] c=True for i in b: j=1 while True: if not d.get(i+j): d[i+j]=True break else: j+=1 answer.append(i) answer.append(i+j) if i+j>2*len(b): c=False if c: print(*answer) else: print(-1) t=int(input()) for i in range(t): foo()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a == 0) return b; else return gcd(b % a, a); } long long findGCD(vector<long long> arr, long long n) { long long result = arr[0]; for (long long i = 1; i < n; i++) result = gcd(arr[i], result); return result; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(8); cout << fixed; long long t; cin >> t; while (t--) { long long n; cin >> n; bool flag = true; vector<long long> v(n); for (auto &itr : v) cin >> itr; long long maxx = 2 * n; vector<long long> ans; for (auto ele : v) { long long nxt = ele + 1; while (1) { if (nxt > maxx) { flag = false; break; } auto pos = find(v.begin(), v.end(), nxt); auto pos2 = find(ans.begin(), ans.end(), nxt); if (pos == v.end() && pos2 == ans.end()) { ans.push_back(ele); ans.push_back(nxt); break; } else { nxt++; } } if (!flag) break; } if (!flag) cout << "-1"; else for (auto ele : ans) cout << ele << " "; 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
def res(n,l): l1=[] d={} for j in range(1,2*n+1): if j not in l: l1.append(j) for i in l: for j in l1: if j>i: d[i]=j l1.remove(j) break if len(l1)!=0: return False else: return d t=int(input()) for i in range(t): n=int(input()) l=[int(x) for x in input().split()] x=res(n,l) if x==False: print("-1") else: for k in l: print(k,x[k],end=" ") print("\n")
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) M=[] def fonction(n,L): if max(L)>=2*n: return -1 elif len(set(L))!=len(L): return -1 else: res=list() D=list() for k in L: j=k ra=[D[i][1] for i in range(len(D))] while(( j in ra) or (j in L)) and (j<=2*n): j+=1 D.append((k,j)) res.append(k) res.append(j) if sorted(res)==[k for k in range(1,2*n+1)]: return res else: return -1 for i in range(t): n=int(input()) L=[int(x) for x in input().split()] M.append(fonction(n,L)) for k in M: if type(k)==list: dh=" ".join([str(i) for i in k]) for x in dh.split()[:-1]: print(int(x) , end =" ") print(int(dh.split()[-1])) else: print(k)
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 t, n, i, j; int b[105]; int a[210]; bool dd[210]; long tong; int sl; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n; memset(dd, false, sizeof(dd)); tong = 0; for (i = 1; i <= n; i++) { cin >> b[i]; a[2 * i - 1] = b[i]; tong += b[i]; dd[b[i]] = true; } sl = 0; for (i = 2; i <= 2 * n; i += 2) { for (j = a[i - 1] + 1; j <= 300; j++) { if (dd[j] == false) { tong += j; a[i] = j; dd[j] = true; break; } } } sl = 2 * n; if (sl * (sl + 1) != tong * 2) cout << -1; else for (i = 1; i <= 2 * n; i++) cout << a[i] << ' '; cout << '\n'; } return 0; }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t = int(input()) for _ in range(0,t): n = int(input()) bb = [int(i) for i in input().split()] le = [int(i) for i in range(1,2*n+1) if i not in bb] aa = [0]*(2*n) ii = 0 for i in range(0,n): aa[ii] = bb[i] ii += 2 ii = 1 k = True for i in range(0,n): k = [ss for ss in le if ss > bb[i]] if len(k) == 0: k = False break else: kk = min(k) aa[ii] = kk le.remove(kk) ii += 2 if k: print(*aa) 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 os import sys from io import BytesIO, IOBase from bisect import bisect def solution(arr, n): all_set = [i for i in range(1, (2 * n) + 1)] for i, x in enumerate(arr): all_set.remove(arr[i]) res = [] for i in range(n): idx = bisect(all_set, arr[i]) if idx == len(all_set): print(-1) return res.append(arr[i]) res.append(all_set[idx]) all_set.pop(idx) for x in res: print(x, end=' ') print() def main(): t = int(input()) for _ in range(t): n = int(input()) arr = [int(x) for x in input().split()] solution(arr, n) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def input(): return sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main()
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
//package CodeForces; import java.io.*; import java.nio.file.Paths; import java.util.Scanner; public class Solution { private static final boolean onlineJudge = "true".equals(System.getProperty("ONLINE_JUDGE")); public static void main(String[] args) throws IOException { FileInputStream fileStream = null; Scanner scanner = new Scanner(onlineJudge ? System.in : (fileStream = new FileInputStream(Paths.get(System.getProperty("user.dir"), "src/res/input").toString()))); int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(); boolean[] f = new boolean[n << 1]; int[] a = new int[n << 1]; int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = scanner.nextInt(); f[b[i] - 1] = true; } boolean exist = true; for (int i = 0; i < n; i++) { a[i << 1] = b[i]; f[b[i] - 1] = true; if(b[i] == n << 1) { exist = false; break; } int max = -1; for (int j = b[i]; j < n << 1; j++) { if(!f[j]) { max = j + 1; f[j] = true; break; } } if(max == -1) { exist = false; break; } a[(i << 1) + 1] = max; } if(exist) { for(int j = 0; j < a.length; j++) { System.out.print(a[j]); if (j == a.length - 1) { System.out.print("\n"); } else { System.out.print(" "); } } } else { System.out.println(-1); } } if(fileStream != null) { fileStream.close(); } scanner.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> long long MOD = 1000000007; using namespace std; bool sortbysec(const pair<long long, long long> &a, const pair<long long, long long> &b) { return (a.second < b.second); } long long POW(long long a, long long b) { if (b == 0) return 1; if (b == 1) return a % MOD; long long temp = POW(a, b / 2); if (b % 2 == 0) return (temp * temp) % MOD; else return (((temp * temp) % MOD) * a) % MOD; } vector<char> capl = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; vector<char> sml = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; map<int, int> m; for (int i = 1; i < 201; i++) { m[i]++; } for (int i = 0; i < n; i++) { cin >> arr[i]; m[arr[i]]--; } int b[2 * n]; for (int i = 0; i < n; i++) { b[2 * i] = arr[i]; for (auto x : m) { if (x.first > arr[i] && x.second != 0) { b[2 * i + 1] = x.first; m[x.first] = 0; break; } } } int c[2 * n], f = 0; copy(b, b + 2 * n, c); sort(c, c + 2 * n); for (int i = 0; i < 2 * n; i++) { if (c[i] != i + 1) { f = 1; break; } } if (f == 1) { cout << "-1\n"; } else { for (int i = 0; i < 2 * n; i++) { cout << b[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; typedef long long ll; typedef unsigned long long ul; int dx[] = {+1, -1, 0, 0}; int dy[] = {0, 0, +1, -1}; vector<long long> vv; const long long range = 1e7 + 123; bitset<range> prime; void sieve() { prime[0] = 1; prime[1] = 1; for (long long i = 2; i <= range; i++) { if (prime[i] == 0) { for (long long j = i * i; j <= range; j += i) prime[j] = 1; } } vv.push_back(2); for (long long i = 3; i <= range; i += 2) if (prime[i] == 0) vv.push_back(i); } void Main() { int n; cin >> n; map<int, int> mp; long long ar[n]; for (int i = 0; i < n; i++) { cin >> ar[i]; mp[ar[i]]++; } bool bol = true; vector<long long> v; for (int i = 0; i < n; i++) { v.push_back(ar[i]); bool hi = false; for (int j = ar[i]; j <= 2 * n; j++) { if (mp[j] == 0) { v.push_back(j); hi = true; mp[j]++; break; } } if (hi == false) { bol = false; break; } } if (bol) { for (auto u : v) cout << u << " "; cout << endl; } else cout << -1 << endl; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long ttt; cin >> ttt; while (ttt--) { Main(); } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
from collections import defaultdict as dfd for _ in range(int(input())): N = int(input()) A = list(map(int,input().split())) C = dfd(int) for i in range(1,(2*N)+1): C[i] = 0 for i in range(len(A)): C[A[i]] = 1 B = [] # print(C) for i in range(len(A)): B.append(A[i]) z = 0 temp = A[i]+1 while(z!=1): if(C[temp]==0): C[temp]=1 B.append(temp) z = 1 else: temp+=1 # print("Hello") res = 0 for i in range(len(B)): if(B[i]>(2*N)): print(-1) res = 1 break if(res==0): print(*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
x=int(input()) for i in range(x): a=int(input()) l=list(map(int,input().split())) i=0 j=1 while(i<len(l)): while(1): if l[i]+j not in l: l.insert(i+1,l[i]+j) j=1 i+=1 break else: j+=1 i+=1 if (max(l)*(max(l)+1)//2) != sum(l): print(-1) else: print(*l)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for _ in range(int(input())): N = int(input()) A = list(map(int,input().split(" "))) flag = 0 maxi = 2*N Ans = [] mark = {} for i in A: mark[i] =1 for i in A: #print(i,Ans) Ans.append(i) f=0;j=i+1 while j<=maxi: if mark.get(j,-1) == -1: f=1 Ans.append(j) mark[j] = 1 break j+=1 if f==0: flag=1 break if flag == 1: print(-1) else: print(*Ans)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int b[n]; map<int, int> mp; for (int i = 0; i < n; i++) { cin >> b[i]; mp[b[i]]++; } int a[2 * n]; for (int i = 0; i < n; i++) { a[2 * i] = b[i]; a[(2 * i) + 1] = 0; } for (int i = 0; i < 2 * n; i = i + 2) { for (int j = a[i] + 1; j <= 2 * n; j++) { if (mp.find(j) == mp.end()) { a[i + 1] = j; mp[j]++; break; } } if (a[i + 1] == 0) { cout << "-1" << '\n'; return; } } for (int i = 0; i < 2 * n; i++) { cout << a[i] << " "; } cout << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int 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
t = int(input()) for T in range(t): n = int(input()) b = list(map(int,input().split())) ans_arr = [] ans = 0 for i in range(len(b)): # ans_arr.append(b[i]) # # print(ans_arr) # for j in range(1,2*n): # if b[i] + j not in b and b[i] + j not in ans_arr: # ans_arr.append(b[i]+j) # break if b[i]<=2*n: ans_arr.append(b[i]) for j in range(1,2*n+1): if b[i] + j not in b and b[i] + j not in ans_arr: ans_arr.append(b[i]+j) break else: ans = -1 break # print(ans_arr) for i in range(1,2*n): if len(ans_arr)!= 0 and i not in ans_arr: ans = -1 break if ans == -1: print(-1) else: print(*ans_arr)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; int tab[205]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> V; for (int i = 0; i < n; i++) { int x; cin >> x; tab[x] = 1; V.push_back(x); } vector<int> V2; bool b = 1; for (int i : V) { b = 0; for (int j = i + 1; j <= 2 * n; j++) { if (tab[j] == 0) { tab[j] = 1; b = 1; V2.push_back(j); break; } } if (!b) { break; } } if (!b) { cout << "-1\n"; } else { for (int i = 0; i < n; i++) { cout << min(V[i], V2[i]) << " " << max(V[i], V2[i]) << " "; } cout << "\n"; } for (int i = 0; i <= 2 * n; i++) { tab[i] = 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
if __name__ == '__main__': for _ in range(int(input())): length = int(input()) a = *map(int,input().split()), rest = {*range(2*length+1)} -{*a} answer = [] try: for i in a: temp = min(rest-{*range(i)}) rest-={temp} answer+=i,temp print(*answer) except: print(-1)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int p=0;p<t;p++){ int n = s.nextInt(); int[] arr = new int[n]; for(int i=0;i<n;i++) arr[i]=s.nextInt(); int[] prr = new int[2*n]; int next = 0; boolean match=false,exceed=false; boolean sup=false; int k=0; l: for(int i=0;i<2*n-1;i=i+2){ prr[i]=arr[k]; next=arr[k]+1; while(true){ sup=false; while(true){ match=false; for(int j=0;j<n;j++){ if(next==arr[j]){ next++; match=true; sup=true; } if(next>2*n){ exceed=true; break l; } } if(!match) break; } while(true){ match=false; for(int j=0;j<2*n;j++){ if(next==prr[j]){ next++; match=true; sup=true; } if(next>2*n){ exceed=true; break l; } } if(!match) break; } if(!sup) break; } prr[i+1]=next; k++; } if(exceed) System.out.print("-1"); else for(int i=0;i<2*n;i++) System.out.print(prr[i] + " "); System.out.println(); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
# -*- coding: utf-8 -*- """ Created on Sat Feb 29 05:27:25 2020 @author: hp """ t = int(input()) for i in range(t): n = int(input()) lst = [int(j) for j in input().split()] a=[0]*2*n y = True for j in range(n): a[2*j] = lst[j] for j in range(n): x = a[2*j] + 1 z = True k=0 while(z==True): if(x+k not in a): a[2*j+1]=x+k z =False k+=1 for j in range(2*n): if(j+1 not in a): y = False break if(y==False): print(-1) else: for j in a: print(j,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 java.util.Scanner; public class JavaApplication2 { public static void main(String[] args) { Scanner s=new Scanner(System.in); int t=s.nextInt(); String ss=""; int counter=0; for(int i=0;i<t;i++){ int n=s.nextInt(); boolean doo=true; int[]ar=new int[n]; int []an=new int[2*n]; for(int o=0;o<an.length;o++) an[o]=o+1; for(int x=0;x<n;x++){ ar[x]=s.nextInt(); if(ar[x]>=2*n) doo=false; } if(doo){ for(int is=0;is<ar.length;is++){ for(int c=0;c<an.length;c++){ if(ar[is]<an[c]&&(exist(ar,an[c]))){ ss+=ar[is]+" "+an[c]+" "; an[c]=0; counter++; break; } } } if(counter<n){ System.out.println(-1); ss=""; } else{ System.out.println(ss); ss=""; } counter=0; } else{System.out.println(-1);} } } public static boolean exist(int ar[],int s){ boolean x=true; int gg; for(int d=0;d<ar.length;d++) if(ar[d]==s) x=false; return x; } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.util.*; public class code_forces { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s= new Scanner(System.in); int t= s.nextInt(); outer: while(t-->0) { int n= s.nextInt(); int[] arr= new int[n]; for(int i=0; i<arr.length;i++) { arr[i] = s.nextInt(); } int[] help= new int[2*n+1]; int[] ans = new int[2*n]; for(int i=0; i<arr.length;i++) { ans[2*i]= arr[i]; help[arr[i]]=1; } for(int i=0; i<ans.length;i++) { if(ans[i]==0) { int j= ans[i-1]; while(j<help.length && help[j]!=0) { j++; } if(j<help.length && help[j]==0) { ans[i] = j; help[j]=1; } else { System.out.println("-1"); continue outer; } } } for(int i=0; i<ans.length;i++) { System.out.print(ans[i]+" "); } System.out.println(); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; using LL = long long; constexpr int N = 1e5 + 5; int a[N], b[N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; set<int> st; for (int i = 1; i <= 2 * n; i++) st.insert(i); for (int i = 1; i <= n; i++) { cin >> b[i]; st.erase(b[i]); } bool ok = true; for (int i = 1; i <= n; i++) { auto it = st.lower_bound(b[i]); if (it == st.end()) { ok = false; break; } a[i] = *it; st.erase(it); } if (!ok) { cout << "-1\n"; continue; } for (int i = 1; i <= n; i++) { if (a[i] > b[i]) swap(a[i], b[i]); cout << a[i] << ' ' << b[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
t = int(input()) for _ in range(t): n = int(input()) b = list(map(int,input().split())) l = [i for i in range(1,2*n+1) if i not in b] a=[] for i in b: a.append(i) for j in l: if j>i: a.append(j) l.remove(j) break if len(a)==2*n: 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 javax.management.DynamicMBean; import javax.print.DocFlavor; import javax.print.attribute.HashAttributeSet; import java.awt.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.file.attribute.FileAttribute; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; import java.util.List; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; import java.util.Arrays; import java.util.concurrent.Callable; import java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc = new FastReader(); int t=sc.nextInt(); for(int i=0;i<t;i++){ int n=sc.nextInt(); int[] arr=new int[n]; HashSet<Integer> set=new HashSet<>(); for(int j=0;j<n;j++){ arr[j]=sc.nextInt(); set.add(arr[j]); } int p=0,q=0; int[] a=new int[2*n]; int flag=1,index=0; for(int j=0;j<n;j++){ p=arr[j]; q=p+1; if(q>2*n){ flag=0; break; } while(set.contains(q)){ q++; if(q>2*n) { flag=0; break; } } if(flag==0){ break; } set.add(q); a[index]=p; a[index+1]=q; index+=2; } if(flag==0){ System.out.print(-1); } else{ for(int j=0;j<2*n;j++){ System.out.print(a[j]+" "); } } System.out.println(" "); } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
import java.io.*; import java.util.*; public class Main { private static void solver(InputReader sc, PrintWriter out) throws Exception { int test = sc.nextInt(); for(int ii=0; ii<test; ii++){ int n = sc.nextInt(); Set<Integer> hs = new HashSet<>(); int arr[] = new int[n]; for(int i=0; i<n; i++){ int x = sc.nextInt(); hs.add(x); arr[i] = x; } List<Integer> al = new ArrayList<>(); boolean ans = true; for(int xx : arr){ int max = xx+1; while(true){ if(hs.contains(max)) max++; else break; } // out.println(max); if(max > (2 * n)){ ans = false; break; } al.add(xx); al.add(max); hs.add(max); } if(!ans) out.println(-1); else{ for(int xx : al) out.print(xx+" "); out.println(); } } } private static long gcd(long a, long b){ if(b==0) return a; return gcd(b, a%b); } public static void main(String[] args) throws Exception { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); solver(in,out); 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 nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble(){ return Double.parseDouble(next()); } } } class Pair{ int x,y,z; Pair(int x, int y, int z){ this.x = x;this.y = y; this.z = z; } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
""" from collections import Counter t=int(input()) for i in range(t): n = int(input()) nums = list(map(int, input().split())) c = Counter(nums) ans = 0 for i,j in c.items(): ans += j//i c[i] = j%i print(c) print(ans)""" """ from functools import lru_cache @lru_cache(maxsize=None) def findMinMax(s): if len(s)==1: return int(s[0])**2 return int(s[0])*int(s[-1]) t = int(input()) for i in range(t): a,k = list(map(int, input().split())) for i in range(k-1): l = "".join(sorted(str(a))) if "0" in l: break else: a += findMinMax(l) print(a)""" t = int(input()) for i in range(t): n = int(input()) nums = list(map(int, input().split())) ans = [0]*(2*n) for i in range(0,n*2,2): ans[i] = nums[i//2] left = sorted(set(range(1,2*n+1))-set(nums)) for j in range(1,n*2, 2): for k in left: if k > ans[j-1]: ans[j]=k left.remove(k) break if 0 in ans: print(-1) else: print(" ".join(list(map(str, ans))))
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 20; const int inf = 0x3f3f3f3f; const int modd = 1e9 + 7; inline int read() { int x = 0, f = 1; char c = getchar(); while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') f = -1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return f * x; } template <class T> inline void sc(T &x) { char c; x = 0; while ((c = getchar()) < '0') ; while (c >= '0' && c <= '9') x = x * 10 + (c - 48), c = getchar(); } inline long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } inline long long exgcd(long long a, long long b, long long &x, long long &y) { long long d; (b == 0 ? (x = 1, y = 0, d = a) : (d = exgcd(b, a % b, y, x), y -= a / b * x)); return d; } inline long long qpow(long long a, long long n) { long long sum = 1; while (n) { if (n & 1) sum = sum * a % modd; a = a * a % modd; n >>= 1; } return sum; } inline long long qmul(long long a, long long n) { long long sum = 0; while (n) { if (n & 1) sum = (sum + a) % modd; a = (a + a) % modd; n >>= 1; } return sum; } inline long long inv(long long a) { return qpow(a, modd - 2); } struct Coke { int v, id; } a[maxn]; bool cmp1(Coke c, Coke d) { return c.v < d.v; } bool cmp2(Coke c, Coke d) { return c.id < d.id; } int vis[205]; int ans[maxn]; vector<int> vec; int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); for (int i = 1; i <= 2 * n; i++) vis[i] = 0; vec.clear(); for (int i = 1; i <= n; i++) scanf("%d", &a[i].v), a[i].id = i, ans[i * 2 - 1] = a[i].v; sort(a + 1, a + 1 + n, cmp1); for (int i = 1; i <= n; i++) vis[a[i].v] = a[i].id; int now = 0, flag = 1; for (int i = 1; i <= 2 * n; i++) { if (vis[i]) now++; else now--; if (now < 0) { flag = 0; break; } } if (!flag) { printf("-1\n"); continue; } for (int i = 1; i <= 2 * n; i++) if (!vis[i]) vec.push_back(i); sort(a + 1, a + 1 + n, cmp2); for (int i = 1; i <= n; i++) { auto it = lower_bound(vec.begin(), vec.end(), a[i].v); ans[a[i].id * 2] = *it; vec.erase(it); } for (int i = 1; i <= 2 * n; i++) printf("%d%c", ans[i], " \n"[i == 2 * n]); } }
CPP
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t=int(input()) for g in range(0,t): n=int(input()) b=list(map(int,input().split())) bnew=[] bnew.extend(b) bnew.sort() a=[] j=0 for i in range(1,2*n+1): if(j<n and i==bnew[j]): j=j+1 else: a.append(i) breaks=0 for i in range(0,n): if(bnew[i]<a[i]): continue else: breaks=1 break if(breaks==1): print(-1) else: for i in range(0,n): for j in range(0,len(a)): if(b[i]<a[j]): print(b[i],a[j],end=" ") a.remove(a[j]) 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
import math from sys import stdin as c n=int(input()) a=[] for i in range(n): input() a.append(list(map(int,c.readline().split(' ')))) def higher(ar,val): u=len(ar)-1 if u==0: return 0 if u==-1: return -1 elif ar[u//2]>val: return higher(ar[:u//2+1],val) else: u1=higher(ar[u//2+1:],val) if u1>-1: u1+=u//2+1 return u1 for i in a: n=len(i) s=set(i) n2=set(range(1,2*n+1)) n2-=s d=dict(zip(i,range(n))) n2=list(n2) if n!=len(n2): print(-1) continue odp=[] b=True for j in range(n): h=higher(n2,i[j]) if i[j]<n2[h]: odp.append(i[j]) odp.append(n2[h]) del n2[h] else: print(-1) b=False break if b: print(*odp)
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
//package com.kraken.cf.cf623; /* * Author: Kraken * Created on 2/25/2020 at 3:50 AM */ import java.io.*; import java.util.*; public class C { public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int[] b = new int[n], x = new int[2 * n + 1]; for (int i = 0; i < n; i++) { b[i] = sc.nextInt(); x[b[i]]++; } StringBuilder res = new StringBuilder(); boolean found = false; for (int i = 0; i < n; i++) { res.append(b[i]).append(" "); found = false; for (int j = b[i] + 1; j < 2 * n + 1; j++) { if (x[j] == 0 && j > b[i]) { x[j] = 1; res.append(j).append(" "); found = true; break; } } if (!found) { break; } } if (!found) System.out.println(-1); else System.out.println(res.toString()); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
t = int(input()) while t: t -= 1 n = int(input()) vis = [False] * (2 * n) ans = [-1] * (2 * n) b = list(map(int, input().split())) def check(): global vis for i in b: if vis[i - 1] is False: vis[i - 1] = True else: return False return True if check() is False: print(-1) continue def solve(): global ans, vis for i in range(0, 2 * n, 2): j = i // 2 ans[i] = b[j] for k in range(b[j] + 1, 2 * n + 1): if vis[k - 1] is False: vis[k - 1] = True ans[i + 1] = k break if ans[i + 1] == -1: return False return True if solve() is False: print(-1) continue else: print(*ans)
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
for p in range(int(input())): n=int(input()) a=list(map(int,input().split())) b=[0]*(2*n) for i in a: b[i-1]+=1 m=0 c=[] for i in range(n): j=a[i] while(j<(2*n)): if b[j]==0: b[j]+=1 c.append(j+1) m+=1 break else: j+=1 if m!=n: print(-1) else: for i in range(n): print(a[i],c[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 java.util.*; import java.io.*; public class C623 { public static void main(String [] args) { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int t = sc.nextInt(); while (t > 0) { int n = sc.nextInt(); int [] res = new int [2*n]; TreeSet <Integer> sorted = new TreeSet <Integer> (); for (int i = 1; i <= 2*n; i++) { sorted.add(i); } for (int i = 0; i < n; i++) { res[2*i] = sc.nextInt(); sorted.remove(res[2*i]); } for (int i = 0; i < n; i++) { Integer ideal = sorted.ceiling(res[2*i]); if (ideal == null) { } else { res[2*i + 1] = ideal; sorted.remove(ideal); } } boolean bad = false; for (int i = 0; i < res.length; i++) { if (res[i] == 0) { bad = true; break; } } if (bad) { out.println(-1); } else { for (int i = 0; i < res.length; i++) { out.print(res[i] + " "); } out.println(); } t--; } out.close(); } //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
JAVA
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> int main() { int t; scanf("%d", &t); while (t--) { int n, b[100] = {0}; bool used[201] = {false}; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &b[i]); used[b[i]] = true; } bool ok = true; int ans[200]; for (int i = 0; i < n; i++) { ans[2 * i] = b[i]; int j = b[i] + 1; while (j <= 2 * n && used[j]) j++; if (j > 2 * n) { ok = false; break; } ans[2 * i + 1] = j; used[j] = true; } if (ok) { for (int i = 0; i < 2 * n; i++) printf("%d ", ans[i]); puts(""); } else { puts("-1"); } } 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 tr2 { static PrintWriter out; static StringBuilder sb; static long inf = (long) 1e10; static ArrayList<Integer> primes; static ArrayList<Integer>[] ad; static int[] si; static long mod = (long) 998244353; static long num = (long) 1e9 + 7; static long[][] memo; static int n; static long k; static double m; static int[] a; static char[] s; static TreeSet<Integer> pr; static ArrayList<Integer> factors, times; static TreeSet<Integer> taken, ans; static TreeMap<pair1, Integer> edge; static boolean[] vis; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); out = new PrintWriter(System.out); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int np=n*2; TreeSet<Integer> ts=new TreeSet<>(); for(int i=1;i<=np;i++) ts.add(i); int []a=new int [n]; boolean f=true; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); if(!ts.contains(a[i])) { f=false; break; } ts.remove(a[i]); } int []ans=new int [np]; int id=0; for(int i=0;i<n;i++) { if(ts.ceiling(a[i])==null) { f=false; break; } int x=ts.ceiling(a[i]); ans[id++]=a[i]; ans[id++]=x; ts.remove(x); } if(f) { for(int i=0;i<np;i++) out.print(ans[i]+" "); out.println(); } else out.println(-1); } out.flush(); } public static class pair1 implements Comparable<pair1> { int x; int y; pair1(int a, int i) { x = a; y = i; // id = g; } public String toString() { return x + " " + y; } @Override public int compareTo(pair1 o) { if (x == o.x) return y - o.y; return x - o.x; } } static long inver(long x) { long a = x; long e = (mod - 2); long res = 1; while (e > 0) { if ((e & 1) == 1) { res = ((1l * res * a) % mod); } a = (int) ((1l * a * a) % mod); e >>= 1; } return res % mod; } static class eq implements Comparable<eq> { int x; int y; long z; eq(int x, int y, long z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } @Override public int compareTo(eq o) { if (x == o.x) if (o.y == y) return Long.compare(z, o.z); else return y - o.y; else return x - o.x; } } static void seive() { si = new int[1000001]; primes = new ArrayList<>(); int N = 1000001; si[1] = 1; for (int i = 2; i < N; i++) { if (si[i] == 0) { si[i] = i; primes.add(i); pr.add(i); } for (int j = 0; j < primes.size() && primes.get(j) <= si[i] && (i * primes.get(j)) < N; j++) si[primes.get(j) * i] = primes.get(j); } } static long fac(int n) { if (n == 0) return 1; if (n == 1) return 1; long ans = 1; for (int i = 1; i <= n; i++) ans = (i % mod * ans % mod) % mod; return ans % mod; } static int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } static class pair implements Comparable<pair> { int num; int v; pair(int n, int v) { num = n; this.v = v; } public String toString() { return num + " " + v; } public int compareTo(pair o) { return o.v - v; } } static class unionfind { int[] p; int[] size; unionfind(int n) { p = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { p[i] = i; } Arrays.fill(size, 1); } int findSet(int v) { if (v == p[v]) return v; return p[v] = findSet(p[v]); } boolean sameSet(int a, int b) { a = findSet(a); b = findSet(b); if (a == b) return true; return false; } int max() { int max = 0; for (int i = 0; i < size.length; i++) if (size[i] > max) max = size[i]; return max; } void combine(int a, int b) { a = findSet(a); b = findSet(b); if (a == b) return; if (size[a] > size[b]) { p[b] = a; size[a] += size[b]; } else { p[a] = b; size[b] += size[a]; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream system) { br = new BufferedReader(new InputStreamReader(system)); } public Scanner(String file) throws Exception { br = new BufferedReader(new FileReader(file)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public char nextChar() throws IOException { return next().charAt(0); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public boolean ready() throws IOException { return br.ready(); } public void waitForInput() throws InterruptedException { Thread.sleep(3000); } } }
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
tests = int(input()) for _ in range(tests): size = int(input()) full = set([i for i in range(1,2*size+1)]) restore = list(map(int,input().split())) check = sorted(full - set(restore)); capture = [] for res in restore: found = False for ch in check: if ch > res: capture.append(str(res)) capture.append(str(ch)) check.remove(ch) found = True break if not found: print (-1); break if len(capture) == 2*size: print(" ".join(capture))
PYTHON3
1315_C. Restoring Permutation
You are given a sequence b_1, b_2, …, b_n. Find the lexicographically minimal permutation a_1, a_2, …, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible. Input Each test contains one or more test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). The first line of each test case consists of one integer n β€” the number of elements in the sequence b (1 ≀ n ≀ 100). The second line of each test case consists of n different integers b_1, …, b_n β€” elements of the sequence b (1 ≀ b_i ≀ 2n). It is guaranteed that the sum of n by all test cases doesn't exceed 100. Output For each test case, if there is no appropriate permutation, print one number -1. Otherwise, print 2n integers a_1, …, a_{2n} β€” required lexicographically minimal permutation of numbers from 1 to 2n. Example Input 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 Output 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
2
9
#include <bits/stdc++.h> using namespace std; vector<long long> vin(long long n) { vector<long long> a(n); for (long long i = 0; i < n; i += 1) cin >> a[i]; return a; } long long intin() { long long n; cin >> n; return n; } char charin() { char a; cin >> a; return a; } string strin() { string s; cin >> s; return s; } long long factorial(long long n) { return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); } void solve() { long long n; cin >> n; vector<long long> a = vin(n); set<long long> s; for (long long i = 1; i < 2 * n + 1; i += 1) s.insert(i); for (auto i : a) { if (s.find(i) == s.end()) { cout << -1 << endl; return; } s.erase(i); } vector<long long> b(2 * n, 0); for (long long i = 0; i < n; i += 1) { b[2 * i] = a[i]; } for (long long i = 1; i < 2 * n; i += 2) { auto x = upper_bound(s.begin(), s.end(), b[i - 1]); if (x == s.end()) { cout << -1 << endl; return; } s.erase(x); b[i] = *x; } for (long long i = 0; i < n; i += 1) { if (a[i] != min(b[2 * i], b[2 * i + 1])) { cout << -1 << endl; return; } } for (auto i : b) cout << i << " "; cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; for (long long _ = 0; _ < t; _ += 1) { 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; int n, b[105]; void solve() { vector<int> a; vector<int> us(205, 0); cin >> n; for (int i = 0; i < n; i++) { cin >> b[i]; us[b[i]] = 1; } for (int i = 0; i < n; i++) { a.push_back(b[i]); int flag = 0; for (int j = 1; j <= 2 * n; j++) { if (us[j] == 0 && j > b[i]) { flag = 1; a.push_back(j); us[j] = 1; break; } } if (flag == 0) { cout << -1; return; } } for (int i = 0; i < a.size(); i++) { cout << a[i] << " "; } } int main() { int t = 1; cin >> t; while (t--) { solve(); cout << endl; } }
CPP