有N张写有数字的卡片排成一行,按一定次序从中拿走N-2张(第1张和最后一张不能拿),每次只拿一张,取走一张卡片的同时,会得到一个分数,分值的计算方法是:要拿的卡片,和它左右两边的卡片,这三张卡片上数字的乘积。按不同的顺序取走N-2张卡片,得到的总分可能不相同,求出给定一组卡片按上述规则拿取的最小得分。
 
DP式子:
dp[L][R] = min(dp[L][R], dp[L][k]+dp[k][R] + a[L]*a[k]*a[R]);
两种方法:
 
 
================================================================================
记忆化搜索:

#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方法:的更多相关文章

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

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

  2. poj 1651 Multiplication Puzzle (区间dp)

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

  3. poj 1651 Multiplication Puzzle

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

  4. poj 1651 Multiplication Puzzle【区间DP】

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

  5. POJ 1651 Multiplication Puzzle (区间DP)

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

  6. Poj 1651 Multiplication Puzzle(区间dp)

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

  7. POJ 1651 Multiplication Puzzle 区间dp(水

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

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

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

  9. 区间dp E - Multiplication Puzzle POJ - 1651

    E - Multiplication Puzzle  POJ - 1651 这个题目没有特别简单,但是也没有我想象之中的那么难,这个题目时区间dp,因为我们是要对区间进行考虑的. 但是呢,这个也和动态 ...

随机推荐

  1. DIV------使用 <div> 元素的网页布局

    <!DOCTYPE html> <html> <head> <style type="text/css"> div#containe ...

  2. C++ hello world

    日文版本的vs 2008 , 在 < 新建 里面先创建一个项目 然后点击项目去创建一个C++的主启动文件 选择创建的文件类型 然后在文件里面写入代码 #include<iostream&g ...

  3. Axure RP 8.0正式版下载地址 安装和汉化说明

    1.Axure RP和中文包包下载地址 官网地址:http://www.axure.com.cn/3510/ 2.下载完成后安装 3.破解 axure8.0注册码激活码:(亲测可用)用户名:aaa注册 ...

  4. DataTable和List集合互转

    /// <summary> /// 将集合转换成DataTable /// </summary> /// <param name="list"> ...

  5. SQL server抽疯后修改sa密码无法成功的处理办法

    今天上班打开电脑,发现尼玛所有项目启动后都报错,原因是说数据库sa的验证错误,无法进行数据库链接等等东西,简单地说---SQL server抽疯了!!!:( 昨天还好好的.而且没有修改过东西.为啥会出 ...

  6. one way WebService

    WSDL支持4种消息交换方式:   1)单向(One-way):服务端接收消息:   2)请求响应(Request-response):服务端点接收请求消息,然后发送响应消息:   3)要求应答(So ...

  7. Jmeter软件测试1--webservice测试

    写在前言 程序猿一枚,原本就是负责安安静静的撸代码,后来公司让兼任下测试的工作,还得照顾下面的几个测试兄弟,无奈本人毫无软件测试理论知识,下面的测试兄弟也是初级水平,又面临公司要求做webservic ...

  8. 找出整数中第k大的数

    一  问题描述: 找出 m 个整数中第 k(0<k<m+1)大的整数. 二  举例: 假设有 12 个整数:data[1, 4, -1, -4, 9, 8, 0, 3, -8, 11, 2 ...

  9. linux 获取cpu百分比

    vmstat 1 |head -n 4 |tail -n 1 |awk '{print $13}'

  10. splice从数组中删除指定定数据

    /*从数组中删除指定定数据var somearray = ["mon", "tue", "wed", "thur"]so ...