天天看点

浅谈区间dp的几个经典题目和总结

UVA 10003

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

Sample Output

The minimum cutting is 200.

The minimum cutting is 22.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx=1e5+10;
const double PI=cos(-1.0);
const int inf=1e9+10;
int a[100],dp[105][105];
int main()
{
   int L,n,minn;
   while(cin>>L)
   {
       if(L==0) break;
       cin>>n;
       for(int i=1;i<=n;i++)
         cin>>a[i];
       memset(dp,0,sizeof dp);
       a[0]=0;a[n+1]=L;
       for(int i=1;i<=n+1;i++)
       {
            for(int l=0,r=i+l;r<=n+1;r++,l++)
         {
            minn=inf;
            for(int j=l+1;j<r;j++)
            {
                if(minn>dp[l][j]+dp[j][r]+a[r]-a[l])
                    minn=dp[l][j]+dp[j][r]+a[r]-a[l];
            }
            if(minn!=inf)
            dp[l][r]=minn;
         }
       }
       printf("The minimum cutting is %d.\n",dp[0][n+1]);
   }
   return 0;
}
           

Poj 1651 矩阵连乘

The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards are left in the row.

The goal is to take cards in such order as to minimize the total number of scored points.

For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring

10150 + 50205 + 10505 = 500+5000+2500 = 8000

If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be

15020 + 1205 + 1015 = 1000+100+50 = 1150.

Input

The first line of the input contains the number of cards N (3 <= N <= 100). The second line contains N integers in the range from 1 to 100, separated by spaces.

Output

Output must contain a single integer - the minimal score.

Sample Input

6

10 1 50 50 20 5

Sample Output

3650

#include <cstdio>
#include <iostream>
#include <algorithm>
#define MAXN 105
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

int dp[MAXN][MAXN];
int x[MAXN];

int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x[i]);
    }
    for(int i=n-2;i>=1;i--){
        for(int j=i+2;j<=n;j++){
            dp[i][j] = INF;
            for(int k=i+1;k<j;k++){
                dp[i][j] = min(dp[i][j],(x[i]*x[j]*x[k]+dp[i][k]+dp[k][j]) );
            }
        }
    }
    cout<<dp[1][n]<<endl;
    return 0;
}
           

最近在看刘汝佳的算法竞赛入门经典的动态规划部分,很多大佬都说的多,当你学了一些东西总要有所痕迹吧,不然下次看见又是重头开始,写博客也是一种学习方法,大家不妨试试,我文笔不是很好,有错误的地方大家指出就好,我会改进的。说下思路吧,其实区间dp吧,说到底就是一个最优子结构的问题,那肯定有人就问了,什么是最优子结构,怎么构造最优子结构,确实这也是靠平时练的,不然遇到问题也写不出(大佬忽略)。最优子结构换句话来说就是叫你递推罢了。就是从小的子结构推到大的子结构,再推的过程中有一个转移方程而已。