-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFJPermutation.java
More file actions
58 lines (55 loc) · 1.63 KB
/
FJPermutation.java
File metadata and controls
58 lines (55 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.*;
public class FJPermutation {
public static void solve() {
Scanner scanner = new Scanner(System.in);
int N = Integer.parseInt(scanner.nextLine());
String[] input = scanner.nextLine().split(" ");
int[] H = new int[N];
for (int i = 0; i < N; i++) {
H[i] = Integer.parseInt(input[i]);
}
if (H[N - 1] != 1) {
System.out.println(-1);
return;
}
boolean[] remaining = new boolean[N + 1];
Arrays.fill(remaining, true);
remaining[0] = false; // 0 is not used
for (int h : H) {
if (!remaining[h]) {
System.out.println(-1);
return;
}
remaining[h] = false;
}
List<Integer> ends = new ArrayList<>();
for (int x = 1; x <= N; x++) {
if (remaining[x]) {
ends.add(x);
}
}
int[] ans = new int[N];
Arrays.fill(ans, -1);
int l = 0, r = N - 1;
ans[l] = ends.get(0);
ans[r] = ends.get(1);
for (int i = 2; i < ends.size(); i++) {
int h = H[i - 1];
if (ans[l] > ans[r]) {
l++;
ans[l] = h;
} else {
r--;
ans[r] = h;
}
}
System.out.println(Arrays.toString(ans).replaceAll("[\\[\\],]", ""));
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < T; i++) {
solve();
}
}
}