地址:http://poj.org/problem?id=1651
Multiplication Puzzle
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5840 | Accepted: 3539 |
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
题意:输入n个数字,然后出去最左端和最右端数字外每个数字都要选一遍,选过的数字不能再用,每选一个数字就将其和其左右两边能用的数字的乘积加到总价值中。给你一列数字,求最小价值。
思路:久违的一A啊。看到这题,就想到了区间分成三段,单独选一个数字以及其左右两边的两段,剩下就是处理单独选的数字的问题。在这里我把单独选出的数字认为是在区间内最后选择的数字,如果将其认为是第一个选择的数字的话需要记忆化,所以这样比较方便。
代码:
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL __int64
#define Max 0xfffffff
int num[110],dp[110][110];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&num[i]);
memset(dp,0,sizeof(dp));
for(int i=2;i<n;i++)
dp[i][i]=num[i]*num[i-1]*num[i+1];
for(int l=1;l<n-2;l++){
for(int i=2;i<n-l;i++){
int j=i+l;
dp[i][j]=Max;
for(int k=i;k<=j;k++){
dp[i][j]=min(dp[i][j],dp[i][k-1]+dp[k+1][j]+num[k]*num[i-1]*num[j+1]);
}
}
}
printf("%d\n",dp[2][n-1]);
return 0;
}