poj 1651 Multiplication Puzzle(区间dp)

Description

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 
10*1*50 + 50*20*5 + 10*50*5 = 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 
1*50*20 + 1*20*5 + 10*1*5 = 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

Source

Northeastern Europe 2001, Far-Eastern Subregion
 
题意:有一个数组,要求从中选出n-2个数(第一个和最后一个除外),每次选出一个数所获得的价值为a[i]*a[i-1]*a[i+1],问选完时所获得的价值综合的最小值。
这不是赤裸裸的区间dp吗?dp[i][j]表示i到j所获得的最小值,
状态转移方程 dp[i][j]=min(dp[i][j],dp[i][k-1]+dp[k+1][j]+a[k]*a[i-1]*a[j+1]);(dp[i][j]=inf)(1<i,j<n)
初始化:dp[i][i]=a[i]*a[i-1]*a[i+1];(1<i<n)
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<vector>
 5 #include<set>
 6 #include<algorithm>
 7 #include<cmath>
 8 #include<stdlib.h>
 9 #include<map>
10 using namespace std;
11 #define N 2006
12 #define inf 1<<26
13 int n;
14 int a[N];
15 int dp[N][N];
16 int main()
17 {
18     while(scanf("%d",&n)==1)
19     {
20         for(int i=1;i<=n;i++)
21         {
22             scanf("%d",&a[i]);
23         }
24         memset(dp,0,sizeof(dp));
25         for(int i=2;i<n;i++)
26           dp[i][i]=a[i]*a[i-1]*a[i+1];
27         for(int len=1;len<n;len++)
28         {
29             for(int i=2;i+len<n;i++)
30             {
31                 int j=i+len;
32                 int tmp=inf;
33                 for(int k=i;k<=j;k++)
34                 {
35                     tmp=min(tmp,dp[i][k-1]+dp[k+1][j]+a[k]*a[i-1]*a[j+1]);
36                 }
37                 dp[i][j]=tmp;    
38             }
39         }
40         printf("%d\n",dp[2][n-1]);
41     }
42     return 0;
43 }
View Code

 

posted @ 2015-08-14 21:36  UniqueColor  阅读(201)  评论(0编辑  收藏  举报