UV-10003-Cutting Sticks (interval DP)

Source: Internet
Author: User

UV-10003-Cutting Sticks (interval DP)

 

 

 

 

 

Here we will briefly describe the interval DP:

Main ideas:

The interval dynamic planning problem is generally considered for each interval. Their Optimal Values are obtained from the Optimal Values of several smaller intervals and are an application of the divide and conquer idea, divide an interval problem into smaller intervals until the intervals of a prime component, enumerate their combinations, and obtain the optimal value after merging.

Define the status: Set dp [I] [j] as the minimum price between interval I and j (actually, let's look at the question)

Implementation process:

For (int p = 1; p <= n; p ++) {// p is the length of the interval, as a stage

For (int I = 1; I <= n; I ++) {// I is the starting point of the exhaustive range

Int j = I + p-1; // j indicates the end point of the interval.

For (int k = I; k <j; k ++) // status transfer

Dp [I] [j] = min {dp [I] [k] + dp [k + 1] [j] + w [I] [j]}; // This is what the question means. Some of them need to start from k, not k + 1.

Dp [I] [j] = max {dp [I] [k] + dp [k + 1] [j] + w [I] [j]};

}

}

 

 

 

 

For this question, we must first define the state transition equation as dp [I] [j] = min (dp [I] [k], dp [k] [j]). + num [j]-num [I] (I

 

 

 

AC code:

 

# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Define INF 0x3fffffffusing namespace std; const int maxn = 60; int dp [maxn] [maxn]; // dp [I] [j] indicates that the interval I, minimum Cost between j: int num [maxn]; int main () {int L, n; while (scanf ("% d", & L) {scanf ("% d", & n); for (int I = 1; I <= n; I ++) scanf ("% d ", & num [I]); num [0] = 0; num [n + 1] = L; memset (dp, 0, sizeof (dp )); for (int p = 1; p <= n + 1; p ++) for (int I = 0; I <= n + 1; I ++) {int j = I + p; int MIN = INF; if (j> n + 1) break; f Or (int k = I + 1; k <j; k ++) {int tmp = dp [I] [k] + dp [k] [j] + num [j]-num [I]; MIN = min (MIN, tmp );} if (MIN! = INF) dp [I] [j] = MIN;} printf ("The minimum cutting is % d. \ n ", dp [0] [n + 1]);} return 0 ;}
     
    
   
  


 

 

 

 

 

 

 

 

 

 

 

Related Article

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.