Baekjoon Online Judge No.11066 [파일 합치기]
Problem
Code
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
64
65
66
67
68
69
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 2100000000;
int tc;
vector<int> answer;
vector<int> sum;
int cost(int i, int j) {
if (i > 0) return sum[j] - sum[i - 1];
else return sum[j];
}
int DP(vector<int>& chaps) {
int num = chaps.size();
vector<vector<int>> dp;
dp.assign(num, vector<int>(num, 0));
// dp[k][k] == 0
for (int i = 1; i < num; i++)
dp[i - 1][i] = chaps[i] + chaps[i - 1];
for (int j = 2; j < num; j++) {
for (int i = 0; i < num - j; i++) {
// (i, i + j)
int I = i;
int J = i + j;
int ans = INF;
for (int k = I; k < J; k++) {
ans = min(ans, dp[I][k] + dp[k + 1][J] + cost(I, J));
}
dp[I][J] = ans;
}
}
return dp[0][num - 1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> tc;
while (tc--) {
int nums;
cin >> nums;
vector<int> input(nums);
sum.assign(nums, 0);
for (int i = 0; i < nums; i++) {
cin >> input[i];
if (i) sum[i] += sum[i - 1] + input[i];
else sum[i] = input[i];
}
answer.push_back(DP(input));
}
for (auto elem : answer) cout << elem << '\n';
}
|
cs |
How to solve
아래 문제와 똑같은 원리로 풀면 된다.
728x90
'Algorithm (C++ based) > BOJ' 카테고리의 다른 글
백준 No.13549 [숨바꼭질3] (0) | 2021.02.16 |
---|---|
백준 No.1504 [특정한 최단 경로] (0) | 2021.02.09 |
백준 No.12865 [평범한 배낭] (0) | 2021.02.06 |
백준 No.10942 [팰린드롬?] (0) | 2021.02.05 |
BOJ No.2798 [블랙잭] (0) | 2021.02.05 |
댓글