티스토리 뷰
파일합치기는 다이나믹 프로그래밍을 이용하여 해결할 수 있다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Scanner; | |
/** | |
* 백준, 11066 | |
* 파일합치기 | |
* 다이나믹 프로그래밍 | |
* | |
* @author whitebeard | |
* | |
*/ | |
public class Problem11066 { | |
static int T; | |
static int K; | |
static int[] pages; | |
static int[] sum; | |
static int[][] memo; | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
T = sc.nextInt(); | |
while (T-- > 0) { | |
K = sc.nextInt(); | |
pages = new int[K + 1]; | |
sum = new int[K + 1]; | |
memo = new int[K + 1][K + 1]; | |
for (int[] arr : memo) | |
Arrays.fill(arr, -1); | |
for (int i = 1; i <= K; i++) { | |
pages[i] = sc.nextInt(); | |
sum[i] = sum[i - 1] + pages[i]; | |
} | |
int result = dp(1, K); | |
System.out.println(result); | |
} | |
sc.close(); | |
} | |
public static int dp(int start, int end) { | |
if (start >= end) | |
return 0; | |
if (memo[start][end] != -1) | |
return memo[start][end]; | |
if (end - start == 1) | |
return pages[start] + pages[end]; | |
memo[start][end] = Integer.MAX_VALUE; | |
for (int i = start; i <= end; i++) { | |
int temp = dp(start, i) + dp(i + 1, end) + sum[end] - sum[start - 1]; | |
memo[start][end] = Math.min(memo[start][end], temp); | |
} | |
return memo[start][end]; | |
} | |
} |
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준][DP] 10942 팰린드롬? (0) | 2018.01.25 |
---|---|
[백준][그래프][DAG] 1766 문제집 (0) | 2018.01.25 |
[백준][그래프] 11403 경로찾기 (0) | 2018.01.24 |
[백준] 1991 트리순회 (0) | 2018.01.13 |
[백준][DP] 1149 RGB 거리 (0) | 2017.11.15 |
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- SQL
- S3
- airflow
- yarn
- SPARK
- java
- 하둡
- AWS
- emr
- 백준
- bash
- ubuntu
- 하이브
- HIVE
- 오류
- Linux
- 정올
- 알고리즘
- HDFS
- oozie
- Python
- build
- error
- nodejs
- hbase
- 파이썬
- 다이나믹
- mysql
- Tez
- Hadoop
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함