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. Spring Boot+BootStrap fileInput 多图片上传

    一.依赖文件 <link rel="stylesheet" type="text/css" th:href="@{/js/bootstrap/c ...

  2. 在xshell中使用sftp上传文件

    Xshell 5 (Build 1335)Copyright (c) 2002-2017 NetSarang Computer, Inc. All rights reserved. Type `hel ...

  3. selenium+python—HTML生成报告代码

    Python自动化测试生成HTML测试报告 HTMLTestRunner是Python标准库unittest单元测试框架的一个扩展,他生成易于使用的HTML测试报告. Ubuntu放置位置:输入Pyt ...

  4. 机器学习与R语言:NB

    #---------------------------------------- # 功能描述:演示NB建模过程 # 数据集:SMS文本信息 # tm包:维也纳财经大学提供 #----------- ...

  5. Spring事务用法示例与实现原理

    关于Java中的事务,简单来说,就是为了保证数据完整性而存在的一种工具,其主要有四大特性:原子性,一致性,隔离性和持久性.对于Spring事务,其最终还是在数据库层面实现的,而Spring只是以一种比 ...

  6. 操作shell两种方式

    交互式shell package main import ( "golang.org/x/crypto/ssh" "log" "os" &q ...

  7. 20145321 《Java程序设计》第5周学习总结

    20145321 <Java程序设计>第5周学习总结 教材学习内容总结 第八章 1.Try.catch:Java中所有错误都会被打包为对象,通过try和catch语法可以对代表错误的对象做 ...

  8. char,short,int长度

    数据类型的本质就是固定内存大小的别名 char:1byte short:  2byte int:4byte 其实变量也是对连续内存的别名,相当于这段内存的句柄.钩子

  9. 08_MySQL DQL_SQL99标准中的多表查询(内连接)

    # sql99语法/*语法: select 查询列表 from 表1 别名 [连接类型] join 表2 别名 on 连接条件 [where 筛选条件] [group by 分组] [having 分 ...

  10. js实现全选checkbox

    js代码 function selectAllCheckBox(parentid) { var PID = document.getElementById(parentid); var cb = PI ...