Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 서비스 디스커버리
- spring boot
- dp
- 스택
- Java
- 메모이제이션
- 구현
- 게이트웨이
- Logback
- 도커
- Spring Cloud Config
- 이분 매칭
- 트리
- 주울
- spring cloud
- BFS
- 이분 탐색
- Gradle
- docker-compose
- 다익스트라
- Zuul
- ZuulFilter
- 플로이드 와샬
- 백트래킹
- 달팽이
- 유레카
- 스프링 시큐리티
- 구간 트리
- 완전 탐색
- 비트마스킹
Archives
- Today
- Total
Hello, Freakin world!
[백준 14889번] 스타트와 링크 본문
14889번: 스타트와 링크
예제 2의 경우에 (1, 3, 6), (2, 4, 5)로 팀을 나누면 되고, 예제 3의 경우에는 (1, 2, 4, 5), (3, 6, 7, 8)로 팀을 나누면 된다.
www.acmicpc.net
문제 풀이
재귀를 이용해 팀을 나눕니다. 그리고 나눈 팀들을 비트로 변환하여 저장합니다.
그리고 비트를 이용해 완전탐색하면서 팀의 능력치를 구합니다.
구현 상의 팁
저는 하나의 팀을 구하고 비트 XOR 연산을 통해 반대편 팀을 구했습니다.
(다른 분들의 코드를 보니 팀을 나누는 시점에 각각의 팀들을 저장하는 것도 좋아보이긴 하지만...)
import java.io.*;
import java.util.*;
/*
백준 14889번 - 스타트와 링크
https://www.acmicpc.net/problem/14889
*/
public class Main {
static int n, INF = 987654321;
static int[][] ability;
static List<Integer> statusSet = new ArrayList<>();
public static void main(String[] args) throws IOException {
InputReader reader = new InputReader("testcase.txt");
// InputReader reader = new InputReader();
n = reader.readInt();
ability = new int[n+1][n+1];
for (int i = 1; i <= n; i++) {
StringTokenizer st = new StringTokenizer(reader.readLine());
for (int j = 1; j <= n; j++) {
ability[i][j] = Integer.parseInt(st.nextToken());
}
}
selectStartMember(1,0,0);
System.out.println(solve());
}
private static int solve() {
int diff = INF;
for (int selected : statusSet) {
//start 팀 능력 계산
int startAbility = 0;
List<Integer> startTeam = convertToList(selected);
// System.out.println("startTeam :" + startTeam);
for (int i = 0; i < startTeam.size(); i++) {
int memberA = startTeam.get(i);
for (int j = i+1; j < startTeam.size(); j++) {
int memberB = startTeam.get(j);
startAbility += ability[memberA][memberB];
startAbility += ability[memberB][memberA];
}
}
//link 팀 능력 계산
int linkAbility = 0;
int fullBit = (1 << n+1)-1;
int selectedLink = fullBit ^ selected;
List<Integer> linkTeam = convertToList(selectedLink);
// System.out.println("linkTeam :" + linkTeam);
// System.out.println();
for (int i = 0; i < linkTeam.size(); i++) {
int memberA = linkTeam.get(i);
for (int j = i+1; j < linkTeam.size(); j++) {
int memberB = linkTeam.get(j);
linkAbility += ability[memberA][memberB];
linkAbility += ability[memberB][memberA];
}
}
diff = Math.min(Math.abs(startAbility - linkAbility), diff);
}
return diff;
}
/*
비트를 리스트로 변환
*/
private static List<Integer> convertToList(int bit) {
List<Integer> team = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if((bit & (1 << i)) != 0) team.add(i);
}
return team;
}
private static void selectStartMember(int index, int count, int selected) {
if(count == n/2) {
statusSet.add(selected); return;
}
if(index > n) return;
//선택한 경우
selectStartMember(index+1, count+1, selected | 1 << index);
//선택하지 않은 경우
selectStartMember(index+1, count, selected);
}
}
class InputReader {
private BufferedReader br;
public InputReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public InputReader(String filepath) {
try {
br = new BufferedReader(new FileReader(filepath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public String readLine() throws IOException {
return br.readLine();
}
public int readInt() throws IOException {
return Integer.parseInt(readLine());
}
}
'알고리즘 > PS' 카테고리의 다른 글
[백준 1927번] 최소 힙 (0) | 2020.09.16 |
---|---|
[백준 14499번] 주사위 굴리기 (0) | 2020.09.15 |
[백준 15686번] 치킨 배달 (0) | 2020.09.14 |
[백준 14502번] 연구소 (0) | 2020.09.13 |
[백준 1541번] 잃어버린 괄호 - 쉽고 간단한 풀이 (0) | 2020.09.13 |