티스토리 뷰
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
58
59
60
61
62
63
|
import java.io.*;
import java.util.*;
public class boj_14889 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static int N;
static int min = Integer.MAX_VALUE;
static int[][] map;
static ArrayList<Integer> team1 = new ArrayList<>();
static ArrayList<Integer> team2 = new ArrayList<>();
public static void main(String[] args) throws IOException {
N = Integer.parseInt(br.readLine());
map = new int[N][N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
func(0);
System.out.println(min);
}
static void func(int index) {
if (index == N) {
if (team1.size() != N/2) return;
if (team2.size() != N/2) return;
int diff = calculate(team1, team2);
min = Math.min(min, diff);
}
if (team1.size() > N/2) return;
if (team2.size() > N/2) return;
team1.add(index);
func(index+1);
team1.remove(team1.size()-1);
team2.add(index);
func(index+1);
team2.remove(team2.size()-1);
}
private static int calculate(ArrayList<Integer> team1, ArrayList<Integer> team2) {
int t1 = 0;
int t2 = 0;
for (int i = 0; i < N/2; i++) {
for (int j = 0; j < N/2; j++) {
if (i == j) continue;
t1 += map[team1.get(i)][team1.get(j)];
t2 += map[team2.get(i)][team2.get(j)];
}
}
return Math.abs(t1 - t2);
}
}
|
cs |
- 재귀, 브루트포스
- 현재의 선수를 team1에 넣을지 team2에 넣을지 라고 생각
- N 제한이 20 까지니까 2^20이라 시간제한 충분
- 백트래킹하듯이 team에 넣고 다음 선수 찾기, team에서 빼기 이런식으로 func 계속 호출
- 모든 선수를 팀에 다 배정 완료되었고 인원수도 맞으면 calculate 이후에 min 갱신
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
|
public static void main(String[] args) throws IOException {
N = Integer.parseInt(br.readLine());
map = new int[N][N];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int i = 0; i < (1 << N); i++) {
int cnt = 0;
for (int j = 0; j < N; j++) {
if ((i & (1 << j)) == 0) {
cnt += 1;
}
}
if (cnt != N / 2) continue;
ArrayList<Integer> team1 = new ArrayList<>();
ArrayList<Integer> team2 = new ArrayList<>();
for (int j = 0; j < N; j++) {
if ((i & (1 << j)) == 0) {
team1.add(j);
} else {
team2.add(j);
}
}
int calculate = calculate(team1, team2);
min = Math.min(min, calculate);
}
System.out.println(min);
}
|
cs |
- 20.12.09 비트마스크 풀이 추가
'PS > boj' 카테고리의 다른 글
boj)2529 - 부등호 (0) | 2020.12.08 |
---|---|
boj)15661 - 링크와 스타트 (0) | 2020.12.06 |
boj)14501 - 퇴사 (0) | 2020.12.02 |
boj)1759 - 암호 만들기 (0) | 2020.12.01 |
boj)6603 - 로또 (0) | 2020.12.01 |
링크
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- js promise
- 드림코딩
- js array
- 킹수빈닷컴
- ㅇㄷㅇㅈ
- Spring Security
- java
- 김영한 JPA
- 이펙티브자바 스터디
- HTTP 완벽 가이드
- 이펙티브자바 아이템60
- dreamcoding
- JS 딥다이브
- 이펙티브자바 아이템59
- BOJ
- 김영한 http
- 프로그래머스
- 가상 면접 사례로 배우는 대규모 시스템 설계 기초
- 모던자바스크립트
- JPA 연관관계 매핑
- REST API
- 패스트캠퍼스 컴퓨터공학 완주반
- 프로그래머스 SQL
- js api
- HTTP 완벽가이드
- 백기선 스터디
- http
- 백준
- GCP
- 이펙티브자바
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함