POJ1651:Multiplication Puzzle(区间dp)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9419 | Accepted: 5850 |
Description
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
Output
Sample Input
6
10 1 50 50 20 5
Sample Output
3650
# include <stdio.h>
# include <string.h>
int min(int a, int b)
{
return a<b?a:b;
}
int main()
{
int n, len, i, k, imin1, imin2, imin, a[102],dp[102][102];
while(scanf("%d",&n) != EOF)
{
memset(dp,0 ,sizeof(dp));
for(i=0; i<n; ++i)
scanf("%d",&a[i]);
for(i=1; i<n-1; ++i)
dp[i][i] = a[i]*a[i-1]*a[i+1];//初始化
for(len=1; len<n; ++len)//枚举区间
for(i=1; i+len<n-1; ++i)
{
//枚举最后消去的点
imin1 = dp[i+1][i+len]+a[i]*a[i-1]*a[i+len+1];//(为首点)
imin2 = dp[i][i+len-1]+a[i+len]*a[i-1]*a[i+len+1];//(为尾点)
imin = min(imin1, imin2);
for(k=i+1; k<i+len; ++k)//(为中间点)
imin = min(imin, dp[i][k-1]+dp[k+1][i+len]+a[k]*a[i-1]*a[i+len+1]);
dp[i][i+len] = imin;
}
printf("%d\n",dp[1][n-2]);
}
return 0;
}
转载于:https://www.cnblogs.com/junior19/p/6730110.html
POJ1651:Multiplication Puzzle(区间dp)的更多相关文章
- 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)
题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...
- POJ 1651 Multiplication Puzzle 区间dp(水
题目链接:id=1651">点击打开链 题意: 给定一个数组,每次能够选择内部的一个数 i 消除,获得的价值就是 a[i-1] * a[i] * a[i+1] 问最小价值 思路: dp ...
- POJ1651 Multiplication Puzzle —— DP 最优矩阵链乘 区间DP
题目链接:https://vjudge.net/problem/POJ-1651 Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65 ...
- [ZOJ]3541 Last Puzzle (区间DP)
ZOJ 3541 题目大意:有n个按钮,第i个按钮在按下ti 时间后回自动弹起,每个开关的位置是di,问什么策略按开关可以使所有的开关同时处于按下状态 Description There is one ...
- POJ1651Multiplication Puzzle[区间DP]
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8737 Accepted: ...
- poj1651 Multiplication Puzzle
比较特别的区间dp.小的区间转移大的区间时,也要枚举断点.不过和普通的区间dp比,断点有特殊意义.表示断点是区间最后取走的点.而且一个区间表示两端都不取走时中间取走的最小花费. #include &l ...
- POJ1651 Multiplication Puzzle【区间DP】
LINK 每次删除一个数,代价是左右两边相邻的数的当前数的积 第一个和最后一个数不能删除 问最后只剩下第一个数的最后一个数的最小代价 思路 很简单的DP 正着考虑没有办法确定两边的数 那么就把每个区间 ...
- poj1651 Multiplication Puzzle(简单区间dp)
题目链接:http://poj.org/problem?id=1651 题意:一系列的数字,除了头尾不能动,每次取出一个数字,这个数字与左右相邻数字的乘积为其价值, 最后将所有价值加起来,要求最小值. ...
随机推荐
- MySQL 在Docker下快速安装(Ubuntu 16.4)
采用dockerhub安装 docker run -p 3306:3306 --name mysql -v /mydata/mysql/log:/var/log/mysql -v /mydata/my ...
- MySql 存储过程分页。
use address;drop procedure if exists `proc_s_area_code`;delimiter // #告诉mysql解释器,该段命令是否已经结束了,mysql是否 ...
- spring-cloud feign的多参数传递方案
查看原文 一.GET请求多参数URL 1.方法一(推荐) @FeignClient(“microservice-provider-user”) public interface UserFeignCl ...
- String 对象-->toUpperCase() 方法
1.定义和用法 将字符串中所有的小写字符转换成大写字符,大写字符保持不变 返回转换后的结果字符串 语法: string.toUpperCase() 注意:不会改变字符串本身,仅以返回值的形式返回结果 ...
- C语言实现双向链表
目前我们所学到的链表,无论是动态链表还是静态链表,表中各节点中都只包含一个指针(游标),且都统一指向直接后继节点,通常称这类链表为单向链表(或单链表). 虽然使用单链表能 100% 解决逻辑关系为 & ...
- 接口 ThreadMXBean 一个很好用的线程管理接口类 可以参考 jdk 帮助文档
概述 软件包 类 使用 树 已过时 索引 帮助 JavaTM Platform Standard Ed. 6 上一个类 下一个类 框架 无框架 所有类 摘要: 嵌套 ...
- 【python实现卷积神经网络】上采样层upSampling2D实现
代码来源:https://github.com/eriklindernoren/ML-From-Scratch 卷积神经网络中卷积层Conv2D(带stride.padding)的具体实现:https ...
- Github star 1.7k 的项目源码解析
先拜读源码,最后总结,以及其他实现思路.如有错误,欢迎指正! 项目介绍 名称:Darkmode.js 功能:给你的网站添加暗色模式 项目链接:https://github.com/sandoche/D ...
- 1年左右的Java开发经验面试者的心得
面试,相信只要踏入这行业的人都会经历,不同的公司有不同的面试流程,但是综合起来,其实还是大体一致的!只有不断的总结自己的面试经历,得出自己的技术不足点,才能更好的去查缺补漏,从而更加自信的进行面试找到 ...
- VulnHub靶场学习_HA: Avengers Arsenal
HA: Avengers Arsenal Vulnhub靶场 下载地址:https://www.vulnhub.com/entry/ha-avengers-arsenal,369/ 背景: 复仇者联盟 ...