poj1651 Multiplication Puzzle

比较特别的区间dp。小的区间转移大的区间时,也要枚举断点。不过和普通的区间dp比,断点有特殊意义。表示断点是区间最后取走的点。而且一个区间表示两端都不取走时中间取走的最小花费。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
using namespace std;
const double EPS=1e-8;
const int SZ=220,INF=0x7FFFFFFF;
typedef long long lon;
int n,arr[220],dp[220][220];

void init()
{
    cin>>n;
    for(int i=1;i<=n;++i)cin>>arr[i];
    for(int i=1;i+2<=n;++i)dp[i][i+2]=arr[i]*arr[i+1]*arr[i+2];
    for(int len=3+1;len<=n;++len)
    {
        for(int i=1;i<=n;++i)
        {
            int r=i+len-1;
            dp[i][r]=INF;
            for(int j=i+1;j+1<=r;++j)
            {
                dp[i][r]=min(dp[i][r],dp[i][j]+dp[j][r]+arr[j]*arr[i]*arr[r]);
            }
        }
    }
    cout<<dp[1][n]<<endl;
}

int main()
{
    std::ios::sync_with_stdio(0);
    //freopen("d:\\1.txt","r",stdin);
    //lon casenum;
    //cin>>casenum;
    //for(lon time=1;time<=casenum;++time)
    //for(lon time=1;;++time)
    {
        init();
        //work();
    }
    return 0;
}

原文地址:https://www.cnblogs.com/gaudar/p/9733291.html

时间: 2024-10-05 03:10:01

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

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(简单区间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

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

POJ 1651 Multiplication Puzzle(区间dp)

Language: Default Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6693   Accepted: 4083 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move

xtu read problem training 4 B - Multiplication Puzzle

Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 165164-bit integer IO format: %lld      Java class name: Main The multiplication puzzle is played with a row of cards, each containing a s

POJ 1651 Multiplication Puzzle (区间dp)

题目大意:对n个数组成的序列取数,规定最两边不能取,每次取一个a[i],得到 a[l] * a[i] * a[r] 的分数(a[l]是a[i]左边的数,a[r]是a[i]右边的数),并把这个数从序列中移走,求n-2次取数后的得分和的最小值 分析:正着确定状态不好做,不如反着来,设dp[l][r]为向区间[l, r]中填满数所得到分数和的最小值,考虑最近一次填数的位置,不难得出: dp[l][r] = fmin(dp[l][m] + dp[m][r] + a[l] * a[m] * a[r]) (