POJ 1651 Language: Multiplication Puzzle
来源:互联网 发布:网络十大热词 英文 编辑:程序博客网 时间:2024/06/16 09:14
Multiplication Puzzle
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10949 Accepted: 6844
Description
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.
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
If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be
Input
The first line of the input 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.
Output
Output must contain a single integer - the minimal score.
Sample Input
610 1 50 50 20 5
Sample Output
3650
Source
Northeastern Europe 2001, Far-Eastern Subregion
矩阵连乘区间dp问题.
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int INF = 0x3ffffff;#define maxn 105int n;int dp[maxn][maxn];//存储子问题的最优解int p[maxn];void init(){ memset(dp,0x3f,sizeof(dp)); for(int i=1;i<=n;i++) dp[i][i]=0;}void matrix_cal(){ for(int r=2;r<n;r++){ for(int i=1;i<=n-r+1;i++){ int j=r+i-1; dp[i][j]=dp[i][i]+dp[i+1][j]+p[i-1]*p[i]*p[j]; for(int k=i+1;k<j;k++){ int temp = dp[i][k]+dp[k+1][j]+p[i-1]*p[k]*p[j]; dp[i][j]=min(dp[i][j],temp); } } }}int main(){ while(~scanf("%d",&n)){ for(int i=1;i<=n;i++){ scanf("%d",&p[i]); } init(); matrix_cal(); printf("%d\n",dp[2][n]); } return 0;}
阅读全文
0 0
- POJ 1651 Language: Multiplication Puzzle
- POJ 1651 Multiplication Puzzle
- poj 1651 Multiplication Puzzle
- POJ 1651 Multiplication Puzzle
- poj 1651 Multiplication Puzzle
- POJ 1651 Multiplication Puzzle
- Poj 1651 Multiplication Puzzle
- POJ 1651 Multiplication Puzzle
- POJ 1651 Multiplication Puzzle
- Multiplication Puzzle POJ 1651
- poj 1651 Multiplication Puzzle
- POJ 1651:Multiplication Puzzle
- Poj 1651 Multiplication Puzzle
- POJ 1651 Multiplication Puzzle
- POJ 1651 Multiplication Puzzle
- POJ 1651 Multiplication Puzzle
- poj 1651 Multiplication Puzzle
- POJ - 1651 Multiplication Puzzle
- 完美数(c/python略)
- 凸优化-对偶问题
- Swift 实践篇之链式 UI 代码
- 求n个数的平均数
- Android 判断手机是否root
- POJ 1651 Language: Multiplication Puzzle
- 基于Dragonboard 410c的智能小车(四)
- Mysql按分组获取前几条内容
- Android | 日志记录的级别与方法
- 【iOS】Class对构造简洁代码很有帮助
- 第三章运算符、表达式和语句
- 有人问我:程序员要供祖师爷的话该供谁?
- 区块链?人工智能?2018 年你应该了解的十大技术趋势
- 2017年11月编程语言排行榜:Java持续下滑仍稳居第1,Python逆袭C#上升到第4