POJ 1651 Multiplication Puzzle
Posted Silenceneo_xw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1651 Multiplication Puzzle相关的知识,希望对你有一定的参考价值。
Multiplication Puzzle
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8760 | Accepted: 5484 |
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.
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
If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be
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张写有数字的卡片,它们排成一行,按一定的顺序从中拿走n-2张卡片(第一张和最后一张不能拿走),每次只拿走一张卡片。每拿走一张卡片,同时会获得一个分数,分数大小为:要拿走的卡片和它左右两边的卡片,这三张卡片上的数字乘积。按不同的顺序拿走中间n-2张卡片,得到的总分可能不相同,现在要你求出给定一组卡片,按照上述规则拿走卡片的可能的最小总分。
解题思路:一道区间DP的题目。设dp[l][r]表示区间[l,r]的最优解,则状态转移如下:
1、当r-l=2时,也即只有三个数时,显然dp[l][r] = num[l]*num[l+1]*num[r];
2、当r-l>2时,对区间的最后一个被拿走的数进行枚举,则dp[l][r] = min(dp[l][r], dp[l][i]+dp[i][r]+num[l]*num[i]*num[r]),其中l<i<r。
附上AC代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 105; 6 const int inf = 0x3f3f3f3f; 7 int num[maxn], dp[maxn][maxn]; 8 int n; 9 10 int dfs(int l, int r){ 11 if (abs(l-r) < 2) 12 return 0; 13 if (r-l == 2) 14 return dp[l][r] = num[l]*num[l+1]*num[r]; 15 if (dp[l][r] != inf) 16 return dp[l][r]; 17 for (int i=l+1; i<r; ++i) 18 dp[l][r] = min(dp[l][r], dfs(l, i)+dfs(i, r)+num[l]*num[i]*num[r]); 19 return dp[l][r]; 20 } 21 22 int main(){ 23 while (~scanf("%d", &n)){ 24 for (int i=0; i<n; ++i) 25 scanf("%d", num+i); 26 memset(dp, inf, sizeof(dp)); 27 dfs(0, n-1); 28 printf("%d\\n", dp[0][n-1]); 29 } 30 return 0; 31 }
以上是关于POJ 1651 Multiplication Puzzle的主要内容,如果未能解决你的问题,请参考以下文章
POJ - 1651 Multiplication Puzzle
题解POJ1651Multiplication Puzzle
poj-1651-Multiplication Puzzle--区间DP
POJ 1651 Multiplication Puzzle (区间DP)