poj1651

Multiplication Puzzle

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7252   Accepted: 4478

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

Source

Northeastern Europe 2001, Far-Eastern Subregion

区间dp,挺简单的。

给你一组数字,第一个和最后一个数字不可以取出去,其它任意取出去,当你要取出一个数字时,它有一个代价,这个代价就是与它相邻的两个数的乘积,求除了首位两位数字,把其他数字都取出来,它们的代价之和的最小值........

#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define repd(i, a, b) for(int i = b; i >= a; i--)
#define sfi(n) scanf("%d", &n)
#define pfi(n) printf("%d\n", n)
#define sfi2(n, m) scanf("%d%d", &n, &m)
#define pfi2(n, m) printf("%d %d\n", n, m)
#define pfi3(a, b, c) printf("%d %d %d\n", a, b, c)
#define MAXN 105
const int INF = 0x3f3f3f3f;
int a[MAXN];
int dp[MAXN][MAXN];
int main()
{
    int n;
    while(~sfi(n))
    {
        repu(i, 0, n) sfi(a[i]);
        _cle(dp, 0x3f);
        dp[0][0] = a[0];
        a[n + 1] = 1;
        repu(i, 1, n + 1) dp[i][i] = a[i - 1] * a[i] * a[i + 1];
        repu(i, 1, n) repu(j, 0, i) dp[i][j] = 0;
        for(int i = n - 1; i > 0; i--)
        repu(j, i + 1, n) repu(k, i, j + 1)
           dp[i][j] = min(dp[i][j], dp[i][k - 1] + a[k] * a[i - 1] * a[j + 1] + dp[k + 1][j]);
        pfi(dp[1][n - 2]);
    }
    return 0;
}

时间: 2024-10-13 08:16:51

poj1651的相关文章

poj1651 区间dp

1 //Accepted 200 KB 0 ms 2 //dp区间 3 //dp[i][j]=min(dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]) i<k<j 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 using namespace std; 8 const int imax_n = 105; 9 const int Pinf = 100000000

POJ1651 Multiplication Puzzle 区间dp

题意:给一个序列,每次选一个数(记为b),计算左边离他最近的数(记为a),右边离他最近的数(记为c),计算a*b*c,sum+=a*b*c 重复操作直到剩下开头结尾两个数 不同的方案对应不同的sum 计算最小的sum值 分析:典型的区间dp,dp[i][j]表示把从i到j所有的数都选走得到的最小值 dp[i][j]=min(dp[i][j],dp[i][k-1]+dp[k+1][j]+num[k]*num[i-1]*num[j+1]): 注:记得预处理dp[i][i],i(2~n-1) #inc

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

poj1651 最优矩阵乘法动态规划解题

题目描述: 有若干个矩阵{Ai},元素都为整数且已知矩阵大小. 如果要计算所有矩阵的乘积A1 * A2 * A3 .. Am,最少要多少次整数乘法? 输入 第一行一个整数n(n <= 100),表示一共有n-1个矩阵.第二行n个整数B1, B2, B3... Bn(Bi <= 100),第i个数Bi表示第i个矩阵的行数和第i-1个矩阵的列数.等价地,可以认为第j个矩阵Aj(1 <= j <= n - 1)的行数为Bj,列数为Bj+1. 输出 一个整数,表示最少所需的乘法次数 采用动

poj1651(区间dp)

题目连接:http://poj.org/problem?id=1651 题意:给出一组N个数,每次从中抽出一个数(第一和最后一个不能抽),该次的得分即为抽出的数与相邻两个数的乘积.直到只剩下首尾两个数为止.问最小得分? 分析:区间dp,记忆化搜索,dp[l][r]表示去掉l~r中所有数(不包括l.r)后得到的最小值,那么当前区间最小值为dp[l][r]=min(dp[l][r],dp[l][i]+dp[i][r]+a[l]*a[r]*a[i]). #include <cstdio> #incl

poj1651 Multiplication Puzzle(简单区间dp)

题目链接:http://poj.org/problem?id=1651 题意:一系列的数字,除了头尾不能动,每次取出一个数字,这个数字与左右相邻数字的乘积为其价值, 最后将所有价值加起来,要求最小值. 这题容易会想到贪心就是先把最大的数先取出这样就能满足剩下的总价值尽可能的小,如果出现多个一样 的数时优先取走价值小的,但是如果有出现多个价值一样的话就不好处理了. 于是可以考虑一下用区间解决,区间转移大致是这样的 dp[j][j + i] = min(dp[j][j + i] , dp[j][k]

POJ1651:Multiplication Puzzle(区间DP 最优矩阵链乘)

题意:除了头尾不能动,每次取出一个数字,这个数字与左右相邻数字的乘积为其价值,最后将所有价值加起来,要求最小值 和最优矩阵链乘模型一样,最后取出的数决定了序,如果没学过最优矩阵连乘找重复子问题还是比较难找的 DP //180K 0MS #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int dp[110][110];

POJ1651 Multiplication Puzzle(相邻乘积之和最小,区间DP)

http://blog.csdn.net/libin56842/article/details/9747021 http://www.cnblogs.com/devil-91/archive/2012/06/26/2562976.html #include <iostream> #include <string> #include <cstring> #include <cstdlib> #include <cstdio> #include &l

poj1651 Multiplication Puzzle

比较特别的区间dp.小的区间转移大的区间时,也要枚举断点.不过和普通的区间dp比,断点有特殊意义.表示断点是区间最后取走的点.而且一个区间表示两端都不取走时中间取走的最小花费. #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <iomanip> #include <cstr