Multiplication Puzzle ZOJ - 1602

传送门

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 file 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.

Process to the end of file.

Output

Output file must contain a single integer - the minimal score.


Sample Input

6
10 1 50 50 20 5

Sample Output

3650

题意:一排牌/卡片(一串数字),每次从这些牌中拿走一张牌(首尾两张不能拿),把前一张,这一张,后一张牌上的数字相乘的结果累加,直到只剩下两张牌为止。问所能得到的最小结果是多少。

    例如:5张牌是10,1,50,20,5。拿走的牌的顺序如果是50,20,1。得到的结果就是:

    1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150;

题解:区间DP

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdlib>
#include<vector>
#include<string>
#include<queue>
using namespace std; #define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f3f
const double PI = acos(-1.0);
const int maxn = 1e2+;
const int mod = 1e9+;
int a[maxn];
int dp[maxn][maxn];
int main()
{ int n;
while(~scanf("%d",&n)) {
memset(dp,INF,sizeof dp);
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
for (int i = ; i <= n; i++)
dp[i][i] = dp[i - ][i] = dp[i][i + ] = ;
for (int i = ; i <= n - ; i++)
dp[i - ][i + ] = a[i] * a[i - ] * 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++)
dp[i][j] = min(dp[i][j], dp[i][k] + a[i] * a[k] * a[j] + dp[k][j]);
}
printf("%d\n", dp[][n]);
}
}

Multiplication Puzzle ZOJ - 1602的更多相关文章

  1. ZOJ 1602 Multiplication Puzzle(区间DP)题解

    题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...

  2. poj 1651 Multiplication Puzzle (区间dp)

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

  3. POJ1651:Multiplication Puzzle(区间DP)

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

  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)

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

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

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

  7. POJ1651 Multiplication Puzzle —— DP 最优矩阵链乘 区间DP

    题目链接:https://vjudge.net/problem/POJ-1651 Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65 ...

  8. xtu read problem training 4 B - Multiplication Puzzle

    Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. O ...

  9. 题解【POJ1651】Multiplication Puzzle

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

随机推荐

  1. PHP中函数的定义与使用

    函数是什么? 函数是一个被命名的.独立的代码段,它执行特定的任务,并可能给调用它的程序返回一个值. 函数是被命名的,每个函数都有唯一的名称. 函数是独立的,无需程序其他部分干预,函数便能执行自己的任务 ...

  2. [vijos]lxhgww的奇思妙想(长链剖分)

    题意 题目链接 Sol 长链剖分 又是一个用各种花式技巧优化的暴力 它的主要思想是:对于每个节点,把深度最深的子节点当做重儿子,它们之间的边当做重边 这样就会有一些非常好的轻质 所有链长总和是\(O( ...

  3. flexbox预习

    创建一个flexbox: .flex-container{ display:flex; } flex-direction:  column;//将flex排成一列 flex-direction: co ...

  4. POS开发问题 - 输入非数字弹出提示框的实现

    业务场景: 一个输入框,如果输入非数字,那么弹出提示框,如下图 点击确定,输入框自动清空非数字的输入,并且自动获得焦点,如图: 实现方案: 实现的想法: 给输入框添加一个 input 事件,给输入框绑 ...

  5. laravel的nginx配置

    最近阅读laravel官方文档 发现了关于nginx的推荐配置 Nginx 如果你使用 Nginx ,在你的网站配置中加入下述代码将会转发所有的请求到 index.php 前端控制器. locatio ...

  6. Ajax使用 初始化数据 + mvc

    2017-3-16 mvc+jquery+easyUI做项目 <input type="text" id="txtSTQty" name="tx ...

  7. 01、Spark安装与配置

    01.Spark安装与配置 1.hadoop回顾 Hadoop是分布式计算引擎,含有四大模块,common.hdfs.mapreduce和yarn. 2.并发和并行 并发通常指针对单个节点的应对多个请 ...

  8. 快速提取邮箱地址(利用word或网站)

    在word中,CTRL+F,输入:[A-z,0-9]{1,}\@[A-z,0-9,\.]{1,} 点击“高级”,勾选“使用通配符”,点击“查找全部”: 复制.粘贴. 还可通过以下页面在线提取. htt ...

  9. replace into 浅析

    转载自:http://blog.itpub.net/22664653/viewspace-1669734/ http://blog.itpub.net/22664653/viewspace-16701 ...

  10. 将TIMESTAMP类型的差值转化为秒的方法

    两个TIMESTAMP之差得到的是INTERVAL类型,而有时我们只需要得到两个时间相差的秒数,如果变成INTERVAL之后,想要获取这个值会非常麻烦. 比较常见的方法是使用EXTRACT来抽取获得的 ...