-
Notifications
You must be signed in to change notification settings - Fork 0
/
10003.cpp
62 lines (50 loc) · 877 Bytes
/
10003.cpp
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
/*
Author: Andreea Musat
10 feb 2018
*/
#include <bits/stdc++.h>
using namespace std;
const int MD = 64;
vector<int> cut_point;
long long memo[MD][MD];
int solve(int i, int j)
{
if (memo[i][j] != -1)
{
return memo[i][j];
}
if (i + 1 >= j)
{
memo[i][j] = 0;
return 0;
}
long long best = LLONG_MAX;
long long curr = 0;
for (int k = i + 1; k <= j - 1; k++)
{
curr = cut_point[j] - cut_point[i];
curr += solve(i, k) + solve(k, j);
best = min(best, curr);
}
memo[i][j] = best;
return best;
}
int main()
{
int l, n, i;
while (true)
{
cin >> l;
if (l == 0) break;
cin >> n;
memset(memo, -1, sizeof(memo));
cut_point.clear();
cut_point.resize(n + 2);
cut_point[0] = 0;
cut_point[n + 1] = l;
for (i = 1; i <= n; i++)
cin >> cut_point[i];
cout << "The minimum cutting is " << solve(0, n + 1) << ".\n";
}
return 0;
}