AYITACM2016省赛第三周F - Cutting Sticks(切木头,区间电dp合并)

You have to cut a wood stick into pieces. The most affordable company, The Analog Cutting Machinery,
Inc. (ACM), charges money according to the length of the stick being cut. Their procedure of work
requires that they only make one cut at a time.
It is easy to notice that different selections in the order of cutting can led to different prices. For
example, consider a stick of length 10 meters that has to be cut at 2, 4 and 7 meters from one end.
There are several choices. One can be cutting first at 2, then at 4, then at 7. This leads to a price
of 10 + 8 + 6 = 24 because the first stick was of 10 meters, the resulting of 8 and the last one of 6.
Another choice could be cutting at 4, then at 2, then at 7. This would lead to a price of 10 + 4 + 6 =
20, which is a better price.
Your boss trusts your computer abilities to find out the minimum cost for cutting a given stick.
Input
The input will consist of several input cases. The first line of each test case will contain a positive
number l that represents the length of the stick to be cut. You can assume l < 1000. The next line will
contain the number n (n < 50) of cuts to be made.
The next line consists of n positive numbers ci (0 < ci < l) representing the places where the cuts
have to be done, given in strictly increasing order.
An input case with l = 0 will represent the end of the input.
Output
You have to print the cost of the optimal solution of the cutting problem, that is the minimum cost of
cutting the given stick. Format the output as shown below.
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.

分析:

区间dp,有点类似于二分法,不同的是,二分法是一直分解直到找到最优解,而区间dp是分解到不能再分解,分解开的是部分最优解,然后合并求得最优解,

一个大的区间可以分解成不同的小区间组成,选取合并后最优的作为最终的解。

例如第一组数据,最优解为:

dp[0][2]=dp[0][1]+dp[1][2]+n[2]-n[0]=50;

dp[2][4]=dp[2][3]+dp[3][4]+n[4]-n[2]=100-50=50;

dp[0][4]=dp[0[[2]+dp[2][4]+n[4]-n[2]=50+50+100=200;

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int dp[60][60],n[60];
int main()
{
    int l,m,p,i,j,k,t;
    while(scanf("%d",&t)&&t)
    {
        scanf("%d",&m);
        for(i=1; i<=m; i++)
            scanf("%d",&n[i]);
        n[0]=0;
        n[m+1]=t;
        memset(dp,0,sizeof(dp));
        for(p=1; p<=m+1; p++)//p是区间的可变结束点
            for(i=0; i<=m+1; i++) //i是区间的开始
            {
                int j=i+p;//i是开始,j是结束
                int mi=INF;
                if(j>m+1) //如果结束点超过了木棍长度,跳出循环
                    break;
                //如果区间内没有小区间是无法被合并的,最优解就是区间长度
                for(k=i+1; k<j; k++)  //从最小的区间向大的区间依次合并
                {
                    int tm=dp[i][k]+dp[k][j]+n[j]-n[i];
                    mi=min(mi,tm); //每个区间保持最小值,最优解
                }
                if(mi!=INF)
                    dp[i][j]=mi;   //找到从i到j的最优解
            }
        printf("The minimum cutting is %d.\n",dp[0][m+1]);
    }
    return 0;
}


你可能感兴趣的:(AYITACM2016省赛第三周F - Cutting Sticks(切木头,区间电dp合并))