File size: 953 Bytes
c4b0eef |
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 |
#include <algorithm>
#include <iostream>
using namespace std;
int length;
int cut[50], cost[51][51];
int dp(int x, int y) {
if (cost[x][y] > -1) {
return cost[x][y];
}
int ans = 2147483647, i;
for (i = x + 1; i < y; i++) {
ans = min(ans, dp(x, i) + dp(i, y));
}
cost[x][y] = ans + cut[y] - cut[x];
return cost[x][y];
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int i, n, j;
while (cin >> length && length) {
for (i = 0; i < 51; i++) {
for (j = i; j < 51; j++)
if (i + 1 == j) {
cost[i][j] = 0;
} else {
cost[i][j] = -1;
}
}
cin >> n;
cut[0] = 0;
cut[n + 1] = length;
for (i = 1; i <= n; i++) {
cin >> cut[i];
}
cout << "The minimum cutting is " << dp(0, n + 1) << ".\n";
}
return 0;
} |