-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcut_rod.cpp
More file actions
35 lines (32 loc) · 1005 Bytes
/
cut_rod.cpp
File metadata and controls
35 lines (32 loc) · 1005 Bytes
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
#include <limits>
#include <vector>
// TODO: check on p
// top-down
std::size_t cut_rod(std::size_t size, std::size_t n, std::vector<std::size_t>& r)
{
if (r[n] != std::numeric_limits<std::size_t>::max()) { return r[n]; }
std::size_t q;
if (n == 0) { q = 0; }
else { q = std::numeric_limits<std::size_t>::max(); }
for (auto i = 1; i < n + 1; ++i) { q = std::max(q, p[i] + cut_rod(p, n - 1, r)); }
r[n] = q;
return q;
}
std::size_t cut_rod(std::size_t size, std::size_t n)
{
std::vector<std::size_t> r(n + 1, std::numeric_limits<std::size_t>::max());
return cut_rod(size, n, r);
}
std::size_t cut_rod_bu(std::size_t size, std::size_t n)
{
std::vector<std::size_t> r(n + 1, std::numeric_limits<std::size_t>::max());
r[0] = 0;
for (auto j = 1; j < n + 1; ++j) {
auto q = std::numeric_limits<std::size_t>::max();
for (auto i = 1; i < j; ++i) {
q = std::max(q, p[i] + r[j - 1]);
}
r[j] = q;
}
return r[n];
}