Steel bar Cutting problem

Source: Internet
Author: User

Suddenly found that the steel bar cutting actually did not write.

Suppose the company sells a piece of steel with a length of I-inch for the price of pi (i = 1, 2, ...). Unit: USD), below is a sample price list:

Length I 1 2 3 4 5 6 7 8 9 10

Price PI 1 5 8 9 10 17 17 20 24 30

The problem with cutting steel bars is this: given a length of n-inch steel bars and a price list pi, the cutting scheme makes the sales profit RN the largest.

Of course, if the length of the steel bar of n-inch is large enough, the optimal solution may be that there is absolutely no need for cutting.

For the above price list example, we can observe all the optimal return value RI and the corresponding optimal solution scheme:

R1 = 1, cut solution 1 = 1 (no cut)

R2 = 5, cut solution 2 = 2 (no cut)

R3 = 8, cut solution 3 = 3 (no cut)

R4 = 10, cutting scheme 4 = 2 + 2

R5 = 13, cutting scheme 5 = 2 + 3

R6 = 17, cut solution 6 = 6 (no cut)

R7 = 18, cutting scheme 7 = 1 + 6 or 7 = 2 + 2 + 3

R8 = 22, cutting scheme 8 = 2 + 6

R9 = 25, cutting scheme 9 = 3 + 6

R10 = 30, cut solution 10 = 10 (no cut)

More generally, for RN (n >= 1), we can describe it with the best cut yield of shorter steel bars:

Rn = Max (Pn, R1 + Rn-1, R2 + Rn-2,..., Rn-1 + R1)

First, the steel bar is cut to the length of I and n-i two, and then the optimal cutting yield ri and rn-i are solved for both segments.

(The optimal yield for each scenario is the sum of the optimal returns of two segments), as there is no way to predict which scheme will yield the best returns,

We must look at all possible I and choose the one with the greatest benefit. If the direct sale of the original steel bar will get the most benefit,

We can of course choose not to do any cutting.

Analysis here, suppose now sell 8 inch of steel bar, how should cut?

Here are three ways to do this:

1, from top to bottom;

2. Top-Down with memo

3, from the bottom up

The simplest and most efficient bottom-up. You can try to set the length of the pipe larger, comparing the difference in operating speed between different methods. By contrast, we can deepen the understanding and memory of the idea of dynamic planning.

Example: For top-down steel bar proceeds in accordance with the above requirements, it is possible to calculate the length of the steel rods below 30. (Very low efficiency)

For top-down with a memo, you can calculate the steel bar length within 10000.

For bottom-up, the bar length can be calculated within 30000. (The above range is for reference only)

Code:

1#include <bits/stdc++.h>2 #definemin_num-1000103 #defineMax_size 1000104 intP[max_size];5 intR[max_size];6 using namespacestd;7 //**************** from top to bottom recursive implementation ***************8 DoubleCut_rod (intP[],intN) {9     if(n==0)return 0;Ten     Doubleq=Min_num; One      for(intI=1; i<=n;i++){ AQ=max (Q,p[i]+cut_rod (p,n-i)); -     } -     returnQ; the } - //**************** top-down ****************** with memo// - intMemoized_cut_rod_aux (intP[],intNintr[]) { -     intq=0; +     if(r[n]>=0) -         returnR[n]; +     if(n==0) Aq=0; at     Elseq=Min_num; -      for(intI=1; i<=n;i++){ -Q=max (Q,p[i]+memoized_cut_rod_aux (p,n-i,r)); -     } -r[n]=Q; -     returnQ; in } -  to intMemoized_cut_rod (intP[],intN) { +      for(intI=0; i<=n;i++){ -r[i]=Min_num; the     } *     returnMemoized_cut_rod_aux (p,n,r); $ }Panax Notoginseng  - //********** Bottom-up algorithm ******// the intBottom_up_cut_rod (intP[],intN) { +      for(intI=0; i<=n;i++){ Ar[i]=0; the     } +     intQ; -      for(intj=1; j<=n;j++){ $q=Min_num; $          for(intI=1; i<=j;i++){ -Q=max (q,p[i]+r[j-i]); -         } ther[j]=Q; -     }Wuyi     returnR[n]; the } - //********** test function ************// Wu intMain () { -     intnum; Aboutcout<<"Please enter the strip yield num:"<<Endl; $Cin>>num; -      for(intI=1; i<=num;i++){ -scanf"%d",&p[i]); -         } A     intN; +cout<<"Please enter the length of the steel bar : \ n"; the      while(~SCANF ("%d",&N)) { -         //cout<< "Maximum benefit is:" <<cut_rod (p,n) <<endl;//top-down recursive algorithm $         //cout<< "Maximum benefit is:" <<memoized_cut_rod (p,n) <<endl;//top-down with memo thecout<<"The maximum benefits are:"<<bottom_up_cut_rod (p,n) <<endl;//bottom-up algorithm thecout<<"Please enter the length of the steel bar : \ n"; the     } the}

Steel bar Cutting problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.