POJ 1651 Multiplication PuzzleDP方法:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const LL INF = 0xfffffff;
const LL maxn = ;
int dp[maxn][maxn], a[maxn]; int DFS(int L,int R)
{
if(dp[L][R])
return dp[L][R];
if(R-L <= )
return ;
dp[L][R] = INF;
for(int k=L+; k < R; k++)
dp[L][R] = min(dp[L][R], DFS(L,k)+DFS(k,R) + a[L]*a[k]*a[R]); return dp[L][R];
} int main()
{
int n;
while(cin >> n)
{
memset(dp, , sizeof(dp));
for(int i=; i<= n; i++)
cin >> a[i]; printf("%d\n", DFS(,n));
}
return ;
}
/*
6
10 1 50 50 20 5
**/
===================================================================================================
DP方法:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const LL INF = 0xfffffff;
const LL maxn = ;
int dp[maxn][maxn], a[maxn]; int main()
{
int n;
while(cin >> n)
{
memset(dp, , sizeof(dp));
for(int i=; i<= n; i++)
cin >> a[i]; for(int len=; len <=n; len ++)
{
for(int i=; i+len<=n; i++)
{
int j = i+len;
for(int k=i+; k<j; k++)
{
if(dp[i][j] == )
dp[i][j] = dp[i][k] + dp[k][j] + a[i]*a[j]*a[k];
else
dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j] + a[i]*a[j]*a[k]);
}
}
}
cout << dp[][n] << endl;
}
return ;
}
/*
6
10 1 50 50 20 5
**/
POJ 1651 Multiplication PuzzleDP方法:的更多相关文章
- POJ 1651 Multiplication Puzzle(类似矩阵连乘 区间dp)
传送门:http://poj.org/problem?id=1651 Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K T ...
- poj 1651 Multiplication Puzzle (区间dp)
题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...
- poj 1651 Multiplication Puzzle
题目链接:http://poj.org/problem?id=1651 思路:除了头尾两个数不能取之外,要求把所有的数取完,每取一个数都要花费这个数与相邻两个数乘积的代价,需要这个代价是最小的 用dp ...
- poj 1651 Multiplication Puzzle【区间DP】
题目链接:http://poj.org/problem? id=1651 题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n) 同一时候ans+=a ...
- POJ 1651 Multiplication Puzzle (区间DP)
Description The multiplication puzzle is played with a row of cards, each containing a single positi ...
- Poj 1651 Multiplication Puzzle(区间dp)
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10010 Accepted: ...
- POJ 1651 Multiplication Puzzle 区间dp(水
题目链接:id=1651">点击打开链 题意: 给定一个数组,每次能够选择内部的一个数 i 消除,获得的价值就是 a[i-1] * a[i] * a[i+1] 问最小价值 思路: dp ...
- POJ 1651 Multiplication Puzzle (区间DP,经典)
题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最 ...
- 区间dp E - Multiplication Puzzle POJ - 1651
E - Multiplication Puzzle POJ - 1651 这个题目没有特别简单,但是也没有我想象之中的那么难,这个题目时区间dp,因为我们是要对区间进行考虑的. 但是呢,这个也和动态 ...
随机推荐
- 10.30 afternoon
P76竞赛时间: ????年??月??日??:??-??:?? 题目名称 他 她 它 名称 he she it 输入 he.in she.in it.in 输出 he.out she.out it.o ...
- 安装zookeeper时候,可以查看进程启动,但是状态显示报错:Error contacting service. It is probably not running
安装zookeeper-3.3.2的时候,启动正常没报错,但zkServer.sh status查看状态的时候却出现错误,如下: JMX enabled by defaultUsing config: ...
- js获取图片高度
js获取图片高度时经常会获取的图片高度为0,原因是图片未加载完毕.第一次加载时,显示0(火狐等部分浏览器显示24).待加载完毕后,再刷新,显示图片高度258. var oImg = document. ...
- C++ 简单的入门语法
入门的hello world using namespace std; 是使用命名空间,有点像java里面的引入包main 方法和java一样是主入口,有且只有一个,因为是int ,所以还必须返回一个 ...
- CSS3条件判断——@supports/window.CSS.supports()(转)
CSS3条件判断,听起来"不明觉厉",如果你对CSS稍为熟悉一点的话,你会发现CSS中的"@media"就是条件判断之一.是的,在CSS3的条件判断规范文档中包 ...
- C#.net时间戳转换
//long ticks = (DateTime.Parse(DateTime.Now.ToString(CultureInfo.InvariantCulture)).ToUniversalTime( ...
- Problem 1183 - 排列
#include<iostream> #include<vector> #include<algorithm> using namespace std; int c ...
- visualSVN server库迁移(转)
转自:http://blog.csdn.net/yuhuijun_1/article/details/9762683 首先,VisualSVN Server Manager,包含两个路径,一个是安装路 ...
- C#应用程序获取项目路径的方法总结
一.非Web程序 //基目录,由程序集冲突解决程序用来探测程序集 1.AppDomain.CurrentDomain.BaseDirectory //当前工作目录的完全限定路径2.Envi ...
- ECSHOP在商品详细页面上获取该商品的顶级分类id和名称
在 goods.php 文件, 找到 $smarty->assign('goods', $goods); 在它上面增加下面代码: 方法一: $cat_arr = get_parent_cats( ...