POJ 1651:Multiplication Puzzle

区间DP基础题

只贴代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define maxn 0xfffffff

int main()
{
        int n,i,j,k,l;
        int num[1111],dp[1111][1111];
        scanf("%d",&n);

                for(i=1;i<=n;i++)
                        scanf("%d",&num[i]);
                //memset(dp,maxn,sizeof(dp));
                memset(dp,0,sizeof(dp));
                for(i=2;i<=n-1;i++)
                        dp[i][i]=num[i-1]*num[i]*num[i+1];
                for(l=1;l<n-2;l++)
                        for(i=2;i<n-1;i++)
                               {
                                 j=i+l;
                                dp[i][j]=maxn;

                                for(k=i;k<=j;k++)
                                        {
                                                dp[i][j]=min(dp[i][j],dp[i][k-1]+dp[k+1][j]+num[i-1]*num[k]*num[j+1]);
                                                //printf("%d %d %d\n",i,j,dp[i][j]);
                                        }
                                }

                printf("%d\n",dp[2][n-1]);

        return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 11:05:24

POJ 1651:Multiplication Puzzle的相关文章

POJ 1651:Multiplication Puzzle 矩阵相乘式DP

Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7118   Accepted: 4385 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one

POJ 区间DP Multiplication Puzzle

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 numb

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]) (

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

poj 1651 区间dp

题目链接:http://poj.org/problem?id=1651 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 prod

poj 1651 http://poj.org/problem?id=1651

http://poj.org/problem?id=1651Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6188   Accepted: 3777 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. Dur

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

[求PN点] poj 2505 A multiplication game

题目链接: http://poj.org/problem?id=2505 A multiplication game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5098   Accepted: 2573 Description Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers

Mark一下, dp状态转移方程写对,但是写代码都错,poj 1651 poj 1179

dp题: 1.写状态转移方程; 2.考虑初始化边界,有意义的赋定值,还没计算的赋边界值: 3.怎么写代码自底向上计算最优值 今天做了几个基础dp,全部是dp方程写对但是初始化以及计算写错 先是poj 1651 其实就是个赤裸裸的矩阵连乘,dp方程很容易写出 dp[i][j]=min(dp[i][k]+dp[k+1][j]+r[i]*c[k]*c[j],dp[i][j]); 先贴两个个二逼的代码,mark下自己多么的二逼: 二逼一:在计算的时候使用了还没有算出来的值,模拟下就知道第一重循环里算dp