[关闭]
@xunuo 2017-01-16T14:16:14.000000Z 字数 1714 阅读 1246

Multiplication Puzzle


    Time limit 1000 ms Memory limit 65536 kB

来源:
poj:poj 1651 Multiplication Puzzle
vjudge:E-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

题意:

输入第一行,有一个数n
第二行,输入n个数
先选取一个数,乘以它的前一个数和后一个数,之后将这个数去掉,再计算下一组这样的数,直至之后两个数为止,将这些数求和。
让计算出按照这种方法计算出的和的最小值,最终输出最小值

解题思路:

区间DP......
主要是找出状态转移方程,该题的状态转移方程为:
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]);

完整代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. #define inf 0x3ffffff
  6. int a[110],dp[110][110];
  7. int main()
  8. {
  9. int n;
  10. while(scanf("%d",&n)!=EOF)
  11. {
  12. memset(dp,0,sizeof(dp));
  13. for(int i=0;i<n;i++)
  14. scanf("%d",&a[i]);
  15. /*不清楚,但是感觉它要不要并没有什么区别,都可以过。。。。。不懂-_-||*/
  16. //for(inti=0;i<n-3;i++)
  17. // dp[i][i+2]=a[i]*a[i+1]*a[i+2];
  18. for(int l=1;l<n;l++)
  19. for(int i=0;i<n-l-1;i++)
  20. {
  21. int j=i+l+1;
  22. dp[i][j]=inf;
  23. for(int k=i+1;k<j;k++)
  24. dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]);
  25. }
  26. printf("%d\n",dp[0][n-1]);
  27. }
  28. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注