-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrodcutting.cpp
More file actions
61 lines (54 loc) · 1.09 KB
/
rodcutting.cpp
File metadata and controls
61 lines (54 loc) · 1.09 KB
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
#include <limits.h>
#include <iostream>
#include <vector>
using namespace std;
int cutRodRec(int *p, int n)
{
if (n <= 0) return 0;
int max = INT_MIN;
for (int i = 0; i < n; ++i)
{
max = std::max(max, p[i] + cutRodRec(p, n - i - 1));
}
return max;
}
int rodCut(int *p, int n)
{
vector<int> dp(n + 1);
for (int i = 1; i <= n; ++i)
{
int max = INT_MIN;
for (int j = 0; j < i; j++)
{
max = std::max(p[j] + dp[i - j - 1], max);
}
dp[i] = max;
}
cout<< "Possible cuts at: ";
int nBestCut = dp[n];
int nCurLen = n;
while (nBestCut > 0)
{
for (int i = 1; i <= nCurLen; i++)
{
if (nBestCut == p[i-1] + dp[nCurLen-i])
{
nBestCut -= p[i-1];
cout<<i << " ";
nCurLen -= i;
continue;
}
}
}
cout <<endl;
return dp[n];
}
// Driver program to test above functions
int main()
{
int arr[] = {1, 5, 8, 9, 10, 17, 17, 20, 24, 30};
int size = sizeof(arr)/sizeof(arr[0]);
printf("Maximum Obtainable Value is %d\n", rodCut(arr, size));
getchar();
return 0;
}