-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcutting_rod_to_minimize_cost.cpp
More file actions
92 lines (68 loc) · 1.78 KB
/
cutting_rod_to_minimize_cost.cpp
File metadata and controls
92 lines (68 loc) · 1.78 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
vector<int> ans;
vector<int> cuts;
vector<vector<LL> > dp;
vector<vector<int> > parent;
LL rec(int l, int r){
if(l+1 >= r)return 0;
LL &ret = dp[l][r];
if(ret != -1) return ret;
ret = LLONG_MAX;
int bestind; //stores the best index
for(int i = l + 1; i < r; i++) {
//recurrence
LL p = rec(l,i) + rec(i,r) + (LL)cuts[r] - (LL)cuts[l];
//update best
//note that we choose lexicographically smallest index
//if multiple give same cost
if(p < ret) {
ret = p;
bestind = i;
}
}
//store parent of (l, r)
parent[l][r] = bestind;
return ret;
}
//function for building solution
void back(int l, int r){
//base case
if(l+1 >= r)return;
//first choose parent of (l,r)
ans.push_back(cuts[parent[l][r]]);
//call back recursively for two new segments
//calling left segment first because we want lexicographically smallest
back(l, parent[l][r]);
back(parent[l][r], r);
}
void solve(int A, vector<int> &B) {
//insert A and 0
B.push_back(A);
B.insert(B.begin(),0);
int n = B.size();
cuts.clear();
for(int i = 0; i < n; i++)
cuts.push_back(B[i]);
//initialise dp array
dp.resize(n);
parent.resize(n);
ans.clear();
for(int i = 0; i < n; i++){
dp[i].resize(n);
parent[i].resize(n);
for(int j = 0; j < n; j++)
dp[i][j] =- 1;
}
//call recurrence
LL best = rec(0, n-1);
//build solution
back(0, n-1);
}
int main() {
int arr[] = {1, 2, 5};
vector<int> v(arr, arr + sizeof arr / sizeof arr[0]);
solve(6, v);
for(int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
return 0;