#include <iostream>#include<algorithm>using namespacestd;//Rn=max (pi+rn-1)intCut_rod (int*p,intN) { if(n = =0) return 0; intQ =-1; for(inti =1; I <= N; i++) {Q= Max (Q,p[i]+cut_rod (p,n-i)); } returnq;}intMemoized_cut_rod_aux (int*p,intNint*R) { if(R[n] >=0) returnR[n]; intQ; if(n = =0) Q=0; Else{Q= -1; for(inti =1; I <= N; i++) {Q= Max (q, P[i]+memoized_cut_rod_aux (p, N-I, R)); }} R[n]=Q; returnq;}intMemoized_cut_rod (int*p,intN) { int*r =New int[n+1]; for(inti =0; I < n+1; i++) R[i]= -1; intres=Memoized_cut_rod_aux (P,N,R); Delete[]r; returnRes;}intBottom_up_rod (int*p,intN) { int*r =New int[n +1]; r[0] =0; for(intj =1; J <= N; J + +) { intQ =-1; for(inti =1; I <= J; i++) {Q= Max (q,p[i]+r[j-i]); } R[j]=Q; } for(inti =0; I < n; i++) Delete[I]r; returnr[n];}intMain () {intP[] = {0,1,5,8,9,Ten, -, -, -, -, -}; cout<< Memoized_cut_rod (P, One) <<Endl; return 0;}
Steel strip Cutting