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,因为我们是要对区间进行考虑的. 但是呢,这个也和动态 ...
随机推荐
- eclipse删除空行
1.打开源码编辑器 2.使用快捷键Ctrl+f 3.在Find输入框中输入:^\s*\n 4.Replace With输入框的值为空 5.在[Options]选中的"Regular expr ...
- Mac OS命令行运行Sublime Text
Opening Sublime Text on command line as subl on Mac OS? Mac OS subl http://www.phodal.com/blog/mac-o ...
- Bash算术计算
1:$(( )) 2:$[ ] 3:`expr $x + $y` 4:bc命令 #!/bin/bash x= y= echo $(( x + y )) echo $[ $x + $y ] echo ` ...
- python----------进程、线程、协程
进程与线程 什么是进程(process)? An executing instance of a program is called a process. Each process provides ...
- 【转】IOS 30多个iOS常用动画,带详细注释
原文: http://blog.csdn.net/zhibudefeng/article/details/8691567 CoreAnimationEffect.h 文件 // CoreAnimati ...
- iOS socket编程
// // ViewController.m // socket // // Created by emerys on 16/3/2. // Copyright © 2016年 Emerys. All ...
- ios strong weak 的区别 与 理解
先一句话总结:strong类保持他们拥有对象的活着,weak类他们拥有的对象被人家一牵就牵走,被人家一干就干死.(strong是一个好大哥所以strong,呵呵,weak是一个虚大哥所以weak,呵呵 ...
- C#操作求出SQL中某一字段所有行的和方法!
DataTable table = xx.sqlcha(sql1);//调数据库 ; foreach(DataRow row in table.Rows)//遍历所查出记录所有行 { v = v + ...
- PowerDesigner 的7种建模文件
1. 概念数据模型 (CDM) 对数据和信息进行建模,利用实体-关系图(E-R图)的形式组织数据,检验数据设计的有效性和合理性. 2. 逻辑数据模型 (LDM) PowerDesigner 15 ...
- 关掉PUTTY后,进程仍可以运行。
如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令.该命令可以在你退出帐户之后继续运行相应的进程.no hup就是不挂起的意思( no hang up).该命令 ...