POJ 1651 Multiplication Puzzle (区间dp)

Multiplication Puzzle
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6466   Accepted: 3927

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
题目大意:开始一行输入一个数n,然后接下来一行输入n个数;
你可以在第 2 个数和第 n-1 个数中去掉任意一个数,此时计算出它和它两边的数的乘积,然后知道去掉这个区间的所有数,计算总的和;
AC代码:区间dp
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<queue>
 6 #include<string>
 7 #include<cmath>
 8 using namespace std;
 9 const int INF = 1e8;
10 int a[110];
11 int dp[110][110];
12 int main()
13 {
14   int n;
15   while(cin>>n)
16   {
17       for(int i=1; i<=n; i++)
18         scanf("%d",&a[i]);
19       for(int i=n-2 ; i>=1; i--)//倒着遍历所有的区间;
20       {
21           for(int j=i+2; j<=n; j++)
22           {
23               dp[i][j] = INF;       //开始初始化全部为最大值;
24                 for(int k=i+1; k<=j-1; k++)
25                  dp[i][j] = min(dp[i][j],a[i]*a[k]*a[j]+dp[i][k]+dp[k][j]);
26           }
27       }
28       printf("%d\n",dp[1][n]); 
29   }
30   return 0;
31 }

 

posted on 2014-10-16 16:02  细雨微光  阅读(730)  评论(0编辑  收藏  举报