首页 > 代码库 > POJ1651——Multiplication Puzzle
POJ1651——Multiplication Puzzle
Multiplication Puzzle
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6511 | Accepted: 3964 |
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
简单区间dp, 我们设dp[i][j]表示从i一直处理到j,所产生的最小代价,则dp[i][j] = min(dp[i][k] + dp[k][j] + a[i] * a[k] * a[j])
一开始决策想错了,答案怎么也出不来
简单区间dp, 我们设dp[i][j]表示从i一直处理到j,所产生的最小代价,则dp[i][j] = min(dp[i][k] + dp[k][j] + a[i] * a[k] * a[j])
一开始决策想错了,答案怎么也出不来
#include <map> #include <set> #include <list> #include <stack> #include <queue> #include <vector> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int inf = 0x3f3f3f3f; __int64 dp[110][110]; int a[110]; int main() { int n; while (~scanf("%d", &n)) { memset (dp, 0, sizeof(dp)); __int64 tmp; for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } for (int i = 1; i <= n - 2; ++i) { dp[i][i + 2] = a[i] * a[i + 1] * a[i + 2]; } for (int i = n; i >= 1; --i) { for (int j = i + 2; j <= n; ++j) { tmp = inf; // printf("%d --- %d ", i, j); for (int k = i + 1; k < j; ++k) { // printf("k = %d\n", k); // printf("dp[%d][%d] = %d, dp[%d][%d] = %d, a[%d] * a[%d] * a[%d] = %d\n", i, k - 1, dp[i][k - 1], k + 1, j, dp[k + 1][i], k - 1, k+1, k, a[k - 1] * a[k + 1] * a[k]); tmp = min(tmp, dp[i][k] + dp[k][j] + a[i] * a[j] * a[k]); } dp[i][j] = tmp; // printf("dp[%d][%d] = %d\n", i, j, tmp); } } printf("%I64d\n", dp[1][n]); } }
POJ1651——Multiplication Puzzle
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。