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 ...
随机推荐
- 软件测试技术第三次作业——打印质数printPrimes()
作业一.Use the following method printPrimes() for questions a–d. printPrimes: /** ********************* ...
- XHML教会我的一些东西-3
在寒假期间,隔几天就同学聚会,每天都是起床困难户.每天都想着要完成任务,要学习新的东西.但是总是被自己惰性占为上风.感觉自己很没用,但是又继续堕落.真的不能理解自己.呵呵.... 在放假一段时间之后, ...
- vue中 eCharts 自适应容器
在 vue 脚手架开发中,echarts图表自适应容器的方法: 父组件: <template> <div class="statistics_wrap"> ...
- 关于Android Studio中的一个小问题——R文件引用Id失败
错误情况: 今天使用AS建立了一个新的EmptyProject,结果出现错误 setContentView(R.layout.activity_main); R文件的引用Id失败.真的是莫名奇妙... ...
- Cookie和Session 简单介绍
cookie : 1.cookie是存在客户端(浏览器)的进程内存中和客户端所在的机器硬盘上 2.cookie只能能够存储少量文本,大概4K大小 3.cookie是不能在不同浏 ...
- win10下各种问题的解决办法
本来申请这个博客是为了写一些Java学习笔记的,但是鉴于我半年内无数次重装系统的惨痛经历,所以把win10系统的一些问题总结一下. 此账号密码:1994llz. 1.win10取消开机密码: http ...
- 显示C++ vector中的数据
C++ 中的vector是一个容器数据类型,不能使用cout直接显示容器中的值. 以下程序中,myvector 是一个vector数据类型.将myvector替换为需要输出的vector. for(i ...
- 数据库连接-ADO.NET
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/huo065000/article/details/25830291 非常早就知道了ADO ...
- 剑指offer39 平衡二叉树
剑指上用了指针传递,这里用的引用传递 class Solution { public: bool IsBalanced_Solution(TreeNode* pRoot) { ; return IsB ...
- MapReduce执行jar练习
1.用程序生成输入文件1.txt和2.txt 生成程序源码如下: https://www.cnblogs.com/jonban/p/10555364.html 2. 上传文件到hdfs文件系统 创建 ...