-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcuttingSticksOJ.cpp
69 lines (56 loc) · 1.34 KB
/
cuttingSticksOJ.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
63
64
65
66
67
68
69
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <climits>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
while (true)
{
int l = 0, n = 0;
cin >> l;
if (l == 0)
{
break;
}
cin >> n;
if (n == 0)
{
cout << "The minimum cutting is 0.\n";
continue;
}
vector<int> a(n + 2);
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
a[0] = 0; a[n + 1] = l;
vector<vector<int>> dp(n + 2, vector<int>(n + 2, INT_MAX));
for (int i = 0; i <= n; ++i)
{
dp[i][i + 1] = 0;
}
for (int d = 0; d <= n + 1; ++d)
{
for (int i = 0; i <= n + 1 - d; ++i)
{
for (int k = i + 1; k < d + i; ++k)
{
dp[i][d + i] = min(dp[i][d + i], dp[i][k] + dp[k][d + i] + a[d + i] - a[i]);
}
}
}
cout << "The minimum cutting is " << dp[0][n + 1] << ".\n";
}
cout.flush();
return 0;
}