UVA 10003 Cutting Sticks
Description
Your mission is to cut sticks for a company called analog cutting Machinery (ACM). The cost of cutting a stick depends on the length of the stick. And cut a stick one at a time. Obviously, the order of the different cuts will have different costs. For example, a 10-metre-long stick must be cut in the 2nd, 4, 7-metre area. There are several options at this time. You can choose to cut 2 meters first, then cut 4 meters, and then cut 7 meters in the final place. The cost of this option is: 10+8+6=24. For the first cut, the stick is 10 meters long, the second cut is 8 meters long, and the stick is 6 meters long when the third time is cut. But if you choose to cut 4 meters first, then cut 2 meters, and finally cut 7 meters, the cost is: 10+4+6=20, the cost is a better choice. Your boss believes that your computer skills must be able to find the minimum cost of cutting a stick.
Input
Each set of test Data 3 columns, the first column has 1 integer L (l<1000), representing the length of the stick to be cut. The second column has an integer N (n<50) that represents the number of times to cut. The third column has n positive integer ci (0 < Ci < L) where the stick needs to be cut. These n integers are all different and are arranged from small to large.
Output
For each set of test data, the minimum cut cost is output.
Sample Input
100
3
25 50 75
10
4
4 5 7 8
0
Sample Output
The minimum cutting is 200.
The minimum cutting is 22.
Topic: The topic of Chinese has been given. Problem-solving ideas: Reverse thinking, as a stick merger, recursive formula:d p[i][j] = max (Dp[i][k] + dp[k][j] + len[j]-len[i]) (I < K < J);
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>using namespace STD;inta[ -], dp[ -][ -];intMain () {intL, N; while(scanf("%d", &l)! = EOF, L) {memset(DP,0,sizeof(DP));scanf("%d", &n); for(inti =1; I <= N; i++) {scanf("%d", &a[i]); } a[0] =0, A[n +1] = l;//Because you want to use the reverse thinking, from the small pieces to start the combination, so call the tail for(intLen =2; Len <= n +1; len++) {//Reverse thinking for(inti =0; i + Len <= n +1; i++) {intj = i + len; DP[I][J] =0xFFFFFFF; for(intK = i +1; K < J; k++) {//k is the dividing point between I and J inttemp = Dp[i][k] + dp[k][j] + a[j]-a[i];if(Temp < DP[I][J]) dp[i][j] = temp;//DP Array Record split point I to Split Point J minimum consumption in various combination modes} } }printf("The minimum cutting is%d.\n", dp[0][n +1]); }return 0;}
UVA 10003 Cutting Sticks (DP)