Skip to content

Commit 800b1fd

Browse files
authored
Merge pull request #826 from AlgorithmWithGod/LiiNi-coder
[20250905] BOJ / G5 / 솜 사이 수열 / 이인희
2 parents 010619c + 928f1ba commit 800b1fd

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.Arrays;
6+
import java.util.StringTokenizer;
7+
8+
public class Main {
9+
private static BufferedReader br;
10+
private static int N;
11+
private static int[] X;
12+
private static int[] answer;
13+
private static boolean[] used;
14+
private static boolean found;
15+
16+
public static void main(String[] args) throws IOException {
17+
br = new BufferedReader(new InputStreamReader(System.in));
18+
N = Integer.parseInt(br.readLine());
19+
X = new int[N];
20+
StringTokenizer st = new StringTokenizer(br.readLine());
21+
for (int i = 0; i < N; i++) {
22+
X[i] = Integer.parseInt(st.nextToken());
23+
}
24+
Arrays.sort(X);
25+
answer = new int[2 * N];
26+
Arrays.fill(answer, -1);
27+
used = new boolean[N];
28+
29+
backtrack(0);
30+
31+
if (!found) {
32+
System.out.println(-1);
33+
}
34+
}
35+
36+
private static void backtrack(int idx) {
37+
if (found)
38+
return;
39+
40+
41+
if (idx == 2 * N) {
42+
StringBuilder sb = new StringBuilder();
43+
for (int v : answer) sb.append(v).append(' ');
44+
System.out.println(sb.toString());
45+
found = true;
46+
return;
47+
}
48+
if (answer[idx] != -1) {
49+
backtrack(idx + 1);
50+
return;
51+
}
52+
53+
for (int i = 0; i < N; i++) {
54+
if (used[i])
55+
continue;
56+
int val = X[i];
57+
int secIdx = idx + val + 1;
58+
if (secIdx >= 2 * N || answer[secIdx] != -1) continue;
59+
60+
answer[idx] = val;
61+
answer[secIdx] = val;
62+
used[i] = true;
63+
64+
backtrack(idx + 1);
65+
66+
answer[idx] = -1;
67+
answer[secIdx] = -1;
68+
used[i] = false;
69+
}
70+
}
71+
}
72+
```

0 commit comments

Comments
 (0)