Skip to content

Commit 3fd580f

Browse files
committed
[Silver I] Title: 스타트와 링크, Time: 404 ms, Memory: 18676 KB -BaekjoonHub
1 parent 147d5ea commit 3fd580f

2 files changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# [Silver I] 스타트와 링크 - 14889
2+
3+
[문제 링크](https://www.acmicpc.net/problem/14889)
4+
5+
### 성능 요약
6+
7+
메모리: 18676 KB, 시간: 404 ms
8+
9+
### 분류
10+
11+
브루트포스 알고리즘, 백트래킹
12+
13+
### 제출 일자
14+
15+
2026년 3월 27일 10:09:23
16+
17+
### 문제 설명
18+
19+
<p>오늘은 스타트링크에 다니는 사람들이 모여서 축구를 해보려고 한다. 축구는 평일 오후에 하고 의무 참석도 아니다. 축구를 하기 위해 모인 사람은 총 N명이고 신기하게도 N은 짝수이다. 이제 N/2명으로 이루어진 스타트 팀과 링크 팀으로 사람들을 나눠야 한다.</p>
20+
21+
<p>BOJ를 운영하는 회사 답게 사람에게 번호를 1부터 N까지로 배정했고, 아래와 같은 능력치를 조사했다. 능력치 S<sub>ij</sub>는 i번 사람과 j번 사람이 같은 팀에 속했을 때, 팀에 더해지는 능력치이다. 팀의 능력치는 팀에 속한 모든 쌍의 능력치 S<sub>ij</sub>의 합이다. S<sub>ij</sub>는 S<sub>ji</sub>와 다를 수도 있으며, i번 사람과 j번 사람이 같은 팀에 속했을 때, 팀에 더해지는 능력치는 S<sub>ij</sub>와 S<sub>ji</sub>이다.</p>
22+
23+
<p>N=4이고, S가 아래와 같은 경우를 살펴보자.</p>
24+
25+
<table class="table table-bordered" style="width:20%">
26+
<thead>
27+
<tr>
28+
<th>i\j</th>
29+
<th>1</th>
30+
<th>2</th>
31+
<th>3</th>
32+
<th>4</th>
33+
</tr>
34+
</thead>
35+
<tbody>
36+
<tr>
37+
<th>1</th>
38+
<td> </td>
39+
<td>1</td>
40+
<td>2</td>
41+
<td>3</td>
42+
</tr>
43+
<tr>
44+
<th>2</th>
45+
<td>4</td>
46+
<td> </td>
47+
<td>5</td>
48+
<td>6</td>
49+
</tr>
50+
<tr>
51+
<th>3</th>
52+
<td>7</td>
53+
<td>1</td>
54+
<td> </td>
55+
<td>2</td>
56+
</tr>
57+
<tr>
58+
<th>4</th>
59+
<td>3</td>
60+
<td>4</td>
61+
<td>5</td>
62+
<td> </td>
63+
</tr>
64+
</tbody>
65+
</table>
66+
67+
<p>예를 들어, 1, 2번이 스타트 팀, 3, 4번이 링크 팀에 속한 경우에 두 팀의 능력치는 아래와 같다.</p>
68+
69+
<ul>
70+
<li>스타트 팀: S<sub>12</sub> + S<sub>21</sub> = 1 + 4 = 5</li>
71+
<li>링크 팀: S<sub>34</sub> + S<sub>43</sub> = 2 + 5 = 7</li>
72+
</ul>
73+
74+
<p>1, 3번이 스타트 팀, 2, 4번이 링크 팀에 속하면, 두 팀의 능력치는 아래와 같다.</p>
75+
76+
<ul>
77+
<li>스타트 팀: S<sub>13</sub> + S<sub>31</sub> = 2 + 7 = 9</li>
78+
<li>링크 팀: S<sub>24</sub> + S<sub>42</sub> = 6 + 4 = 10</li>
79+
</ul>
80+
81+
<p>축구를 재미있게 하기 위해서 스타트 팀의 능력치와 링크 팀의 능력치의 차이를 최소로 하려고 한다. 위의 예제와 같은 경우에는 1, 4번이 스타트 팀, 2, 3번 팀이 링크 팀에 속하면 스타트 팀의 능력치는 6, 링크 팀의 능력치는 6이 되어서 차이가 0이 되고 이 값이 최소이다.</p>
82+
83+
### 입력
84+
85+
<p>첫째 줄에 N(4 ≤ N ≤ 20, N은 짝수)이 주어진다. 둘째 줄부터 N개의 줄에 S가 주어진다. 각 줄은 N개의 수로 이루어져 있고, i번 줄의 j번째 수는 S<sub>ij</sub> 이다. S<sub>ii</sub>는 항상 0이고, 나머지 S<sub>ij</sub>는 1보다 크거나 같고, 100보다 작거나 같은 정수이다.</p>
86+
87+
### 출력
88+
89+
<p>첫째 줄에 스타트 팀과 링크 팀의 능력치의 차이의 최솟값을 출력한다.</p>
90+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.StringTokenizer;
5+
6+
public class Main {
7+
8+
static int N;
9+
static int[][] teamStatInfo;
10+
static boolean[] selected;
11+
static int result = Integer.MAX_VALUE;
12+
13+
public static void main(String[] args) throws IOException {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
N = Integer.parseInt(br.readLine());
16+
17+
teamStatInfo = new int[N][N];
18+
selected = new boolean[N];
19+
20+
for (int i = 0; i < N; i++) {
21+
StringTokenizer st = new StringTokenizer(br.readLine());
22+
for (int j = 0; j < N; j++) {
23+
teamStatInfo[i][j] = Integer.parseInt(st.nextToken());
24+
}
25+
}
26+
27+
makeTeam(0, 0);
28+
System.out.println(result);
29+
}
30+
31+
private static void makeTeam(int index, int count) {
32+
if (count == N / 2) {
33+
calculateDifference();
34+
return;
35+
}
36+
37+
for (int i = index; i < N; i++) {
38+
selected[i] = true;
39+
makeTeam(i + 1, count + 1);
40+
selected[i] = false;
41+
}
42+
}
43+
44+
private static void calculateDifference() {
45+
int startTeamStat = 0;
46+
int linkTeamStat = 0;
47+
48+
for (int i = 0; i < N; i++) {
49+
for (int j = i + 1; j < N; j++) {
50+
if (selected[i] && selected[j]) {
51+
startTeamStat += teamStatInfo[i][j] + teamStatInfo[j][i];
52+
} else if (!selected[i] && !selected[j]) {
53+
linkTeamStat += teamStatInfo[i][j] + teamStatInfo[j][i];
54+
}
55+
}
56+
}
57+
58+
result = Math.min(result, Math.abs(startTeamStat - linkTeamStat));
59+
}
60+
}

0 commit comments

Comments
 (0)