Multiplication Puzzle ZOJ - 1602
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的更多相关文章
- ZOJ 1602 Multiplication Puzzle(区间DP)题解
题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...
- poj 1651 Multiplication Puzzle (区间dp)
题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...
- POJ1651:Multiplication Puzzle(区间DP)
Description The multiplication puzzle is played with a row of cards, each containing a single positi ...
- 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)
传送门:http://poj.org/problem?id=1651 Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K T ...
- POJ1651 Multiplication Puzzle —— DP 最优矩阵链乘 区间DP
题目链接:https://vjudge.net/problem/POJ-1651 Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65 ...
- xtu read problem training 4 B - Multiplication Puzzle
Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. O ...
- 题解【POJ1651】Multiplication Puzzle
Description The multiplication puzzle is played with a row of cards, each containing a single positi ...
随机推荐
- u-boot分析(七)----内存初始化
u-boot分析(七) 上篇博文我们按照210的启动流程,分析到了时钟初始化,今天我们继续按照u-boot的启动流程对内存的初始化进行分析. 今天我们会用到的文档: 1. 2440芯片手 ...
- 服务器raid状态监控
参考 http://hwraid.le-vert.net/wiki 查看raid芯片 lspci -v 02:00.0 SCSI storage controller: LSI Logic / Sym ...
- nginx配置优化-生产环境应用版
user www www; worker_processes auto; worker_cpu_affinity auto; error_log /usr/local/nginx/logs/error ...
- 【转载】#346 - Polymorphism
Recall that polymorphism is one of the three core principles of object-oriented programming. Polymor ...
- spring-mabatis整合的配置文件
1.spring.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...
- js 去掉指定符号的字符串做法
//去掉字符串末尾指定符号 function RemoveSymbol(str, symbol) { if (str.charAt(str.length - 1) == symbol) { str = ...
- COGS2287 [HZOI 2015]疯狂的机器人
[题目描述] 现在在二维平面内原点上有一只机器人 他每次操作可以选择向右走,向左走,向下走,向上走和不走(每次如果走只能走一格) 但是由于本蒟蒻施展的大魔法,机器人不能走到横坐标是负数或者纵坐标是负数 ...
- python web应用--WSGI接口(二)
WSGI接口定义非常简单,它只要求Web开发者实现一个函数,就可以响应HTTP请求.我们来看一个最简单的Web版本的“Hello, web!”: 1 # server.py 2 # 从wsgiref模 ...
- 2017.9.18 include指令和include动作有什么区别?
问题:include指令和include动作有什么区别? 答:include指令合并静态文档或Jsp页面中的内容,可以用于包括动态生成的输出结果,因此可以包含一个Servlet include指令在编 ...
- Maven项目导出可执行jar
配置文件中添加插件 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>m ...