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 | # N, M, P = map(int, input().split())
# A = list(map(int, input().split()))
T = int(input())
for t in range(T):
N = int(input())
B = list(map(int, input().split()))
S = set(B)
out = []
for n in range(2*N):
if n%2 == 0:
out.append(str(B[n//2]))
else:
curr = B[(n-1)//2]
fl = 0
for i in range(1, (N*2)+1):
if i > curr and i not in S:
out.append(str(i))
S.add(i)
fl = 1
break
if fl == 0:
break
if fl == 0:
print(-1)
else:
print(" ".join(out))
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Unstoppable solver = new Unstoppable();
int t=in.nextInt();
while(t-->0)
solver.solve(in, out);
out.close();
}
static class Unstoppable {
public void solve(InputReader in, PrintWriter out) {
int n=in.nextInt();
int a[]=new int[n];
int b[]=new int[n];
int flag=0;
HashSet<Integer> h=new HashSet<>();
for(int i=0;i<n;i++) { a[i]=in.nextInt(); h.add(a[i]); }
for(int i=0;i<n;i++)
{
for(int j=a[i]+1;j<=n*2;j++)
{
if(!h.contains(j)) { h.add(j); b[i]=j; break; }
}
if(b[i]==0){flag=1; break; }
}
if(flag==1) out.println("-1");
else{
for(int i=0;i<n;i++){ out.print(a[i]+" "+b[i]+" "); } out.println("");
}
}
}
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 long nextLong(){
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const long long INF = 1e18;
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T>
T gcd(T a, T b) {
if (b == 0) return a;
return gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
return a / gcd(a, b) * b;
}
template <typename T>
T pow(T a, int b) {
return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1;
}
const int mod = 1000000007;
long long modpow(long long a, int b) {
return b ? modpow(a * a % mod, b / 2) * (b % 2 ? a : 1) % mod : 1;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
for (int i = 0; i < ((int)vec.size()); ++i) {
if (i) os << " ";
os << vec[i];
}
return os;
}
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
os << p.first << " " << p.second;
return os;
}
template <class T>
inline void add(T& a, int b) {
a += b;
if (a >= mod) a -= mod;
}
void solve();
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--) {
solve();
}
}
void solve() {
int n;
cin >> n;
vector<int> b(n);
for (int i = 0; i < (n); ++i) cin >> b[i];
vector<int> a(2 * n);
for (int i = 0; i < (n); ++i) a[2 * i] = b[i];
set<int> t;
for (int i = 0; i < (n); ++i) t.insert(b[i]);
set<int> s;
for (int i = 0; i < (2 * n); ++i)
if (!t.count(i + 1)) s.insert(i + 1);
for (int i = 0; i < (n); ++i) {
auto it = s.upper_bound(b[i]);
if (it == s.end()) {
cout << -1 << endl;
return;
}
a[2 * i + 1] = *it;
s.erase(it);
}
cout << a << endl;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 5e5 + 10;
int b[105], a[205], n;
int main() {
int t;
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; i++) cin >> b[i];
map<int, int> mp;
set<int> s;
for (int i = 1, j = 1; i <= 2 * n; i += 2, j++) {
a[i] = b[j];
mp[a[i]] = 1;
}
for (int i = 1; i <= 2 * n; i++) {
if (!mp[i]) s.insert(i);
}
bool flag = 1;
for (int i = 2; i <= 2 * n; i += 2) {
if (s.upper_bound(a[i - 1]) == s.end()) {
flag = 0;
break;
}
auto it = *s.upper_bound(a[i - 1]);
a[i] = it;
s.erase(it);
}
if (!flag)
cout << -1 << endl;
else {
for (int i = 1; i <= 2 * n; i++) cout << a[i] << " ";
cout << endl;
}
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | t=int(input())
elt=[]
for i in range(t):
n=int(input())
l=[]
c=[]
chaine=input()
el=chaine.split(" ")
for e in el:
l.append(int(e))
c.append(int(e))
elt.append((n,c,l))
for n,c,l in elt:
for k in range(2*n):
m=0
if k+1 not in l:
g=0
for p in c:
if k+1>p:
l.insert(l.index(p)+1,k+1)
c.remove(p)
g=1
break
if g==0:
m=-1
print(-1)
break
if m==0:
for g in l:
print(g,end=' ')
print('')
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int b[100];
int a[201];
int main(void) {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
set<int> left;
for (int i = 1; i <= (2 * n); i++) {
left.insert(i);
}
for (int i = 0; i < n; i++) {
cin >> b[i];
left.erase(b[i]);
}
int can = 1;
for (int i = 0; i < n; i++) {
a[2 * i] = b[i];
auto it = left.upper_bound(b[i]);
if (it == left.end()) {
can = 0;
break;
}
a[2 * i + 1] = *it;
left.erase(it);
}
if (!can)
cout << "-1\n";
else {
for (int i = 0; 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(t):
n = int(input())
b = list(map(int, input().split()))
flag = True
s = set(b)
ans = []
for i in range(n):
ans.append(b[i])
k = b[i]
while(k in s):
k += 1
if k > n*2:
flag = False
break
ans.append(k)
s.add(k)
if flag:
print(" ".join(map(str, ans)))
else:
print(-1)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.util.*;
import java.math.*;
public class HelloWorld {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
while(t-- > 0) {
int n = scanner.nextInt();
int[] arr = new int[n];
boolean[] a = new boolean[2 * n + 1];
for(int i =0 ; i < n; i++) {
arr[i] = scanner.nextInt();
a[arr[i]] = true;
}
int[] result = new int[2 * n];
boolean works = true;
for(int i = 0; i < arr.length; i++) {
int current = arr[i];
result[i * 2] = current;
while(current < a.length && a[current++]) {
continue;
}
current--;
if(a[current]) {
System.out.println(-1);
works = false;
break;
}
a[current] = true;
result[i * 2 + 1] = current;
}
if(!works) {
continue;
}
for(int i = 0; i < result.length; i++) {
System.out.print(result[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>
#pragma GCC optimize("Ofast")
using namespace std;
const long double PI = acos(-1.0L);
const long long MOD = 1000000007LL;
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
void solve() {
int N;
cin >> N;
vector<int> B(N);
for (int i = 0; i < (int)(N); ++i) cin >> B[i];
set<int> ppp, dic;
for (int i = 0; i < (int)(N); ++i) ppp.emplace(B[i]);
for (int i = 0; i < (int)(2 * N); ++i)
if (!ppp.count(i + 1)) dic.emplace(i + 1);
vector<int> ans(2 * N);
for (int i = 0; i < (int)(N); ++i) {
ans[i * 2] = B[i];
bool ok = false;
for (auto j : dic) {
if (j > B[i]) {
ans[i * 2 + 1] = j;
ok = true;
break;
}
}
if (!ok) {
cout << -1 << '\n';
return;
} else
dic.erase(ans[i * 2 + 1]);
}
for (int i = 0; i < (int)(2 * N); ++i) cout << ans[i] << " ";
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int Q;
cin >> Q;
while (Q--) {
solve();
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | testcases=int(input())
masterlist=[]
for i in range(testcases):
input()
tempseq=list(map(int,input().split(" ")))
masterlist.append(tuple(tempseq))
def lexicographer(tup):
buildlist=[]
for i in range(len(tup)):
val=tup[i]
buildlist.append(val)
run=True
orig=val+1
while run==True:
if orig in buildlist or orig in tup:
orig+=1
else:
buildlist.append(orig)
run=False
maxim=max(buildlist)
for i in range(1,maxim):
if i not in buildlist:
return [-1]
return buildlist
for sequence in masterlist:
ans=lexicographer(sequence)
str=""
for i in ans:
str+="{} ".format(i)
print(str)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.util.*;
import java.io.*;
public class Main {
/**
*
* k = 1
*
* 2^17+1 2^17 0
* 1 2^17+1 1
*
* dp[2][2] = max(
*/
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
outer: while (t-->0) {
int n = sc.nextInt();
int[] arr = new int[n + 1];
for (int i = 1; i <= n; i++) {
arr[i] = sc.nextInt();
}
int[] ans = new int[2 * n + 1];
boolean[] used = new boolean[2*n+1];
for (int i = 1; i <= n; i++) {
ans[2 * i - 1] = arr[i];
used[arr[i]] = true;
}
for (int i = 2; i <= 2 * n; i+=2) {
int val = ans[i-1] + 1;
while (val <= 2 * n && used[val]) {
val++;
}
if (val > 2 * n) {
System.out.println(-1);
continue outer;
}
used[val] = true;
ans[i] = val;
}
for (int i = 1; i <= 2*n; i++) {
System.out.print(ans[i] + " ");
}
System.out.println();
}
}
// private static void solve(int[] ans, int n, int[] arr) {
// boolean[] used = new boolean[2*n+1];
// for (int i = 1; i <= n; i++) {
// ans[2 * i - 1] = arr[i];
// used[arr[i]] = true;
// }
// for (int i = 2; i <= 2 * n; i+=2) {
// int val = ans[i-1] + 1;
// while (val <= 2 * n && used[val]) {
// val++;
// }
// if (val > 2 * n) {
// System.out.println(-1);
// return;
// }
// used[val] = true;
// ans[i] = val;
// }
//
// for (int i = 1; i <= 2*n; i++) {
// System.out.print(ans[i] + " ");
// }
// System.out.println();
// }
/**\
*
* 4
* 2 3 4 5
*
*
* 1 2 3 4 5 6 7 8
* 2 6 3 7 4 8 5 9
*/
// 1 1 1 0 0 0
// 0 0 1 1 1 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;
}
}
}
/**
* 7 3
* 3 1 5
* 9 6 5
*
* i = 1: 1
* i = 2: 2
* i = 3: 3
*
*
* 1 2 3 3 3 3 3
*/
/**
*
* 1+1
**/ | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import sys
input = sys.stdin.readline
def fastio():
from io import StringIO
from atexit import register
global input
sys.stdin = StringIO(sys.stdin.read())
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.stdout = StringIO()
register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()
INF = 10**20
MOD = 998244353
I = lambda:list(map(int,input().split()))
from math import gcd
from math import ceil
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
t, = I()
while t:
t -= 1
n, = I()
a = I()
ans = [0] * (2 * n)
v = [0] * (2 * n + 1)
for i in range(0, 2 * n, 2):
ans[i] = a[i // 2]
v[ans[i]] = 1
for i in range(1, 2 * n, 2):
cur = ans[i - 1]
for j in range(1, 2 * n + 1):
if v[j] == 0 and j > cur:
v[j] = 1
ans[i] = j
break
else:
print(-1)
break
else:
print(*ans) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long b[n];
unordered_map<long long, long long> mapp;
for (long long i = 0; i < n; i++) {
cin >> b[i];
mapp[b[i]]++;
}
long long a[n];
long long x = 0;
long long z = 0;
for (long long i = 0; i < n; i++) {
bool chk = false;
for (long long j = b[i] + 1; j <= 2 * n; j++) {
if (mapp[j]) {
continue;
} else {
chk = true;
a[x] = j;
mapp[a[x]]++;
x++;
break;
}
}
if (chk == false) {
z++;
break;
}
}
if (z) {
cout << "-1" << '\n';
} else {
for (long long i = 0; i < n; i++) {
cout << b[i] << " " << a[i] << " ";
}
cout << '\n';
}
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | for _ in range(int(input())):
n=int(input());A=list(map(int,input().split()));mark=[];C=[]
for i in A:
m=i+1
while True :
if m in A or m in C:
m+=1
else:
break
mark.append(i);mark.append(m);C.append(m)
Ans=[];jaya=mark.copy();jaya.sort()
for i in range(1,2*n+1):
Ans.append(i)
if jaya==Ans:
print(*mark)
else:
print(-1)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.Queue;
import java.util.StringTokenizer;
import java.math.BigInteger;
public class hello2
{static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static String sum (String s)
{
String s1 = "";
if(s.contains("a"))
s1+="a";
if(s.contains("e"))
s1+="e";
if(s.contains("i"))
s1+="i";
if(s.contains("o"))
s1+="o";
if(s.contains("u"))
s1+="u";
return s1;
}
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];
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i=0;i<n;i++){
b[i]=sc.nextInt();
if(max<b[i]){
max=b[i];
}
if(min>b[i]){
min=b[i];
}
}
if(max==2*n){
System.out.println(-1);
continue;
}
else if(min!=1){
System.out.println(-1);
continue;
}
else{
int arr[] = new int[2*n];
int count[] = new int[2*n+1];
int ind=0;
for(int i=0;i<n;i++){
arr[ind]=b[i];
count[arr[ind]]=1;
ind+=2;
}
int f=0;
for(int i=0;i<2*n;i++){
if(arr[i]>0){continue;}
else{
int x = arr[i-1]+1;
while(x<=2*n && count[x]==1){
x++;
}
if(x>2*n){
x=1;
while(x<=2*n && count[x]==1){
x++;
}
if(x<arr[i-1]){
f=1;
break;
}
arr[i]=x;
count[x]=1;
}
else{
arr[i]=x;
count[x]=1;
}
}
}
if(f==1){
System.out.println(-1);
continue;
}
for(int i=0;i<2*n;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | # import sys
def main():
for t in range(int(input())):
n = int(input())
s = [i for i in range(1,2*n+1)]
l = [int(j) for j in input().split()]
for i in range(n):
s.remove(l[i])
z = list(zip(l, [i for i in range(n)]))
# z.sort()
s.sort()
ans = [0]*(2*n)
# print(z, s)
for i in range(n):
num = z[i][0]
# print(s, ans)
for j in s:
if j>num:
ans[2*z[i][1]]=num
ans[2*z[i][1]+1]=j
s.remove(j)
break
else:
print(-1)
break
else:
print(" ".join(map(str, ans)))
# ans.append((z[i][0], s[i], z[i]))
# ans.append(s[i])
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;
long long int dif(long long int a, long long int b) {
return ((a / b) + (a % b != 0));
}
vector<long long int> v;
long long int c[100100], par[100100];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t;
cin >> t;
while (t--) {
long long int n, f = 0, k;
cin >> n;
pair<long long int, long long int> a[n], sol[2 * n];
v.clear();
for (long long int i = 0; i < 2 * n + 2; i++) {
par[i] = 0;
c[i] = 0;
}
for (long long int i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i;
c[a[i].first] = 1;
if (a[i].first >= 2 * n) {
f = 1;
}
}
if (f) {
cout << -1 << '\n';
continue;
}
sort(a, a + n);
for (long long int i = 1; i <= 2 * n; i++) {
if (!c[i]) v.push_back(i);
}
for (long long int i = 0; i < n; i++) {
k = lower_bound(v.begin(), v.end(), a[i].first) - v.begin();
if (k == v.size()) {
f = 1;
break;
}
par[a[i].first] = v[k];
v.erase(v.begin() + k);
}
if (f) {
cout << -1 << '\n';
continue;
}
for (long long int i = 0; i < n; i++) {
sol[a[i].second] = {a[i].first, par[a[i].first]};
}
for (long long int i = 0; i < n; i++) {
for (long long int j = i + 1; j < n; j++) {
if (sol[i].second > sol[j].second && sol[i].first < sol[j].second &&
sol[j].first < sol[i].second) {
swap(sol[j].second, sol[i].second);
}
}
}
for (long long int i = 0; i < n; i++) {
cout << min(sol[i].first, sol[i].second) << " "
<< max(sol[i].first, sol[i].second) << " ";
}
cout << '\n';
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int b[n + 1];
int i, j;
for (int i = 1; i <= n; i++) cin >> b[i];
int a[2 * n + 1];
bool vis[2 * n + 1];
for (int i = 0; i <= 2 * n; i++) vis[i] = false;
for (int i = 1; i <= n; i++) {
a[2 * i - 1] = b[i];
vis[b[i]] = true;
}
bool flag = false;
for (int i = 1; i <= n; i++) {
flag = false;
for (j = b[i] + 1; j <= 2 * n; j++) {
if (!vis[j]) {
a[2 * i] = j;
vis[j] = true;
flag = true;
break;
}
}
if (!flag) {
break;
}
}
if (!flag) {
cout << "-1\n";
} 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.io.*;
import java.util.*;
public class Permutation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
for (int i1 = 0; i1 < t; i1 ++){
int n = sc.nextInt();
int[] a = new int[2 * n];
boolean[] taken = new boolean[2 * n + 1];
for (int i2 = 0; i2 < n; i2 ++){
a[i2 * 2] = sc.nextInt();
taken[a[i2 * 2]] = true;
}
for (int i3 = 0; i3 < n; i3 ++){
for (int i4 = a[i3 * 2] + 1; i4 <= 2 * n; i4 ++){
if (!taken[i4]){
a[i3 * 2 + 1] = i4;
taken[i4] = true;
break;
}
}
}
boolean works = true;
StringBuilder ans = new StringBuilder();
for (int i5 = 0; i5 < 2 * n; i5 ++){
ans.append(a[i5]);
ans.append(" ");
if (a[i5] == 0){
pw.println(-1);
works = false;
break;
}
}
if (works){
pw.println(ans);
}
}
pw.close();
}
}
| JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author ky112233
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
int[] b = new int[n];
int[] arr = new int[2 * n];
for (int i = 0; i < n; i++) {
int num = in.nextInt();
b[i] = num;
arr[num - 1] = 1;
}
int[] comp = new int[n];
for (int i = 0; i < n; i++) {
int temp = b[i];
int j;
for (j = temp; j < 2 * n; j++) {
if (arr[j] == 0) {
arr[j] = 1;
comp[i] = j + 1;
break;
}
}
if (j == 2 * n) {
out.println(-1);
return;
}
}
for (int i = 0; i < n; i++) {
out.print(b[i] + " " + comp[i] + " ");
}
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.util.*;
import static java.lang.Math.*;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
public class A {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] a = new int[2 * n];
int[] b = new int[2 * n];
for (int i = 0; i < b.length; i += 2) {
a[i] = Integer.parseInt(st.nextToken());
b[a[i] - 1] = 1;
}
boolean f = true;
for (int i = 0; i < b.length; i += 2) {
int idx = -1;
for (int j = a[i]; j < 2*n; j++) {
if (b[j] == 0) {
idx = j + 1;
b[j] = 1;
break;
}
}
if (idx == -1) {
f = false;
break;
}
a[i + 1] = idx;
}
if (!f)
pw.println(-1);
else {
for (int i = 0; i < b.length; i++) {
pw.print(a[i] + " ");
}
pw.println();
}
}
pw.flush();
pw.close();
}
}
| JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.io.*;
import java.util.*;
public class Code2802 {
public static void main(String args[]){
Reader s = new Reader();
int t = s.nextInt();
// 4 1 3
Outer:
while(t-- > 0){
int n = s.nextInt();
int a[]= new int[2*n];
boolean b[] = new boolean[2*n];
for(int i=0;i<a.length;i=i+2){
a[i] = s.nextInt();
b[a[i] - 1] = true;
}
for (int i = 1; i <a.length ; i = i +2) {
for (int j = a[i-1] + 1; j <=a.length ; j++) {
if(!b[j-1]){
b[j-1] = true;
a[i] = j;
break;
}
}
}
for (int i = 0; i < a.length; i++) {
if(a[i] == 0){
System.out.println(-1);
continue Outer;
}
}
for (int i = 0; i < a.length ; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
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 String next() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
double nextDouble()
{
return Double.parseDouble(next());
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
| JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | for _ in range(int(input())):
n = int(input())
b = [int(i) for i in input().split()]
missed = []
sorted_b = sorted(b)
for ind, num in enumerate(sorted_b):
if ind != 0:
missed.extend(list(range(sorted_b[ind - 1] + 1, num)))
else:
missed.extend(list(range(1, num)))
if sorted_b[-1] != 2 * n:
missed.extend((range(sorted_b[-1] + 1, 2 * n + 1)))
# print(missed)
ans = []
flag = 0
for num in b:
m = float('inf')
for candidate in missed:
if num < candidate < m:
m = candidate
if m != float('inf'):
del missed[missed.index(m)]
else:
flag = 1
break
if min(m, num) == num:
ans.extend([num, m])
if flag == 0:
print(*ans)
else:
print(-1)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.*;
public class Solution
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
StringBuilder out=new StringBuilder();
int t=Integer.parseInt(br.readLine());
for(int j=0;j<t;j++){
int n=Integer.parseInt(br.readLine());
int[] arr=new int[n];
StringTokenizer s=new StringTokenizer(br.readLine());
boolean not_one=true;
boolean maximum=false;
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(s.nextToken());
if(arr[i]==1)
not_one=false;
if(arr[i]==(2*n))
maximum=true;
}
if(not_one|| maximum)
{
out.append("-1"+"\n");
continue;
}
int[] ans=new int[2*n];
HashSet<Integer>h=new HashSet<>();
for(int i=0;i<n;i++){
ans[2*i]=arr[i];
h.add(arr[i]);
}
boolean o=false;
for(int i=1;i<2*n;i=i+2){
int k=ans[i-1];
while(k<=2*n && h.contains(k))
k++;
if(k==2*n+1){
out.append("-1"+"\n");
o=true;
break;
}
ans[i]=k;
h.add(k);
}
if(o==true)
continue;
for(int i=0;i<2*n;i++){
out.append(ans[i]+" ");
}
out.append("\n");
}
System.out.println(out);
}
}
class Segment{
int s;
int e;
public Segment(int x,int y){
s=x;
e=y;
}
}
class SortSeg implements Comparator<Segment>
{
// Used for sorting in ascending order of
// roll number
public int compare(Segment a, Segment b)
{
int lena= a.e-a.s+1;
int lenb= b.e-b.s+1;
if(lena==lenb)
return a.s - b.s;
else
//decsending order
return lenb-lena;
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | I = input
for _ in range(int(I())):
try:
n = int(I())
b = list(map(int, I().split(' ')))
a = [0 for i in range((2 * n) + 1)]
s = []
for i in range(n):
if a[b[i]] == 0:
a[b[i]] = 1
s.append(str(b[i]))
s.append('0')
else:
assert 1 == 0
for i in range(n):
t = 0
for j in range(b[i] + 1, (2 * n) + 1):
if a[j] == 0:
t = j
a[j] = 1
break
if t == 0:
assert 1 == 0
s[2*(i+ 1)-1] = str(t)
# print(s)
for i in range(1, (2 * n) + 1):
if a[i] == 0:
assert 1 == 0
for i in s:
print(i,end=' ')
print()
except AssertionError:
print('-1') | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | from sys import stdin,stderr
def rl():
return [int(w) for w in stdin.readline().split()]
def solve(n, b):
f = [True for i in range(2*n+1)]
for x in b:
if not f[x]:
return [-1]
f[x] = False
a = []
for x in b:
a.append(x)
for y in range(x+1, 2*n+1):
if f[y]:
a.append(y)
f[y] = False
break
else:
return [-1]
return a
t, = rl()
for _ in range(t):
print(*solve(rl()[0], rl()))
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | I=input
exec(int(I())*'I();b=*map(int,I().split()),;s={*range(2*len(b)+1)}-{*b};a=[]\ntry:\n for x in b:a+=x,min(s-{*range(x)});s-={*a}\nexcept:a=-1,\nprint(*a);') | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void solve(int n, int b[]) {
set<int> s;
int a[2 * n];
for (int i = 0; i < (int)2 * n; i++) {
s.insert(i + 1);
}
for (int i = 0; i < (int)n; i++) {
s.erase(b[i]);
}
if (*min_element(s.begin(), s.end()) == 1 ||
*max_element(b, b + n) == 2 * n) {
cout << "-1\n";
return;
} else {
for (int i = 0; i < (int)2 * n; i++) {
if (i % 2 == 0)
a[i] = b[i / 2];
else {
if ((s.upper_bound(b[i / 2])) != s.end()) {
a[i] = *s.upper_bound(b[i / 2]);
s.erase(*s.upper_bound(b[i / 2]));
} else {
cout << "-1\n";
return;
}
}
}
}
for (auto x : a) cout << x << " ";
cout << "\n";
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc;
cin >> tc;
while (tc--) {
int n;
cin >> n;
int b[n];
for (int i = 0; i < (int)n; i++) {
cin >> b[i];
}
solve(n, b);
}
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;
long long t, i, a[100000], h, b[10000], k[10000], g, p, r, l, n;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> t;
while (t--) {
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
b[a[i]]++;
}
i = 0;
l = 0;
g = 0;
h = n;
while (h-- and l == 0) {
g++;
i++;
k[g] = a[i];
r = a[i] + 1;
p = 0;
while (p == 0) {
if (b[r] == 0) {
p++;
b[r]++;
}
r++;
}
if (r - 1 > n * 2) {
l++;
}
g++;
k[g] = r - 1;
}
if (l == 1) {
cout << -1;
} else {
for (i = 1; i <= n * 2; i++) {
cout << k[i] << " ";
}
}
cout << '\n';
fill(b, b + 3 * n, 0);
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
set<int> s;
for (int i = 1; i <= 2 * n; i++) s.insert(i);
vector<int> b(2 * n);
bool er = false;
for (int i = 0; i < n; i++) {
if (s.find(a[i]) == s.end()) {
er = true;
break;
} else
s.erase(a[i]);
b[2 * i] = a[i];
}
for (int i = 0; i < n; i++) {
auto it = s.upper_bound(a[i]);
if (it == s.end()) {
er = true;
break;
}
b[2 * i + 1] = *it;
s.erase(it);
}
if (er)
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 | import sys
import math
import itertools
import functools
import collections
import operator
import fileinput
import copy
from collections import *
ORDA = 97 # a
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return [int(i) for i in input().split()]
def lcm(a, b): return abs(a * b) // math.gcd(a, b)
def revn(n): return str(n)[::-1]
def dd(): return collections.defaultdict(int)
def ddl(): return collections.defaultdict(list)
def sieve(n):
if n < 2: return list()
prime = [True for _ in range(n + 1)]
p = 3
while p * p <= n:
if prime[p]:
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 2
r = [2]
for p in range(3, n + 1, 2):
if prime[p]:
r.append(p)
return r
def divs(n, start=2):
r = []
for i in range(start, int(math.sqrt(n) + 1)):
if (n % i == 0):
if (n / i == i):
r.append(i)
else:
r.extend([i, n // i])
return r
def divn(n, primes):
divs_number = 1
for i in primes:
if n == 1:
return divs_number
t = 1
while n % i == 0:
t += 1
n //= i
divs_number *= t
def prime(n):
if n == 2: return True
if n % 2 == 0 or n <= 1: return False
sqr = int(math.sqrt(n)) + 1
for d in range(3, sqr, 2):
if n % d == 0: return False
return True
def convn(number, base):
new_number = 0
while number > 0:
new_number += number % base
number //= base
return new_number
def cdiv(n, k): return n // k + (n % k != 0)
def ispal(s): # Palindrome
for i in range(len(s) // 2 + 1):
if s[i] != s[-i - 1]:
return False
return True
# a = [1,2,3,4,5] -----> print(*a) ----> list print krne ka new way
def function(arr, size,dp):
if size == 1 or size == 0:
return 1
key = ''.join(map(str, arr[:size]))
if dp[key]:
return dp[key]
output = function(arr, size-1, dp)
key = ''.join(map(str, arr[:size-1]))
dp[key] = output
if (arr[size-2]*10 + arr[size-1]) <= 26:
output += function(arr, size-2, dp)
key = ''.join(map(str, arr[:size-2]))
dp[key] = output
return output
def main():
for _ in range(ii()):
n = ii()
arr = li()
b =[0]*2*n
d = [True]*(2*n + 1)
i = 0
for ele in arr:
d[ele] = False
b[i] = ele
i += 2
ok = True
for i in range(1,2*n,2):
e = b[i-1] + 1
while e <= 2*n:
if d[e]:
d[e] = False
b[i] = e
break
e += 1
else:
ok = False
break
if ok:
print(*b)
else:
print(-1)
main() | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.io.*;
import java.util.*;
import java.lang.*;
public class c1 {
public static FScanner scan;
public static PrintWriter out;
public static void main(String[] args) {
scan=new FScanner();
out=new PrintWriter(new BufferedOutputStream(System.out));
// int t=1;
int t=scan.nextInt();
while(t-->0) {
int n=scan.nextInt();
int[] a=new int[n],ans=new int[2*n];
ArrayList<Integer> unused=new ArrayList<>();
for(int c=0;c<2*n;c++) unused.add(c+1);
for(int c=0;c<n;c++) {
a[c]=scan.nextInt();
ans[2*c]=a[c];
unused.remove(unused.indexOf(a[c]));
}
boolean possible=true;
for(int c=0;c<n;c++) {
int num=bs(ans[2*c],unused);
if(num==-1) {
possible=false;
break;
} else {
ans[2*c+1]=unused.get(num);
unused.remove(num);
}
}
if(!possible) out.println(-1);
else {
for(int c=0;c<2*n;c++) out.print(ans[c]+" ");
out.println();
}
}
out.close();
}
//-----------------------------------------------------HERE---------------------------------------------------------
static int bs(int min,ArrayList<Integer> list) {
int start=0,end=list.size()-1,ans=-1;
while(start<=end) {
int mid=(start+end)/2;
if(list.get(mid)>=min) {
ans=mid;
end=mid-1;
} else start=mid+1;
} return ans;
}
//------------------------------------------------------------------------------------------------------------------
//scanner
public static class FScanner {
BufferedReader br;
StringTokenizer st;
public FScanner() {
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.io.*;
import java.math.*;
import java.util.*;
import java.lang.*;
public class RestoringPermutation {
public static void main(String[] args) {
FastScanner I = new FastScanner(); //Input
OutPut O = new OutPut(); //Output
int T = I.nextInt();
while (T-->0) {
int N = I.nextInt();
boolean[] vis = new boolean[2*N+1];
int[] ans = new int[2*N];
int[] a = new int[N];
boolean bad = false;
for (int i = 0; i < N; i++) {
a[i] = I.nextInt();
vis[a[i]] = true;
}
for (int cur = 0; cur < N; cur++) {
int conv = cur+1; //Conversion to one-based indexing
int L = conv*2-1;
int R = conv*2;
L--;
R--; //Converting back to 0 based indexing
ans[L] = a[cur]; //The minimum should come first if lexicographically minimal
//solution is needed
boolean added=false;
for (int pair = a[cur]+1; pair <= 2*N; pair++) { //Looking for larger partner
if (!vis[pair]) {
added=true;
ans[R]=pair;
vis[pair]=true;
break;
}
}
if (!added) {
bad=true;
break;
}
}
if (bad) O.pln(-1);
else {
for (int i = 0; i < 2*N; i++) O.p(ans[i]+" ");
O.p("\n");
}
}
}
public static void sort(long[] a) {Arrays.sort(a);}
public static void sort(int[] a) {Arrays.sort(a);}
public static void sort(String[] a) {Arrays.sort(a);}
public static void sort(char[] a) {Arrays.sort(a);}
public static long ceil(long num, long den) {long ans = num/den; if (num%den!=0)
ans++; return ans;}
public static long GCD(long a, long b) {
if (a==0||b==0) return Math.max(a,b);
return GCD(Math.min(a, b),Math.max(a, b)%Math.min(a, b));
}
public static long FastExp(long base, long exp, long mod) {
long ans=1;
while (exp>0) {
if (exp%2==1) ans*=base;
exp/=2;
base*=base;
base%=mod;
ans%=mod;
}
return ans;
}
public static long ModInv(long num,long mod) {return FastExp(num,mod-2,mod);}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!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());};
}
static class OutPut{
PrintWriter w = new PrintWriter(System.out);
void pln(int x) {w.println(x);w.flush();}
void pln(long x) {w.println(x);w.flush();}
void pln(String x) {w.println(x);w.flush();}
void pln(char x) {w.println(x);w.flush();}
void pln(StringBuilder x) {w.println(x);w.flush();}
void p(int x) {w.print(x);w.flush();}
void p(long x) {w.print(x);w.flush();}
void p(String x) {w.print(x);w.flush();}
void p(char x) {w.print(x);w.flush();}
void p(StringBuilder x) {w.print(x);w.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 | for tc in range(int(input())):
n = int(input())
b = list(map(int, input().split(' ')))
b.insert(0, 0)
a = (2*n+1)*[0]
s = set()
flag = True
for i in range(1,n+1):
a[2*i-1] = b[i]
if b[i] in s:
flag = False
break
s.add(b[i])
for i in range(1,len(b)):
val = b[i]+1
while val in s:
val+=1
a[2*i] = val
s.add(val)
for i in range(1,2*n+1):
if not i in s:
flag = False
break
if flag:
print(*a[1:])
else:
print(-1) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.util.*;
import java.io.*;
public class Solution{
long rem = 1000000007L;
public Solution(){
Scanner sc = new Scanner(System.in);
int tests = sc.nextInt();
for(int t=1; t<=tests; t++){
int n = sc.nextInt();
int[] b = new int[n];
boolean[] found = new boolean[(2*n)+1];
boolean possible = false;
for(int i=0; i<n; i++){
b[i] = sc.nextInt();
found[b[i]] = true;
}
int[] res = new int[2*n];
int index = 0;
for(int i=0; i<n; i++){
res[index++] = b[i];
possible = false;
for(int j=b[i]+1; j<found.length; j++){
if(!found[j]){
found[j] = true;
res[index++] = j;
possible = true;
break;
}
}
if(!possible){
break;
}
}
if(!possible){
System.out.println("-1");
}
else{
for(int i=0; i<(res.length-1); i++){
System.out.print(res[i] + " ");
}
System.out.println(res[res.length-1]);
}
}
}
public static void main(String[] args){
new Solution();
}
} | 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 math
import atexit
import io
import sys
# input = sys.stdin.readline
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER
@atexit.register
def write():
sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
def array_to_string(a):
s = ""
for i in a:
s += str(i)
return s
t = int(input())
while t>0:
t = t-1
n = int(input())
b = list(map(int,input().split()))
temp = [1 for i in range(4*n)]
temp = [0] + temp
a = [0 for i in range(2*n)]
for i in range(n):
temp[b[i]] = 0
a[2*i] = b[i]
# print("a",a)
for i in range(n):
num = b[i]
for j in range(num+1,len(temp)):
if temp[j] == 1:
a[2*i+1] = j
temp[j] = 0
break
if max(a)>2*n:
print(-1)
continue
for i in a:
print(i,end = " ")
print() | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long t, n, a[1005], dd[2005], kt, b[2005], m;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> t;
while (t--) {
memset(dd, 0, sizeof(dd));
memset(b, 0, sizeof(b));
m = 0;
cin >> n;
for (long long i = 1; i <= n; i++) {
cin >> a[i];
dd[a[i]] = 1;
}
for (long long i = 1; i <= n; i++) {
kt = 0;
for (long long j = a[i] + 1; j <= 2 * n; j++)
if (dd[j] == 0) {
kt = j;
break;
}
if (kt == 0) {
m = -1;
break;
} else {
m++;
b[m] = a[i];
m++;
b[m] = kt;
dd[kt] = 1;
}
}
if (m == -1)
cout << m << '\n';
else {
for (long long i = 1; i <= m; i++) cout << b[i] << " ";
cout << '\n';
}
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int t = 1;
cin >> t;
while (t--) {
int n;
cin >> n;
int a[n], b[2 * n];
unordered_map<int, int> m;
int flag = 0;
for (long long int i = 0; i < n; i++) {
cin >> a[i];
m[a[i]]++;
}
if (!m[1] or m[2 * n])
cout << -1 << endl;
else {
flag = 0;
int j = 0;
for (long long int i = 0; i < n; i++) {
b[j] = a[i];
j += 2;
}
j = 1;
int in = b[j - 1];
for (long long int i = 0; i < n; i++) {
in = b[j - 1] + 1;
while (m[in]) {
in++;
if (in == 2 * n + 1) {
flag = 1;
break;
}
}
m[in]++;
b[j] = in;
j += 2;
}
if (flag)
cout << -1 << endl;
else
for (long long int i = 0; i < 2 * n; i++) cout << b[i] << " ";
cout << endl;
}
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | t = int(input())
while(t):
t=t-1
n = int(input())
b = [int(s) for s in input().split()]
dic = [0]*((2*n)+1)
for i in b:
dic[i] = 1
x = (2*n)+1
a = []
ans = 1
# print(dic)
for i in b:
flag = 0
# print(i)
for j in range(i+1,x):
if dic[j] == 0:
# print(" {}".format(j))
n = j
dic[j] = 1
flag = 1
break
# print(flag)
# print(n)
# print(dic)
if flag == 0:
ans = -1
break
else:
a.append(i)
a.append(n)
if ans == 1:
for i in a:
print(i,end=" ")
print()
else:
print(ans)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | from sys import stdin
from collections import defaultdict
import bisect
input=stdin.readline
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
dict=defaultdict(int)
arr=[]
for i in range(n):
arr.append([a[i],i])
dict[a[i]]=1
arr.sort()
#print(arr)
tmp=[]
for i in range(1,2*n+1):
if dict[i]==0:
tmp.append(i)
#print(tmp)
fl=0
farr={}
for i in range(len(tmp)):
if arr[i][0]>tmp[i]:
fl=1
break
if fl==1:
print(-1)
continue
# a, temp
for x in a:
val=tmp[bisect.bisect_right(tmp,x)]
tmp.remove(val)
print(x,val,end=" ")
print()
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | def Input():
tem = input().split()
ans = []
for it in tem:
ans.append(int(it))
return ans
from collections import Counter
T = Input()[0]
def solve(n, b):
m = 2*n
a = []
for i in range(n):
a.append([b[i],i])
a.sort(reverse=True)
vis=[0 for i in range(m+1)]
ans=[0 for i in range(m)]
for i in range(n):
vis[a[i][0]]=1
for i in range(n):
flag = False
big_a = None
for j in range(m,a[i][0],-1):
if vis[j]==0:
flag = True
big_a = j
vis[j]=1
break
if flag:
ans[2*a[i][1]]=a[i][0]
ans[2*a[i][1]+1]=big_a
else:
return [-1]
for i in range(1,m,2):
for j in range(i+2,m,2):
if ans[j]<ans[i] and ans[j]>ans[i-1] and ans[i]>ans[j-1]:
ans[i],ans[j]=ans[j],ans[i]
return ans
for tt in range(T):
n = Input()[0]
b = Input()
ans=solve(n,b)
if ans[0]==-1:
print(-1)
else:
print(ans[0],end='')
for i in range(1,2*n):
print('',ans[i],end='')
print()
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | t=int(input())
while t:
n=int(input())
b=input().split()
visited=[0 for i in range(2*n+1)]
a=[]
for i in range(n):
b[i]=int(b[i])
visited[b[i]]=1
for i in range(n):
a.append(b[i])
flag=False
for j in range(b[i]+1,2*n+1):
if(visited[j]==0):
flag=True
visited[j]=1
a.append(j)
break
if(flag==False):break
if(flag==False):print(-1)
else:print(*a)
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 | for _ in range(int(input())):
n = int(input())
se = set()
flag = True
li = list(map(int, input().split()))
for ele in li:
se.add(ele)
newli = []
for ele in li:
newli.append(ele)
srch = ele + 1
if srch>2*n:
flag = False
break
while True:
if srch not in se:
newli.append(srch)
se.add(srch)
break
else:
srch+=1
if srch>2*n:
flag = False
break
if flag == False:
print("-1", end = "")
else:
for ele in newli:
print(ele, end=" ")
print()
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | def helper(helpArr,num):
for i in range(num,len(helpArr)):
if helpArr[i]==0:
return i+1
return -1
def soln(n,arr):
helpArr=[0]*(2*n)
for i in arr:
helpArr[i-1]=1
ans=[]
for i in arr:
temp=helper(helpArr,i)
if temp==-1:
return -1
helpArr[temp-1]=1
ans.append(i)
ans.append(temp)
return ans
for _ in range(int(input())):
n=int(input())
arr=list(map(int,input().split()))
ans=soln(n,arr)
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 | for t in range(int(input())):
n=int(input())
arr=list(map(int,input().split()))
ans=[0]*(n*2)
for i in range(n):
ele=arr[i]
ans[2*i]=ele
included=set(arr)
i=1
broken=0
while i<(2*n):
starting=ans[i-1]
ending=(2*n)
for j in range(starting+1,ending+1):
if j not in included:
ans[i]=j
included.add(j)
break
if ans[i]==0:
broken+=1
break
i+=2
if broken==0:
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 | #include <bits/stdc++.h>
using namespace std;
signed main() {
long long tt;
cin >> tt;
while (tt--) {
long long n;
cin >> n;
vector<long long> a(2 * n), b(n);
set<long long> help;
for (long long i = 0; i < n; ++i) {
cin >> b[i];
help.insert(2 * i);
help.insert(2 * i + 1);
}
vector<pair<long long, long long> > all(n);
for (long long i = 0; i < n; ++i) {
a[2 * i] = b[i] - 1;
help.erase(b[i] - 1);
all[i] = {b[i] - 1, i};
}
sort(all.begin(), all.end());
long long i = 0;
bool ans = true;
for (auto u : help) {
if (a[all[i].second * 2] > u) {
cout << -1;
ans = false;
break;
}
a[all[i].second * 2 + 1] = u;
++i;
}
if (ans) {
for (auto u : help) {
all.push_back({u, -1});
}
sort(all.begin(), all.end());
set<long long> now;
for (i = 0; i < all.size(); ++i) {
if (all[i].second != -1)
now.insert(all[i].second);
else {
auto k = *now.begin();
a[2 * k + 1] = all[i].first;
now.erase(k);
}
}
for (i = 0; i < 2 * n; ++i) cout << a[i] + 1 << " ";
}
cout << "\n";
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | t=int(input())
while t:
t-=1
n=int(input())
b=list(map(int, input().split()))
cpy=b[::]
s=list(set(range(1, 2*n+1)) - set(b))
s.sort()
cpy.sort()
d=dict()
for val, k in enumerate(cpy):
d[k]=val
ans=[]
flag=True
for i in b:
val=0
for j in s:
if i<j:
val=j
s.remove(j)
break
if val:
ans.append(i)
ans.append(val)
else:
flag=False
break
if flag:
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 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const long double PI = acos(-1);
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
const int mod1 = 998244353;
const long long INF = 2e18;
template <class T>
inline void amin(T& x, const T& y) {
if (y < x) x = y;
}
template <class T>
inline void amax(T& x, const T& y) {
if (x < y) x = y;
}
int read() {
long long s = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
s = s * 10 + c - '0';
c = getchar();
}
return s * f;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
bool visited[210];
memset(visited, 0, sizeof(visited));
int a[105];
vector<int> v;
vector<int> t;
int k = mod;
int p = -mod;
bool flag = false;
for (int i = 1; i <= n; i++) {
cin >> a[i];
t.push_back(a[i]);
visited[a[i]] = true;
k = min(k, a[i]);
p = max(p, a[i]);
}
sort(t.begin(), t.end());
for (int i = 1; i <= 2 * n; i++) {
if (!visited[i]) {
v.push_back(i);
visited[i] = true;
}
}
sort(v.begin(), v.end());
for (int i = 0; i < n; i++) {
if (t[i] > v[i]) {
flag = true;
}
}
if (flag) {
cout << "-1\n";
continue;
} else {
for (int i = 1; i <= n; i++) {
cout << a[i] << " ";
for (int j = 0; j < n; j++) {
if (v[j] > a[i] && visited[v[j]]) {
cout << v[j] << " ";
visited[v[j]] = false;
break;
}
}
}
}
cout << "\n";
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | t = int(input())
for i in range(t):
n = int(input())
ans = [0] * ((2 * n))
list2 = [True] * (2 * n + 1)
b = list(map(int, input().split()))
fin = True
for k,j in enumerate(b):
ans[2*k] = j
list2[j] = False
for l,j in enumerate(b):
for k in range(j+1, 2 * n +1):
if list2[k] == True:
list2[k] = False
ans[2*l+1] = k
break
else:
fin = False
break
if fin == True:
print(" ".join(map(str,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())
while t:
t -= 1
n = int(input())
b = [int(i) for i in input().split()]
a = []
if (not 1 in b) or 2*n in b:
print("-1")
else:
disp = []
for i in range(1,2*n+1):
if not i in b:
disp.append([i, True])
for i in b:
min = -1
a.append(i)
for j in disp:
if j[1] and j[0] > i:
min = j[0]
j[1] = False
break
else:
print(min)
break
a.append(min)
else:
print(*a) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long int n;
int t;
cin >> t;
while (t--) {
cin >> n;
vector<long long int> v(n), vv;
for (long long int i = 0; i <= n - 1; i++) cin >> v[i];
bool arr[210];
memset(arr, 0, sizeof(arr));
bool ok = 0, ans = 1;
for (long long int i = 0; i <= n - 1; i++) arr[v[i]] = 1;
for (long long int i = 0; i < n; i++) {
ok = 0;
vv.push_back(v[i]);
for (long long int j = v[i] + 1; j <= 2 * n; j++) {
if (arr[j] == 0) {
vv.push_back(j);
arr[j] = 1;
ok = 1;
break;
}
}
if (!ok) {
ans = 0;
break;
}
}
if (ans) {
for (long long int i = 0; i <= n + n - 1; i++) cout << vv[i] << ' ';
cout << endl;
} else
cout << -1 << 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;
template <typename T>
inline T gcd(T a, T b) {
while (b != 0) swap(b, a %= b);
return a;
}
template <typename T>
inline void seethis(vector<T> vect) {
for (T x : vect) cout << x << " ";
cout << "\n";
}
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
int lcm(int a, int b) { return a * (b / gcd(a, b)); }
bool cmp(const pair<int, int> &a) { return a.first < a.second; }
long long int modpower(long long int a, long long int b, long long int c) {
long long int res = 1;
while (b) {
if (b & 1LL) res = (res * a) % c;
a = (a * a) % c;
b >>= 1;
}
return res;
}
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(false);
;
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> b(n);
vector<int> ans(2 * n);
vector<bool> aval(2 * n + 1, true);
for (int i = 0; i < n; i++) {
cin >> b[i];
aval[b[i]] = false;
ans[2 * i] = b[i];
}
bool flag = true;
for (int i = 1; i < 2 * n; i += 2) {
int x = -1;
for (int j = ans[i - 1] + 1; j <= 2 * n; j++)
if (aval[j]) {
x = j;
aval[j] = false;
break;
}
if (x == -1) {
flag = false;
break;
}
ans[i] = x;
}
if (flag) {
seethis(ans);
} else {
cout << "-1\n";
}
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 |
import java.util.*;import java.io.*;import java.math.*;
public class Main
{
public static void process()throws IOException
{
int n=ni(),arr[]=new int[n+1];
HashSet<Integer> t_set = new HashSet<Integer>();
for(int i=1;i<=n;i++){
t_set.add(arr[i]=ni());
}
TreeSet<Integer> set = new TreeSet<Integer>();
// StringBuilder res = new StringBuilder();
for(int i=1;i<=2*n;i++){
if(!t_set.contains(i))
set.add(i);
}
ArrayList<Integer> li = new ArrayList<>();
li.add(0);
for(int i=1;i<=n;i++){
Integer x=set.higher(arr[i]);
if(x==null){
pn(-1);
return;
}
li.add(arr[i]);
li.add(x);
set.remove(x);
}
for(int i=1;i<=2*n;i++) p(li.get(i)+" ");
pn("");
}
static AnotherReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
out = new PrintWriter(System.out);
sc=new AnotherReader();
boolean oj = true;
oj = System.getProperty("ONLINE_JUDGE") != null;
if(!oj) sc=new AnotherReader(100);
long s = System.currentTimeMillis();
int t=1;
t=ni();
while(t-->0)
process();
out.flush();
if(!oj)
System.out.println(System.currentTimeMillis()-s+"ms");
System.out.close();
}
static void pn(Object o){out.println(o);}
static void p(Object o){out.print(o);}
static void pni(Object o){out.println(o);System.out.flush();}
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
static double nd()throws IOException{return sc.nextDouble();}
static String nln()throws IOException{return sc.nextLine();}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
static boolean multipleTC=false;
static long mod=(long)1e9+7l;
static void r_sort(int arr[],int n){
Random r = new Random();
for (int i = n-1; i > 0; i--){
int j = r.nextInt(i+1);
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
Arrays.sort(arr);
}
static long mpow(long x, long n) {
if(n == 0)
return 1;
if(n % 2 == 0) {
long root = mpow(x, n / 2);
return root * root % mod;
}else {
return x * mpow(x, n - 1) % mod;
}
}
static long mcomb(long a, long b) {
if(b > a - b)
return mcomb(a, a - b);
long m = 1;
long d = 1;
long i;
for(i = 0; i < b; i++) {
m *= (a - i);
m %= mod;
d *= (i + 1);
d %= mod;
}
long ans = m * mpow(d, mod - 2) % mod;
return ans;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class AnotherReader{BufferedReader br; StringTokenizer st;
AnotherReader()throws FileNotFoundException{
br=new BufferedReader(new InputStreamReader(System.in));}
AnotherReader(int a)throws FileNotFoundException{
br = new BufferedReader(new FileReader("input.txt"));}
String next()throws IOException{
while (st == null || !st.hasMoreElements()) {try{
st = new StringTokenizer(br.readLine());}
catch (IOException e){ e.printStackTrace(); }}
return st.nextToken(); } int nextInt() throws IOException{
return Integer.parseInt(next());}
long nextLong() throws IOException
{return Long.parseLong(next());}
double nextDouble()throws IOException { return Double.parseDouble(next()); }
String nextLine() throws IOException{ String str = ""; try{
str = br.readLine();} catch (IOException e){
e.printStackTrace();} return str;}}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int ans = 0, sign = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') sign = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
ans = ans * 10 + (ch - '0');
ch = getchar();
}
return ans * sign;
}
const double PI = acos(-1.0);
const int mod = 1e9 + 7;
const int N = 100 + 5;
int a[N], b[N + N];
int vis[N + N];
void solve() {
int n = read();
bool flag = true;
for (int i = 1; i <= n; i++) {
a[i] = read();
}
memset(b, 0, sizeof(b));
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++) {
b[i * 2 - 1] = a[i];
vis[a[i]] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = a[i] + 1; j <= 2 * n; j++)
if (!vis[j] && j > a[i]) {
b[i * 2] = j;
vis[j] = 1;
break;
}
if (!b[i * 2]) {
flag = false;
break;
}
}
if (!flag)
cout << "-1" << endl;
else {
for (int i = 1; i <= 2 * n - 1; i++) {
cout << b[i] << " ";
}
cout << b[2 * n] << endl;
}
}
int main() {
int t = read();
while (t--) {
solve();
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import sys
input = lambda: sys.stdin.readline().rstrip()
def last_proverka(v):
for i in range(len(v)):
i += 1
if i not in v:
return -1
return True
# v - ans
# h - b
def l_proverka(v, c):
for i in range(len(c)):
if v[i] == c[i]:
return False
return True
def x(m, sr):
for i in range(1000):
if i > sr and i not in m and i not in a:
return i
return False
for _ in range(int(input())):
n = int(input())
b = list(map(int, input().split()))
a = [0] * 2 * n
for i in range(n):
a[2 * i] = b[i]
k = True
for i in range(2 * n):
if a[i] == 0:
c = x(b, a[i - 1])
if c == False:
k = False
break
else:
a[i] = c
if last_proverka(a) == True:
print(*a)
else:
print(-1)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | for _ in range(int(input())):
n=int(input())
B=list(map(int,input().split()))
A=[]
for i in range(n):
A.append(B[i])
for j in range(B[i]+1,1000):
if(j not in A and j not in B):
A.append(j)
break
C=[]
for i in range(0,2*n):
C.append(A[i])
C.sort()
ans=-1
for j in range(0,2*n):
if(C[i]!=i+1):
ans=0
break
if(ans==0):
print("-1")
else:
print(*A)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | for i in range(int(input())):
n=int(input())
nums=[int(i) for i in input().split()]
check=set(nums)
s=max(nums)
l=[int(i)+1 if int(i)+1 not in check else 0 for i in range(2*n)]
l=sorted(list(set(l)))
l.remove(0)
o=""
b=0
for i in nums:
s=0
while s<len(l) and l[s]<i:
s+=1
if s==len(l):
b=1
break
else:
o+=str(i)+" "+str(l[s])+" "
l.remove(l[s])
if b:
print(-1)
else:
print(o[:-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.swing.*;
import java.awt.desktop.SystemSleepEvent;
import java.util.*;
import java.io.*;
public class Main {
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
}
public class pair {
int a;
int b;
pair(int p, int q) {
a = p;
b = q;
}
public int hashCode() {
return new Integer(a).hashCode() * 31 + new Integer(b).hashCode();
}
}
public static int gcd(int a, int b) {
if (a == 1 || b == 1)
return 1;
if (a == 0)
return b;
if (b == 0)
return a;
return gcd(b % a, a);
}
public static void main(String[] args) {
FastReader s = new FastReader();
int t=s.nextInt();
while(t>0)
{
int n=s.nextInt();
int[] arr=new int[n];
int[] ans=new int[2*n];
HashSet<Integer> set=new HashSet<>();
for(int i=0;i<n;i++) {
arr[i] = s.nextInt();
set.add(arr[i]);
}
boolean c=true;
for(int i=0;i<n;i++)
{
ans[2*i] = arr[i];
for(int j=arr[i];j<=2*n;j++)
{
if(!set.contains(j))
{
ans[2*i + 1] = j;
set.add(j);
break;
}
if(j==2*n)
c=false;
}
}
if(c==false)
System.out.println(-1);
else
{
for(int i=0;i<2*n;i++)
System.out.print(ans[i]+" ");
System.out.println();
}
t--;
}
}
}
| 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 os
import sys
from io import BytesIO, IOBase
# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
newlines = 0
def __init__(self, file):
self._fd = file.fileno()
self.buffer = BytesIO()
self.writable = "x" in file.mode or "r" not in file.mode
self.write = self.buffer.write if self.writable else None
def read(self):
while True:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
if not b:
break
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines = 0
return self.buffer.read()
def readline(self):
while self.newlines == 0:
b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
self.newlines = b.count(b"\n") + (not b)
ptr = self.buffer.tell()
self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
self.newlines -= 1
return self.buffer.readline()
def flush(self):
if self.writable:
os.write(self._fd, self.buffer.getvalue())
self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
def __init__(self, file):
self.buffer = FastIO(file)
self.flush = self.buffer.flush
self.writable = self.buffer.writable
self.write = lambda s: self.buffer.write(s.encode("ascii"))
self.read = lambda: self.buffer.read().decode("ascii")
self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush
def RL(): return map(int, sys.stdin.readline().rstrip().split())
def N(): return int(input())
def comb(n, m): return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0
def perm(n, m): return factorial(n) // (factorial(n - m)) if n >= m else 0
def mdis(x1, y1, x2, y2): return abs(x1 - x2) + abs(y1 - y2)
mod = 1000000007
INF = float('inf')
# ------------------------------
def main():
for _ in range(N()):
n = N()
arr = list(RL())
rec = [0]*(2*n+1)
res = [0]*(2*n)
for i in range(n):
rec[arr[i]] = 1
tag = False
# print(rec)
for i in range(0, 2*n, 2):
res[i] = arr[i//2]
for j in range(arr[i//2]+1, 2*n+1):
if rec[j]!=1:
rec[j] = 1
res[i+1] = j
break
else:
tag = True
break
if tag: break
# print(rec)
if tag: print(-1)
else:
print(*res)
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 | from sys import stdin
from collections import defaultdict
from bisect import bisect_right
input=stdin.readline
t=int(input())
for _ in range(t):
n=int(input())
a=list(map(int,input().split()))
dict=defaultdict(int)
arr=[]
for i in range(n):
arr.append([a[i],i])
dict[a[i]]=1
arr.sort()
#print(arr)
tmp=[]
for i in range(1,2*n+1):
if dict[i]==0:
tmp.append(i)
fl=0
farr={}
for i in range(len(tmp)):
if arr[i][0]>tmp[i]:
fl=1
break
if fl==1:
print(-1)
continue
# a,,
for x in a:
val=tmp[bisect_right(tmp,x)]
tmp.remove(val)
print(x,val,end=" ")
print() | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.io.*;import java.util.*;import java.math.*;
public class Main
{
static long mod=1000000007l;
static int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;
static long maxl=Long.MAX_VALUE,minl=Long.MIN_VALUE;
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int max(int a,int b){return Math.max(a,b);}
static int min(int a,int b){return Math.min(a,b);}
static int abs(int a){return Math.abs(a);}
static long max(long a,long b){return Math.max(a,b);}
static long min(long a,long b){return Math.min(a,b);}
static long abs(long a){return Math.abs(a);}
static int sq(int a){return (int)Math.sqrt(a);}
static long sq(long a){return (long)Math.sqrt(a);}
static int ncr(int n,int c,long m)
{
long a=1l;
for(int x=n-c+1;x<=n;x++)a=((a*x)%m);
long b=1l;
for(int x=2;x<=c;x++)b=((b*x)%m);
long v=(div((int)b,m-2,m)%m);
return (int)((a*v)%m);
}
static int fib(int n)
{
double p=(1+Math.sqrt(5))/2;
return (int)Math.round(Math.pow(p,n)/Math.sqrt(5));
}
static boolean[] sieve(int n)
{
boolean bo[]=new boolean[n+1];
bo[0]=true;bo[1]=true;
for(int x=4;x<=n;x+=2)bo[x]=true;
for(int x=3;x*x<=n;x+=2)
{
if(!bo[x])
{
for(int y=x*x;y<=n;y+=x)bo[y]=true;
}
}
return bo;
}
static int[] fac(int n)
{
int bo[]=new int[n+1];
bo[0]=0;bo[1]=1;
for(int x=1;x<=n;x++)
{
for(int y=x;y<=n;y+=x)bo[y]++;
}
return bo;
}
static int gcd(int a,int b)
{
if(b==0)return a;
return gcd(b,a%b);
}
static long div(long a,long b,long m)
{
long r=1l;
a%=m;
while(b>0)
{
if((b&1)==1)r=(r*a)%m;
b>>=1;
a=(a*a)%m;
}
return r;
}
static int i()throws IOException
{
if(!st.hasMoreTokens()) st=new StringTokenizer(br.readLine());
return Integer.parseInt(st.nextToken());
}
static long l()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return Long.parseLong(st.nextToken());
}
static String s()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return st.nextToken();
}
static double d()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return Double.parseDouble(st.nextToken());
}
static char c()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return st.nextToken().charAt(0);
}
static boolean b()throws IOException
{
if(!st.hasMoreTokens())st=new StringTokenizer(br.readLine());
return Boolean.parseBoolean(st.nextToken());
}
static void p(Object p){System.out.print(p);}
static void p(String p){System.out.print(p);}
static void p(int p){System.out.print(p);}
static void p(double p){System.out.print(p);}
static void p(long p){System.out.print(p);}
static void p(char p){System.out.print(p);}
static void p(boolean p){System.out.print(p);}
static void pl(Object p){System.out.println(p);}
static void pl(String p){System.out.println(p);}
static void pl(int p){System.out.println(p);}
static void pl(char p){System.out.println(p);}
static void pl(double p){System.out.println(p);}
static void pl(long p){System.out.println(p);}
static void pl(boolean p){System.out.println(p);}
static void pl(){System.out.println();}
static int[] ari(int n)throws IOException
{
int ar[]=new int[n];
st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++) ar[x]=Integer.parseInt(st.nextToken());
return ar;
}
static int[][] ari(int n,int m)throws IOException
{
int ar[][]=new int[n][m];
for(int x=0;x<n;x++)
{
st=new StringTokenizer(br.readLine());
for(int y=0;y<m;y++)ar[x][y]=Integer.parseInt(st.nextToken());
}
return ar;
}
static long[] arl(int n)throws IOException
{
long ar[]=new long[n];
st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++) ar[x]=Long.parseLong(st.nextToken());
return ar;
}
static long[][] arl(int n,int m)throws IOException
{
long ar[][]=new long[n][m];
for(int x=0;x<n;x++)
{
st=new StringTokenizer(br.readLine());
for(int y=0;y<m;y++)ar[x][y]=Long.parseLong(st.nextToken());
}
return ar;
}
static String[] ars(int n)throws IOException
{
String ar[]=new String[n];
st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++) ar[x]=st.nextToken();
return ar;
}
static double[] ard(int n)throws IOException
{
double ar[]=new double[n];
st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++)ar[x]=Double.parseDouble(st.nextToken());
return ar;
}
static double[][] ard(int n,int m)throws IOException
{
double ar[][]=new double[n][m];
for(int x=0;x<n;x++)
{
st=new StringTokenizer(br.readLine());
for(int y=0;y<m;y++)ar[x][y]=Double.parseDouble(st.nextToken());
}
return ar;
}
static char[] arc(int n)throws IOException
{
char ar[]=new char[n];
st=new StringTokenizer(br.readLine());
for(int x=0;x<n;x++)ar[x]=st.nextToken().charAt(0);
return ar;
}
static char[][] arc(int n,int m)throws IOException
{
char ar[][]=new char[n][m];
for(int x=0;x<n;x++)
{
String s=br.readLine();
for(int y=0;y<m;y++)ar[x][y]=s.charAt(y);
}
return ar;
}
static void p(int ar[])
{
StringBuilder sb=new StringBuilder("");;
for(int a:ar) sb.append(a+" ");
System.out.println(sb);
}
static void p(int ar[][])
{
StringBuilder sb;
for(int a[]:ar)
{
sb=new StringBuilder("");
for(int aa:a) sb.append(aa+" ");
System.out.println(sb);
}
}
static void p(long ar[])
{
StringBuilder sb=new StringBuilder("");
for(long a:ar)sb.append(a+" ");
System.out.println(sb);
}
static void p(long ar[][])
{
StringBuilder sb;
for(long a[]:ar)
{
sb=new StringBuilder("");
for(long aa:a)sb.append(aa+" ");
System.out.println(sb);
}
}
static void p(String ar[])
{
StringBuilder sb=new StringBuilder("");
for(String a:ar)sb.append(a+" ");
System.out.println(sb);
}
static void p(double ar[])
{
StringBuilder sb=new StringBuilder("");
for(double a:ar)sb.append(a+" ");
System.out.println(sb);
}
static void p(double ar[][])
{
StringBuilder sb;
for(double a[]:ar)
{
sb=new StringBuilder("");
for(double aa:a)sb.append(aa+" ");
System.out.println(sb);
}
}
static void p(char ar[])
{
StringBuilder sb=new StringBuilder("");
for(char aa:ar)sb.append(aa+" ");
System.out.println(sb);
}
static void p(char ar[][])
{
StringBuilder sb;
for(char a[]:ar)
{
sb=new StringBuilder("");
for(char aa:a)sb.append(aa+" ");
System.out.println(sb);
}
}
static public void main(String[] args)throws Exception
{
st=new StringTokenizer(br.readLine());
int t=i();
o:
while(t-->0)
{
int n=i();
int ar[]=ari(n);
int kk[]=new int[2*n+1];
// for(int x=0;x<n;x++)kk[x]=ar[x];
// Arrays.sort(kk);
for(int x=0;x<n;x++)
{
kk[ar[x]]=-1;
}
TreeSet<Integer> tt=new TreeSet<>();
for(int x=1;x<=2*n;x++)tt.add(x);
for(int a:ar)tt.remove(a);
StringBuilder sb=new StringBuilder("");
int i=0;
List<Integer> ll=new LinkedList<>();
boolean bo[]=new boolean[2*n+1];
for(int a:tt)
{
bo[a]=true;
//ll.add(a);
i++;
while(kk[i]!=-1)i++;
kk[i]=a;
if(a<i)
{
pl(-1);
continue o;
}
}
i=1;
for(int a:ar)
{
while(true)
{
if(!bo[i])
{
i++;
if(i==(2*n)+1)i=1;
}
else
{
if(a<i)
{
sb.append(a+" "+i+" ");
bo[i]=false;
i=1;
break;
}
i++;
if(i==(2*n)+1)i=1;
}
//pl(sb);
}
}
pl(sb);
}
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int b[n];
unordered_set<int> s;
for (int i = 0; i < n; i++) {
cin >> b[i];
s.insert(b[i]);
}
int a[2 * n], k = 0;
for (int i = 0; i < 2 * n; i++) {
if (i % 2 == 0) {
a[i] = b[k++];
}
}
int p = 0;
for (int i = 1; i <= 2 * n; i++) {
if (s.find(i) == s.end()) {
if (a[p] < i) {
a[p + 1] = i;
s.insert(i);
p += 2;
i = 1;
}
}
}
int flag = 0;
for (int i = 0; i < 2 * n; i++) {
if (a[i] >= 1 && a[i] <= 2 * n) {
flag = 0;
} else {
flag = 1;
break;
}
}
if (flag == 0) {
for (int i = 0; i < 2 * n; i++) {
cout << a[i] << " ";
}
} else {
cout << "-1";
}
cout << endl;
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | //189301019.akshay
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Collections;
public class A
{
public static void main(String[] args)
{
FastReader sc=new FastReader();
StringBuffer ans=new StringBuffer();
int test=sc.nextInt();
outer:while(test-->0)
{
int n=sc.nextInt();
int arr[]=new int[n];
boolean vis[]=new boolean[2*n+1];
int res[]=new int[2*n];
int c=0;
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
res[c]=arr[i];c+=2;
vis[arr[i]]=true;
}
for(int i=1;i<2*n;i+=2) {
// for(int j:res)
// System.out.print(j+" ");
// System.out.println();
for(int j=res[i-1]+1;j<=2*n;j++)
if(!vis[j]) {
vis[j]=true;
res[i]=j;
break;
}
if(res[i] == 0) {
ans.append("-1\n");
continue outer;
}
}
for(int i:res)
ans.append(i+" ");
ans.append("\n");
}
System.out.print(ans);
}
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n);
long temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
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 sys
# sys.stdin=open("input1.in","r")
# sys.stdout=open("outpul.out","w")
for _ in range(int(input())):
N=int(input())
L=list(map(int,input().split()))
Hash=[0]*(2*N+1)
for i in range(N):
Hash[L[i]]+=1
Nahi=[]
for i in range(N):
FLAG=0
for j in range(L[i]+1,2*N+1):
if Hash[j]==0:
FLAG=1
Nahi.append(L[i])
Nahi.append(j)
Hash[j]=1
break
if FLAG==0:
break
if FLAG==0:
print("-1")
else:
for i in range(len(Nahi)):
print(Nahi[i],end=" ")
print()
# Hai.sort()
# Nahi.sort()
# N=len(Nahi)
# count=0
# Pair=[0]*(2*N+1)
# FLAG=0
# print(Hai)
# print(Nahi)
# for i in Hai:
# if count<N:
# if Nahi[count]<i:
# FLAG=1
# break
# Pair[i]=Nahi[count]
# count+=1
# else:
# FLAG=1
# break
# if FLAG==1:
# print("-1")
# else:
# for i in range(N):
# print(L[i],end=" ")
# print(Pair[L[i]],end=" ")
# print() | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long a[250];
long long b[250];
long long c[250];
signed main() {
long long t;
cin >> t;
while (t--) {
long long f;
cin >> f;
memset(b, 0, sizeof(b));
for (long long i = 1; i <= f; i++) {
cin >> a[i];
b[a[i]] = 1;
}
long long cnt = 0;
for (long long i = 1; i <= f; i++) {
for (long long j = a[i] + 1; j <= 2 * f; j++) {
if (b[j] == 0) {
b[j] = 1, c[i] = j, cnt++;
j = 2 * f + 1;
}
}
}
if (cnt != f) {
cout << -1 << endl;
} else {
for (long long i = 1; i <= f; i++) {
cout << a[i] << " " << c[i] << " ";
}
cout << endl;
}
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long inf = 1000000000;
const long long N = 2 * (1e2) + 5;
long long ar[N];
long long ans[N];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
long long t = 1;
cin >> t;
while (t--) {
long long n;
cin >> n;
vector<long long> done(2 * n + 1, 0);
for (long long i = 1; i <= n; ++i) {
cin >> ar[i];
done[ar[i]] = 1;
}
long long d = 1;
for (long long i = 1; i <= n; ++i) {
long long x = ar[i];
long long y = 0;
for (long long val = x + 1; val <= 2 * n; ++val) {
if (!done[val]) {
y = val;
done[val] = 1;
break;
}
}
if (!y) {
d = 0;
break;
}
ans[i] = y;
}
if (!d)
cout << -1 << "\n";
else {
for (long long i = 1; i <= n; ++i) {
cout << ar[i] << " " << ans[i] << " ";
}
cout << "\n";
}
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.util.*;
import java.io.*;
public class B{
public static void main(String[] args)
{
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
m:for(int tt=0;tt<t;tt++)
{
int n = fs.nextInt();
Set<Integer> s = new HashSet();
int[] arr = new int[n];
for(int i=0;i<n;i++)
{
int a = fs.nextInt();
arr[i] = a;
if(s.contains(a)==true)
{
System.out.println(-1);
continue m;
}
s.add(a);
}
int[] ans = new int[2*n];
boolean flag = true;
int index = 0;
for(int i=0;i<n;i++)
{
int a = arr[i];
int need = a+1;
while(s.contains(need)==true)need++;
s.add(need);
ans[index] = a;
ans[index+1] = need;
index+=2;
}
for(int i:s)
{
if(i>2*n)
{
flag = false;
break;
}
}
if(flag == false)
{
out.println(-1);
}
else
{
for(int i:ans)
{
out.print(i+" ");
}
out.println();
}
}
out.close();
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import sys
input = lambda: sys.stdin.readline().rstrip()
from bisect import bisect_left as bl, bisect_right as br
for _ in range(int(input())):
n=int(input())
a=[int(i) for i in input().split()]
b=[0]*(n*2)
for i in range(0,2*n,2):
b[i]=a[i//2]
st=set([i for i in range(1,2*n+1)])
for i in a:
st.remove(i)
st=list(st)
st.sort()
flag=1
for i in range(1,2*n,2):
z=br(st,b[i-1])
if z==len(st):
flag=0
break
b[i]=st.pop(z)
if flag==0:
print(-1)
continue
for i in range(0,(2*n)-1,2):
if b[i]>b[i+1] : b[i],b[i+1]=b[i+1],b[i]
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 |
t = int(input())
for _ in range(t):
n = int(input())
b = [int(x) for x in input().split()]
d = sorted(b)
c = []
j = 0
for i in range(1, 2 * n + 1):
if j < len(d) and i == d[j]:
j += 1
else:
c.append(i)
j = 0
out = []
for val in b:
out.append(val)
while j < len(c):
if c[j] > val:
out.append(c[j])
c[j] = -1
j = 0
break
else:
j += 1
else:
print(-1)
break
else:
print(' '.join(map(str, out)))
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
vector<int> v1;
vector<int> v2;
set<int> s;
for (int k = 0; k < t; ++k) {
int n;
bool f = true;
cin >> n;
v2.resize(2 * n);
for (int i = 0; i < 2 * n; ++i) {
s.insert(i + 1);
}
for (int i = 0; i < n; ++i) {
cin >> v2[2 * i];
if (s.find(v2[2 * i]) == s.end()) {
f = false;
break;
} else {
s.erase(v2[2 * i]);
}
}
if (!f) {
cout << -1 << endl;
continue;
} else {
for (int i = 0; i < n; ++i) {
auto ptr = s.lower_bound(v2[2 * i]);
if (ptr == s.end()) {
cout << -1 << endl;
f = false;
break;
}
v2[2 * i + 1] = *ptr;
s.erase(ptr);
}
if (f) {
for (auto x : v2) {
cout << x << " ";
}
cout << endl;
}
}
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 200005;
int a[MAX];
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
bool flag = false;
int maxc = n * 2;
bool b[205];
memset(b, false, sizeof(b));
for (int i = 0; i < n; i++) {
cin >> a[i];
b[a[i]] = true;
if (a[i] == maxc) {
flag = true;
}
}
if (flag) {
cout << -1 << endl;
continue;
}
vector<int> ans;
for (int i = 0; i < n; i++) {
for (int j = 1; j <= 2 * n; j++) {
if (b[j] == false) {
if (a[i] < j) {
b[j] = true;
ans.push_back(a[i]);
ans.push_back(j);
break;
}
}
}
}
if (ans.size() != 2 * n) {
cout << -1 << endl;
} else {
for (auto &q : ans) {
cout << q << " ";
}
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 read():
n = int(input())
b = list(map(int, input().split()))
return n, b
def solve(n, b):
a = [0] * 2 * n
marked = [False] * 2 * n
for bi in b:
marked[bi - 1] = True
for i in range(n):
a[2 * i] = b[i]
try:
next_unmarked = marked.index(False, b[i])
marked[next_unmarked] = True
a[2 * i + 1] = next_unmarked + 1
except ValueError:
return None
return a
for t in range(int(input())):
result = solve(*read())
if result:
print(*result)
else:
print(-1)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
vector<int> a(2 * n);
set<int> s;
for (int i = int(1); i < int(2 * n + 1); ++i) {
s.insert(i);
}
for (int i = 0; i < int(n); ++i) {
cin >> a[2 * i];
s.erase(a[2 * i]);
}
for (int i = 0; i < int(n); ++i) {
set<int>::iterator it = s.upper_bound(a[2 * i]);
if (it == s.end()) {
cout << -1 << '\n';
return;
}
a[2 * i + 1] = *it;
s.erase(it);
}
for (int i = 0; i < int(2 * n); ++i) {
cout << a[i] << ' ';
}
cout << '\n';
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) solve();
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long int n, m;
long long int arr[3000000];
long long int brr[3000000];
long long int an[3000000];
array<long long int, 2> dp[3000000];
string st;
vector<long long int> vec;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long int TESTS, q, a, s, b, r, k, c, p, w, d, x, i, j, y, z, xs, ys, t;
TESTS = 1;
cin >> TESTS;
while (TESTS--) {
cin >> n;
for (long long int i = 1; i <= 2 * n; i++) brr[i] = 0;
for (long long int i = 0; i <= n - 1; i++) {
cin >> arr[i];
brr[arr[i]] = 1;
}
bool pos = true;
for (long long int i = 0; i <= n - 1; i++) {
an[2 * i] = arr[i];
bool cur = false;
for (long long int j = arr[i]; j <= 2 * n; j++)
if (brr[j] == 0) {
cur = true;
an[2 * i + 1] = j;
brr[j] = 1;
break;
}
if (!cur) {
pos = false;
break;
}
}
if (!pos) {
cout << -1 << "\n";
continue;
}
for (long long int i = 0; i <= 2 * n - 1; i++) cout << an[i] << " ";
cout << "\n";
}
return 0;
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.awt.Point;
public class Main {
static int mod=(int)1e9+7;
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
StringBuilder sb=new StringBuilder();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int[] b=new int[n];
boolean[] c=new boolean[2*n+1];
for(int i=0;i<n;i++)c[b[i]=sc.nextInt()]=true;
TreeSet<Integer> treeSet=new TreeSet<>();
for(int i=1;i<=2*n;i++){
if(!c[i])treeSet.add(i);
}
StringBuilder temp=new StringBuilder();
boolean z=false;
for(int i=0;i<n;i++){
Integer x=treeSet.higher(b[i]);
if(x==null){
z=true;
break;
}
temp.append(b[i]+" "+x+" ");
treeSet.remove(x);
}
if(z)sb.append("-1\n");
else sb.append(temp+"\n");
}
System.out.println(sb);
}
// static boolean gcd(int a,int b){
// if(b==0)return a==1;
// else return gcd(b,a%b);
// }
}
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 | '''
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
'''
class NotFoundError(Exception):
def __str__(self):
return "Lexicograohic sequence nnot found"
def returnNumber(bFinal, a, index):
for i in range(len(a)):
# print(a[i], bFinal[index])
if a[i] > bFinal[index] and a[i] not in bFinal:
return a[i]
raise NotFoundError()
testCases = int(input())
for t in range(testCases):
n = int(input())
b = list(map(int, input().rstrip().split()))
a = [i for i in range(1, 2*n + 1)]
bFinal = [None]*2*n
for i in range(len(b)):
bFinal[2*i] = b[i]
for i in b:
if i in a:
a.pop(a.index(i))
# print(b)
# print(a)
# print(bFinal)
for i in range(n):
try:
bFinal[2*i + 1] = returnNumber(bFinal, a, 2*i)
# print("bFinal[2*i + 1]= ", bFinal[2*i + 1])
except NotFoundError as e:
# print(e)
bFinal = -1
break
# print(bFinal)
if type(bFinal) is list:
s = ''
for m in range(len(bFinal)):
if m == len(bFinal) - 1:
s = s + str(bFinal[m])
else:
s = s + str(bFinal[m]) + ' '
print(s)
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.print.DocFlavor;
import javax.swing.text.html.HTMLDocument;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.nio.charset.IllegalCharsetNameException;
import java.sql.SQLOutput;
import java.util.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class Main {
static int[] parent;
public static void main(String[] args) throws IOException{
Reader.init(System.in);
int t = Reader.nextInt();
while (t-->0) {
solve();
}
}
static int n ;
static int m;
static int k;
static int[] arr;
static int A;
static int B;
static void solve()throws IOException {
int n = Reader.nextInt();
int[] b = new int[n];
int[] is = new int[(2*n)+1];
for (int i = 0 ; i < n ; i++){
b[i] = Reader.nextInt();
is[b[i]] = 1;
}
TreeSet<Integer> set = new TreeSet<>();
for (int i = 1 ; i <= 2*n ; i++){
if (is[i]==0){
set.add(i);
}
}
int[] ans = new int[2*n];
for (int i = 0 ; i < 2*n ; i++){
if (i%2==0){
ans[i] = b[i/2];
}
else{
if (set.ceiling(ans[i-1])==null){
System.out.println(-1);
return;
}
else{
ans[i] = set.ceiling(ans[i-1]);
set.remove(ans[i]);
}
}
}
for (int i = 0 ; i < n+n ; i++){
System.out.print(ans[i] + " ");
}
System.out.println();
}
static int next(long[] arr, long target)
{
int start = 0, end = arr.length - 1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
// Move to right side if target is
// greater.
if (arr[mid] <= target) {
start = mid + 1;
}
// Move left side.
else {
ans = mid;
end = mid - 1;
}
}
return ans;
}
static int find(int x) {
return parent[x] == x ? x : (parent[x] = find(parent[x]));
}
static boolean merge(int x, int y) {
x = find(x);
y = find(y);
if (x==y){
return false;
}
parent[x] = y;
return true;
}
public static void sortbyColumn(int arr[][], int col)
{
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<int[]>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
}); // End of function call sort().
}
static class Node{
long a;
int i;
Node(long a, int i){
this.a = a;
this.i = i;
}
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
}
class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
// Driver method
}
class Node implements Comparable<Node>{
int a;
int b;
Node (int a , int b){
this.a = a;
this.b = b;
}
public int compareTo(Node o) {
if ((this.a%2) == (o.a%2)){
return (this.b - o.b);
}
else{
return this.a - o.a;
}
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int a[n + 1];
int b[2 * n + 1];
int temp[2 * n + 1];
for (int i = 1; i <= 2 * n; i++) temp[i] = 0;
bool con1 = false, con2 = false;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] == 1) con1 = true;
if (a[i] == 2 * n) con2 = true;
temp[a[i]] = 1;
}
if (con1 == false or con2 == true)
cout << "-1" << endl;
else {
int check = 0;
for (int i = 1; i <= n; i++) {
b[2 * i - 1] = a[i];
int j;
for (j = a[i] + 1; j <= 2 * n; j++) {
if (temp[j] == 0) {
temp[j] = 1;
b[2 * i] = j;
break;
}
}
if (j == 2 * n + 1) {
check = -1;
}
}
if (check != -1) {
for (int i = 1; i <= 2 * n; i++) cout << b[i] << " ";
cout << endl;
} else
cout << "-1" << 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;
long long M = 1000000007;
int main() {
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> v;
map<int, int> m;
int z = 0;
int j;
int b[n];
for (long long i = (0); i < (n); i++) {
cin >> b[i];
m[b[i]]++;
}
for (long long i = (0); i < (n); i++) {
j = 1;
v.push_back(b[i]);
while (m[b[i] + j] != 0) {
j++;
}
v.push_back(b[i] + j);
m[b[i] + j]++;
}
vector<int> a(2 * n, 0);
for (long long i = (0); i < (2 * n); i++) {
a[i] = v[i];
}
sort(v.begin(), v.end());
for (long long i = (0); i < (2 * n); i++) {
if (v[i] != i + 1) {
z++;
}
}
if (z == 0) {
for (long long i = (0); i < (2 * n); i++) {
cout << a[i] << " ";
}
cout << endl;
} else {
cout << -1 << endl;
}
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | for T in range(int(input())):
n = int(input())
x = list(map(int, input().split(" ")))
b = [0]
for i in x:
b.append(i)
vis = [False for cnt_used in range(2*n + 1)]
a = [0 for cnt_a in range(2*n + 1)]
set_elem_b = True
for i in range(1, n+1):
num = int(b[i])
if not vis[num]:
vis[num] = True
else:
set_elem_b = False
break
if not set_elem_b:
print(-1)
continue
find = False
for i in range(1, n+1):
find = False
for j in range(1, 2*n+1):
if not vis[j] and j > b[i]:
vis[j] = True
a[i*2-1] = b[i]
a[i*2] = j
find = True
break
if not find:
break
if not find:
print(-1)
continue
for i in range(1, 2*n+1):
print(a[i], end = ' ')
print()
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | t=int(input())
for _ in range(t):
n=int(input())
b=list(map(int,input().split()))
if 2*n in b:
print(-1)
continue
arr=[0 for i in range(2*n)]
for i in b:
arr[i-1]=1
res=[]
for i in b:
res.append(i)
k=i
while k<2*n:
if arr[k]==0:
res.append(k+1)
arr[k]=1
break
k+=1
if len(res)<2*n:
print(-1)
else:
for i in res:
print(i,end=" ")
print()
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C1315 {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
int T=fs.nextInt();
for (int tt=1; tt<=T; tt++) {
int n=fs.nextInt();
int firstNum[]=fs.readArray(n);
int secondNum[]=new int [n];
int counter[]=new int [2*n+1];
for(int i=0;i<n;i++) {
counter[firstNum[i]]++;
}
boolean isPossible=true;
for(int i=0;i<n;i++) {
for(int j=firstNum[i];j<=2*n;j++) {
if(counter[j]==0) {
counter[j]++;
secondNum[i]=j;
break;
}
}
}
for(int i=1;i<=2*n;i++) {
if(counter[i]==0) {
isPossible=false;
break;
}
}
if(isPossible) {
for(int i=0;i<n;i++) {
System.out.print(firstNum[i]+" "+secondNum[i]+" ");
}
System.out.println();
}
else {
System.out.println(-1);
}
}
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
int[][] readArray(int n,int m) {
int[][] a=new int[n][m];
for (int i=0; i<n; i++) for(int j=0; j<m; j++) a[i][j]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int M = 220;
int a[M], v[M];
int t, n;
int main() {
scanf("%d", &t);
while (t--) {
int cd;
memset(v, 0, sizeof(v));
scanf("%d", &n);
for (int i = 1; i <= 2 * n; i += 2) {
scanf("%d", &a[i]);
v[a[i]] = 1;
}
int num = 0;
for (int i = 2; i <= 2 * n; i += 2) {
if (a[i - 1] == 2 * n) {
num = 1;
break;
}
for (int j = a[i - 1] + 1; j <= 2 * n; j++) {
if (v[j] == 0) {
a[i] = j;
v[j] = 1;
break;
}
if (j == 2 * n) {
num = 1;
break;
}
}
if (num == 1) break;
}
if (num == 1)
printf("-1\n");
else {
for (int i = 1; i <= 2 * n; i++) {
printf("%d ", a[i]);
}
puts("");
}
}
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
void solve() {
int n, m, i, j, k, l, r, x, y, z, a, b, c;
cin >> n;
vector<int> v(n);
for (int zz = 0; zz < n; zz++) cin >> v[zz];
bool chk[n * 2 + 5];
memset(chk, 0, sizeof(chk));
for (i = 0; i < n; i++) {
chk[v[i]] = true;
}
bool yes;
vector<int> ans;
for (i = 0; i < n; i++) {
ans.push_back(v[i]);
yes = false;
for (j = v[i]; j <= 2 * n; j++) {
if (chk[j] == false) {
ans.push_back(j);
chk[j] = true;
yes = true;
break;
}
}
if (yes == false) {
cout << "-1"
<< "\n";
return;
}
}
for (int zzz = 0; zzz < ans.size(); zzz++) cout << ans[zzz] << " ";
cout << "\n";
}
int main() {
int t;
cin >> t;
while (t--) solve();
}
| CPP |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import java.io.*;
import java.util.*;
public class Restoring_Permutation
{
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for (int i = 1; i <= T; i++) {
int N = Integer.parseInt(br.readLine());
int[] arr = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
check(arr);
}
} catch (Exception e) {
return;
}
}
private static void check(int[] arr)
{
HashSet<Integer> seq = new HashSet<>();
for(int i=0;i<arr.length;i++){
seq.add(arr[i]);
}
String s="";
for(int i=0;i<arr.length;i++)
{
int k = arr[i] + 1;
boolean found = false;
while(k <= 2*arr.length)
{
if(!seq.contains(k)){
s += arr[i] + " " + k + " ";
found = true;
seq.add(k);
break;
}
k++;
}
if(!found){
System.out.println("-1");
return;
}
}
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 | import java.util.*;
public class Problem1315c {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] getLine = new int[n];
int[] numLine = new int[210];
for (int j = 0; j < n; j++) {
int b = sc.nextInt();
numLine[b]++;
getLine[j] = b;
}
boolean noperm = true;
int numHave = 0;
for (int j = 1; j < n * 2; j++) {
if (numLine[j] > 1) {
noperm = false;
break;
}
if (numLine[j] == 1) {
numHave++;
}
if (j % 2 == 1) {
if (numHave < j / 2 + 1) {
noperm = false;
break;
}
}
}
if (!noperm) {
System.out.println("-1");
continue;
}
for (int j = 0; j < n; j++) {
System.out.print(getLine[j] + " ");
for (int k = getLine[j] + 1; ; k++) {
if (numLine[k] == 0) {
System.out.print(k + " ");
numLine[k] = 1;
break;
}
}
}
System.out.println();
}
System.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 | from bisect import *
from collections import *
from itertools import *
import functools
import sys
import math
from decimal import *
from copy import *
from heapq import *
from fractions import *
getcontext().prec = 30
MAX = sys.maxsize
MAXN = int(1.5e6)
MOD = 10**9+7
spf = [i for i in range(MAXN)]
spf[0]=spf[1] = -1
def sieve():
for i in range(2,MAXN,2):
spf[i] = 2
for i in range(3,int(MAXN**0.5)+1):
if spf[i]==i:
for j in range(i*i,MAXN,i):
if spf[j]==j:
spf[j]=i
def fib(n,m):
if n == 0:
return [0, 1]
else:
a, b = fib(n // 2)
c = ((a%m) * ((b%m) * 2 - (a%m)))%m
d = ((a%m) * (a%m))%m + ((b)%m * (b)%m)%m
if n % 2 == 0:
return [c, d]
else:
return [d, c + d]
def charIN(x= ' '):
return(sys.stdin.readline().strip().split(x))
def arrIN(x = ' '):
return list(map(int,sys.stdin.readline().strip().split(x)))
def ncr(n,r):
num=den=1
for i in range(r):
num = (num*(n-i))%MOD
den = (den*(i+1))%MOD
return (num*(pow(den,MOD-2,MOD)))%MOD
def flush():
return sys.stdout.flush()
'''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*'''
for _ in range(int(input())):
n = int(input())
b = [0]+arrIN()
a = [0]*(2*n+1)
if (1 not in b) or (2*n in b):
print(-1)
continue
f = [1]*(2*n+1)
for i in range(1,n+1):
a[2*i-1] = b[i]
f[b[i]] = 0
rem = [i for i in range(1,2*n+1) if f[i]]
rem.sort()
for i in range(1,n+1):
idx = bisect_left(rem,b[i])
if idx==len(rem):
print(-1)
break
a[2*i] = rem[idx]
rem.pop(idx)
else:
print(*a[1:])
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | # from future import print_function,division
# range = xrange
import sys
input = sys.stdin.readline
from bisect import bisect_right
# sys.setrecursionlimit(10**9)
from sys import stdin, stdout
def main():
for _ in range(int(input())):
n = int(input())
l = [int(s) for s in input().split()]
tot = set(i+1 for i in range(2*n))
re = sorted(tot - set(l))
ans = [-1]*(2*n)
flag = 0
for i in range(n):
ans[2*i] = l[i]
w = bisect_right(re,l[i])
if w==len(re):
flag = 1
break
ans[2*i+1] = re[w]
re.remove(re[w])
if flag:
print(-1)
else:
print(*ans)
if __name__== '__main__':
main() | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
b = list(map(int, input().split()))
a = [0] * (2 * n)
used = [False] * (2 * n + 1)
for i in range(2 * n):
if i % 2 == 0:
a[i] = b[i // 2]
used[a[i]] = True
poss = True
for i in range(2 * n):
if i % 2 == 1:
for k in range(a[i-1] + 1, 2 * n + 1):
if not used[k]:
a[i] = k
used[k] = True
break
if a[i] == 0:
poss = False
break
if not poss:
print(-1)
else:
print(*a) | PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | import sys
input = sys.stdin.readline
t, = map(int, input().split())
for _ in range(t):
N = int(input())
B = list(map(int, input().split()))
vs = set(B)
cc = 0
for i in range(2*N, 0, -1):
# print(i)
if i in vs:
cc -= 1
else:
cc += 1
if cc < 0:
print(-1)
break
else:
rs = []
for b in B:
rs.append(b)
for i in range(b+1, 2*N+1):
if i not in vs:
rs.append(i)
vs.add(i)
break
print(*rs)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int b[n];
set<int> s;
for (int i = 0; i < n; i++) {
cin >> b[i];
s.insert(b[i]);
}
bool sad = false;
vector<int> a;
for (int i = 0; i < n; i++) {
a.push_back(b[i]);
int x = b[i] + 1;
while (s.find(x) != s.end()) {
x++;
}
if (x > 2 * n) {
sad = true;
break;
} else {
a.push_back(x);
s.insert(x);
}
}
if (sad == true)
cout << -1;
else {
for (int 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 | #include <bits/stdc++.h>
using namespace std;
const int N = 100 + 3;
int a[N], b[N], vis[300];
int main() {
int t, n, x;
map<int, int> mp;
cin >> t;
while (t--) {
cin >> n;
int jg = 0, bj = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
b[i * 2 - 1] = a[i];
vis[a[i]]++;
if (a[i] >= n * 2) bj = 1;
}
if (bj == 1)
printf("-1\n");
else {
for (int i = 2; i <= n * 2; i += 2) {
int ks = b[i - 1];
while (vis[ks] == 1) ks++;
b[i] = ks;
vis[ks]++;
if (ks > n * 2) {
jg = 1;
break;
}
}
if (jg == 1)
printf("-1\n");
else {
for (int i = 1; i <= 2 * n; i++) {
printf("%d ", b[i]);
}
printf("\n");
}
}
for (int i = 1; i <= 200; i++) vis[i] = 0;
}
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 N = 2e2 + 10;
const long long mod = 1e9 + 7;
const long long inf = 1e18 + 10;
long long a[N], b[N], c[N], use[N];
set<int> st[N];
void solve() {
long long n;
cin >> n;
fill(c, c + N, 0);
fill(use, use + N, 0);
for (int i = 1; i <= n; i++) {
cin >> a[i];
use[a[i]] = 1;
}
for (int i = 1; i <= n; i++)
for (int j = a[i] + 1; j <= 2 * n; j++)
if (!use[j]) st[i].insert(j);
for (int i = 1; i <= n; i++) {
if (st[i].empty()) {
cout << -1 << '\n';
return;
}
bool find = 0;
for (int j = a[i] + 1; j <= 2 * n; j++) {
if (st[i].find(j) == st[i].end() || use[j]) continue;
bool nec = 0;
for (int k = 1; k <= n; k++)
if (k != i && c[k] == 0 && st[k].size() == 1 &&
st[k].find(j) != st[k].end())
nec = 1;
if (nec) continue;
b[i] = j;
c[i] = 1;
for (int k = 1; k <= n; k++)
if (st[k].find(j) != st[k].end()) st[k].erase(j);
find = 1;
break;
}
if (find == 0) {
cout << -1 << '\n';
return;
}
}
for (int i = 1; i <= n; i++) cout << a[i] << ' ' << b[i] << ' ';
cout << '\n';
for (int i = 0; i < N; i++)
while (st[i].size()) st[i].erase(st[i].begin());
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long 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 | # restoring_permutation1.py
from collections import defaultdict
for _ in range(int(input())):
n = int(input())
b = list(map(int,input().split()))
mydick = dict()
mydick = defaultdict(lambda:0,mydick)
for i in range(n):
mydick[b[i]]=1
p = 2*n
a = []
# a.append(b[0])
ok = True
if mydick[p]==1:
print(-1)
continue
for i in range(n):
a.append(b[i])
if p-b[i]>=1:
j = b[i]+1
while j<=p:
if mydick[j]!=1:
a.append(j)
mydick[j]=1
break
j+=1
# a.append(j)
if len(a)==p:
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 | n=int(input())
for i in range(n):
a_l=int(input())
l=list(map(int,input().split()))
temp=[0]*((max(l)*2)+1)
dicti={}
flag=False
for j in range(a_l):
if(l[j]>=a_l*2):
flag=True
break
if((1 not in l) or flag==True):
print(-1)
else:
for j in range(a_l):
temp[l[j]]=1
for j in range(a_l):
for k in range(l[j]+1,len(temp)):
if(temp[k]==0):
dicti[l[j]]=k
temp[k]=1
break
sum=0
for a in dicti:
sum+=a+dicti[a]
if(sum==(((a_l*2)*(a_l*2+1))/2)):
for j in range(a_l):
print(l[j],end=" ")
print(dicti[l[j]],end=" ")
else:
print(-1)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 |
for _ in range(int(input())):
n=int(input())
l=list(map(int,input().split()))
ans=[]
a=0
for i in range(n):
ans.append(l[i])
c=l[i]
while(c in l or c in ans):
c+=1
if c>2*n:
a=-1
break
ans.append(c)
if a==-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 | t=int(input())
for i in range(0,t):
n=int(input())
b=[]
c=[]
a=list(map(int,input().split()))
k1=1
k2=n*2
if(k1 in a and k2 not in a):
for j in range(1,2*n+1):
if(j not in a):
b.append(j)
for j in range(0,n):
k=a[j]+1
f=1
while(k not in b):
k=k+1
if(k>2*n):
f=0
break
if(f==0):
print(-1)
break
c.append(a[j])
c.append(k)
b.pop(b.index(k))
if(f==0):
continue
for j in range(0,2*n):
print(c[j],end=" ")
print("")
else:
print(-1)
| PYTHON3 |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 |
import java.io.*;
import java.util.*;
/**
* A simple template for competitive programming problems.
*/
public class Solution {
//final InputReader in = new InputReader("input.txt");
final InputReader in = new InputReader(System.in);
final PrintWriter out = new PrintWriter(System.out);
final int mod = 1000000007;
final int MAX = 1000001;
boolean DEBUG = false;
void solve() {
int T = in.nextInt();
while(T-->0) {
int n = in.nextInt();
int[] b = in.nextArray(n);
int[] a = new int[n+n];
boolean[] taken = new boolean[n+n+1];
for(int i=0; i<n; i++) {
a[i+i] = b[i];
taken[b[i]] = true;
}
boolean ansFound = true;
for(int i=0; i<n; i++) {
int num = a[i+i]+1;
while(num < taken.length && taken[num]) {
num++;
}
if(num>=taken.length) {
ansFound = false;
break;
}
taken[num] = true;
a[i+i+1] = num;
}
if(!ansFound) {
out.println(-1);
continue;
}
printArray(a);
}
}
void printArray(int[] a) {
for(int k : a) {
out.print(k+ " ");
}
out.println();
}
public static void main(String[] args) throws FileNotFoundException { Solution s = new Solution(); s.solve(); s.out.close(); }
public Solution() throws FileNotFoundException { }
private static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
InputReader(InputStream stream) {
this.stream = stream;
}
InputReader(String fileName) {
InputStream stream = null;
try {
stream = new FileInputStream(fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
this.stream = stream;
}
int[] nextArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
int[][] nextMatrix(int n, int m) {
int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
matrix[i][j] = nextInt();
return matrix;
}
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();
}
String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
double nextDouble() {
return Double.parseDouble(nextString());
}
private int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try { numChars = stream.read(buf); }
catch (IOException e) { throw new InputMismatchException(); }
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
private 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; }
}
} | JAVA |
1315_C. Restoring Permutation | You are given a sequence b_1, b_2, β¦, b_n. Find the lexicographically minimal permutation a_1, a_2, β¦, a_{2n} such that b_i = min(a_{2i-1}, a_{2i}), or determine that it is impossible.
Input
Each test contains one or more test cases. The first line contains the number of test cases t (1 β€ t β€ 100).
The first line of each test case consists of one integer n β the number of elements in the sequence b (1 β€ n β€ 100).
The second line of each test case consists of n different integers b_1, β¦, b_n β elements of the sequence b (1 β€ b_i β€ 2n).
It is guaranteed that the sum of n by all test cases doesn't exceed 100.
Output
For each test case, if there is no appropriate permutation, print one number -1.
Otherwise, print 2n integers a_1, β¦, a_{2n} β required lexicographically minimal permutation of numbers from 1 to 2n.
Example
Input
5
1
1
2
4 1
3
4 1 3
4
2 3 4 5
5
1 5 7 2 8
Output
1 2
-1
4 5 1 2 3 6
-1
1 3 5 6 7 9 2 4 8 10 | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long N = 2e2 + 5, mod = 1e9 + 7, mod1 = 998244353, mod2 = 1e9 + 9,
inf = 1e18 + 7;
const long long infll = 1e18 + 7;
long long n, m, k;
long long ans, cnt, tmp, idx, mx = -inf, mn = inf;
long long x, y, z;
string s, t;
long long a[N], b[N], c[N];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long q = 1;
cin >> q;
while (q--) {
bool ck = 0;
memset(c, 0, sizeof(c));
memset(a, 0, sizeof(a));
cin >> n;
set<long long> stt;
for (long long i = 1; i <= 2 * n; i++) {
c[i] = 1;
}
for (long long i = 1; i <= n; i++) {
cin >> b[i];
if (!c[b[i]]) {
ck = 1;
break;
}
c[b[i]] = 0;
a[2 * i - 1] = b[i];
}
if (ck) {
cout << -1 << '\n';
continue;
}
for (long long i = 1; i <= 2 * n; i++) {
if (c[i]) {
bool ckk = 1;
for (long long j = 1; j <= n; j++) {
if (b[j] < i && !a[j * 2]) {
ckk = 0;
a[j * 2] = i;
break;
}
}
if (ckk) {
ck = 1;
break;
}
}
}
if (ck) {
cout << -1 << '\n';
continue;
}
for (long long iiii = 1; iiii <= 2 * n; iiii++) cout << a[iiii] << ' ';
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 | import java.io.*;
import java.util.*;
public class Codeforces {
public static void main(String[] args) throws IOException {
FastScanner fs = new FastScanner();
PrintWriter pw = new PrintWriter(System.out);
int t = fs.getInt();
while (t-- > 0) {
int n = fs.getInt();
int[] ar = fs.getIntArray(n);
HashSet<Integer> hash = new HashSet<>();
for (int i = 1; i <= 2 * n; i++)
hash.add(i);
for (int i = 0; i < ar.length; i++) {
hash.remove(ar[i]);
}
boolean hasAnswer = true;
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < ar.length; i++) {
int greater = -1;
for (Integer num : hash) {
if (num > ar[i]) {
greater = num;
break;
}
}
if (greater == -1) {
hasAnswer = false;
break;
}
hash.remove(greater);
ans.add(ar[i]);
ans.add(greater);
}
if (hasAnswer)
Utility.printList(ans);
else
System.out.println(-1);
}
pw.close();
}
}
class ModularFunctions {
static long mod = 1000000007;
static long add(long x, long y) {
long result = x + y;
return result > mod ? result - mod : result;
}
static long sub(long x, long y) {
long result = x - y;
return result < 0 ? result + mod : result;
}
static long mul(long x, long y) {
long result = x * y;
return result >= mod ? result % mod : result;
}
static long pow(long x, long y) {
long result = 1;
x %= mod;
while (y > 0) {
if ((y & 1) == 1)
result = mul(result, x);
x = mul(x, x);
y = y >>> 1;
}
return result;
}
static long modInv(long x) {
return pow(x, mod - 2);
}
}
class Utility {
static int binarySearch(int[] ar, int x, int start, int end) {
int l = start;
int r = end;
while (l <= r) {
int mid = l + (r - l) / 2;
if (ar[mid] == x) {
return mid;
} else if (ar[mid] > x) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return -1;
}
static int gcd(int a, int b) {
if (a > b) {
int t = a;
a = b;
b = t;
}
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lowerBound(int[] ar, int x) {
int l = 0;
int r = ar.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (ar[mid] >= x)
r = mid - 1;
else
l = mid + 1;
}
return r + 1;
}
static int upperBound(int[] ar, int x) {
int l = 0;
int r = ar.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (ar[mid] > x)
r = mid - 1;
else
l = mid + 1;
}
return l;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static boolean isPalindrome(String s) {
char[] ss = s.toCharArray();
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (ss[i] != ss[j]) {
return false;
}
i += 1;
j -= 1;
}
return true;
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
else if (n == 2)
return true;
else if (n % 2 == 0)
return false;
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return false;
}
return true;
}
static List<Integer> sieveOfErathosthenes(int n) {
List<Integer> list = new ArrayList<>();
boolean[] prime = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p <= Math.sqrt(n); p++) {
if (prime[p]) {
for (int i = p * p; i <= n; i += p) {
prime[i] = false;
}
}
}
for (int i = 2; i <= n; i++)
if (prime[i])
list.add(i);
return list;
}
static void printArray(int[] ar) {
for (int i = 0; i < ar.length; i++) {
System.out.print(ar[i] + " ");
}
System.out.println();
}
static void printArray(long[] ar) {
for (int i = 0; i < ar.length; i++) {
System.out.print(ar[i] + " ");
}
System.out.println();
}
static void printList(List<Integer> list) {
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println();
}
static void printLongList(List<Long> list) {
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println();
}
static long choose(long n, long k) {
if (n < k)
return 0;
if (k == 0 || k == n)
return 1;
return choose(n - 1, k - 1) + choose(n - 1, k);
}
static List<Integer> divisors(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 1; i * i < n; i++) {
if (n % i == 0)
list.add(i);
}
for (int i = (int) Math.sqrt(n); i >= 1; i--) {
if (n % i == 0)
list.add(n / i);
}
return list;
}
}
class Pair implements Comparable<Pair> {
Integer first;
Integer second;
Pair(Integer f, Integer s) {
this.first = f;
this.second = s;
}
public String toString() {
return "(" + this.first + ", " + this.second + ")";
}
@Override
public boolean equals(Object object) {
if (((Pair) object).first == this.first && ((Pair) object).second == this.second && object instanceof Pair) {
return true;
} else {
return false;
}
}
@Override
public int hashCode() {
return (String.valueOf(first) + ":" + String.valueOf(second)).hashCode();
}
@Override
public int compareTo(Pair p) {
int f = first.compareTo(p.first);
if (f != 0)
return f;
return Integer.compare(second, p.second);
}
}
class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int getInt() {
return Integer.parseInt(next());
}
long getLong() {
return Long.parseLong(next());
}
int[] getIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = getInt();
return a;
}
long[] getLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = getLong();
return a;
}
List<Integer> getIntList(int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(getInt());
return list;
}
List<Long> getLongList(int n) {
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(getLong());
return list;
}
}
| JAVA |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.