Multiplication Puzzle
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10010   Accepted: 6188

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。。。设dp[l][r]表示区间[l,r]的最优解,则状态转移如下:
1、当r-l=2时,也即只有三个数时,显然dp[l][r] = a[l]*a[l+1]*a[r];
2、当r-l>2时,对区间的最后一个被拿走的数进行枚举,则dp[l][r] = min(dp[l][r], dp[l][i]+dp[i][r]+a[l]*a[i]*a[r]),其中l<i<r。
 
#include <iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<deque>
#include<algorithm>
#include<string>
#include<stack>
#include<cmath>
using namespace std;
int ans,n;
int a[];
int dp[][];
const int inf=0x3f3f3f3f;
int dfs(int l,int r)
{
if(r-l<) return ;
if(r-l==) return dp[l][r]=a[l]*a[l+]*a[r]; //起始值
if (dp[l][r]!=inf) return dp[l][r];
for(int i=l+;i<r;i++)
dp[l][r]=min(dp[l][r],dfs(l,i)+dfs(i,r)+a[l]*a[i]*a[r]);
return dp[l][r];
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
ans=;
memset(dp,inf,sizeof(dp));
printf("%d\n",dfs(,n));
}
return ;
}

Poj 1651 Multiplication Puzzle(区间dp)的更多相关文章

  1. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  2. POJ 1651 Multiplication Puzzle 区间dp(水

    题目链接:id=1651">点击打开链 题意: 给定一个数组,每次能够选择内部的一个数 i 消除,获得的价值就是 a[i-1] * a[i] * a[i+1] 问最小价值 思路: dp ...

  3. POJ 1651 Multiplication Puzzle(类似矩阵连乘 区间dp)

    传送门:http://poj.org/problem?id=1651 Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K T ...

  4. POJ 1651 Multiplication Puzzle (区间DP)

    Description The multiplication puzzle is played with a row of cards, each containing a single positi ...

  5. POJ 1651 Multiplication Puzzle (区间DP,经典)

    题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最 ...

  6. POJ1651:Multiplication Puzzle(区间DP)

    Description The multiplication puzzle is played with a row of cards, each containing a single positi ...

  7. poj 1651 Multiplication Puzzle【区间DP】

    题目链接:http://poj.org/problem? id=1651 题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n) 同一时候ans+=a ...

  8. poj 1651 Multiplication Puzzle

    题目链接:http://poj.org/problem?id=1651 思路:除了头尾两个数不能取之外,要求把所有的数取完,每取一个数都要花费这个数与相邻两个数乘积的代价,需要这个代价是最小的 用dp ...

  9. POJ 1651 Mulitiplication Puzzle

    The multiplication puzzle is played with a row of cards, each containing a single positive integer. ...

随机推荐

  1. [C#]浮点数除零无法捕获异常的解决办法

    解决方法: //运算前先检查被除数是否为零,为零则手动抛出除零异常 if (numberB == 0.0) { throw new DivideByZeroException(); } Result ...

  2. linux系统中python版本升级

    一,查看python版本号 python -V 二,下载需要升级到python版本包下载地址https://www.python.org/ftp/python/ 根据需要选择需要的python版本 e ...

  3. Django学习笔记之Queryset的高效使用

    对象关系映射 (ORM) 使得与SQL数据库交互更为简单,不过也被认为效率不高,比原始的SQL要慢. 要有效的使用ORM,意味着需要多少要明白它是如何查询数据库的.本文我将重点介绍如何有效使用 Dja ...

  4. 20145327 《Java程序设计》第十周学习总结

    20145327 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程就是运行在不同计算机中两个程序之间的数据交换. 网络中的每个设备都会有一个唯一的数字标识,这个就是IP地址. ...

  5. linux input输入子系统分析《四》:input子系统整体流程全面分析

    1      input输入子系统整体流程 本节分析input子系统在内核中的实现,包括输入子系统(Input Core),事件处理层(Event Handler)和设备驱动层.由于上节代码讲解了设备 ...

  6. Flutter中集成Font Awesome

    1.添加引用 在 pubspec.yaml文件中,加入 font awesome的引用 dependencies: flutter: sdk: flutter # The following adds ...

  7. JAVA基础补漏--文件读取

    public class Test2 { public static void main(String[] args) throws IOException { FileInputStream fis ...

  8. vue.js中路由传递参数

    知识点:vue路由传递参数,第二个页面(A.B页面)拿到参数,使用参数 方法一:使用 <router-link :to="{name:'edithospital',params:{hi ...

  9. LeetCode——Find Duplicate Subtrees

    Question Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, yo ...

  10. hbase(一)region

    前言 文章不含源码,只是一些官方资料的整理和个人理解 架构总览 这张图在大街小巷里都能看到,感觉是hbase架构中最详细最清晰的一张,稍微再补充几点. 1) Hlog是低版本hbase术语,现在称为W ...