POJ 1651 Multiplication Puzzle

LINK:8-19-小练     POJ 1651

题目:

Multiplication Puzzle

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

题意:

就是给出一组排好序列的数,你可以从中去掉数字,每一次去掉的数字就会有一次的得分,得分为去掉的这个数字乘以它前面的那个数字和它后面的那个数字。这样子去数字直到只剩下原数列的第一个数和原数列的最后一个数~

SO~从题目上就可以知道这是一道赤果果的DP╮(╯▽╰)╭

状态转移方程:dp[i][j]=min{dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]} (i<k<j)

其中dp[i][j]表示的是:第i个数字到第j个数字中间的所有数字都取完了,所得到的最小分数~,k为i到j中最后取的数字~

代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <queue>
 4 #include <math.h>
 5 #include <stdio.h>
 6 #include <string.h>
 7 using namespace std;
 8 #define MOD 1000000007
 9 int dp[110][110];
10 
11 int main()
12 {
13     int i,j,k;
14     int n;
15     int a[110];
16     while(scanf("%d",&n)!=EOF)
17     {
18         for(i=1;i<=n;i++)
19            scanf("%d",&a[i]);
20         memset(dp,0,sizeof(dp));
21         for(i=1;i<=n;i++)
22             dp[i][i+2]=a[i]*a[i+1]*a[i+2];
23         for(k=4;k<=n;k++){
24             for(i=1;i+k-1<=n;i++){
25                  dp[i][i+k-1]=10000000;
26                 for(j=i+1;j<i+k-1;j++)
27                  dp[i][i+k-1]=min(dp[i][i+k-1],dp[i][j]+dp[j][i+k-1]+a[i]*a[j]*a[i+k-1]);
28             }
29         }
30         printf("%d
",dp[1][n]);
31 
32     }
33     return 0;
34 }

//memory:212KB   time:0ms

原文地址:https://www.cnblogs.com/teilawll/p/3271309.html