POJ1651(KB-E)
2024-08-21 02:46:03
Multiplication Puzzle
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10034 Accepted: 6200
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.
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
6
10 1 50 50 20 5
Sample Output
3650
Source
Northeastern Europe 2001, Far-Eastern Subregion
//2017-05-23
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int inf = 0x3f3f3f3f;
int arr[], dp[][];//dp[i][j]表示区间i到j的最小价值 int main()
{
int n;
while(scanf("%d", &n)==){
for(int i = ; i < n; i++)scanf("%d", &arr[i]);
for(int i = ; i+ < n; i++)dp[i][i+] = arr[i]*arr[i+]*arr[i+];
for(int len = ; len < n; len++){
for(int i = ; i+len < n; i++){
int j = i+len;
dp[i][j] = inf;
for(int k = i+; k < j; k++){
dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]+arr[i]*arr[j]*arr[k]);
}
}
}
printf("%d\n", dp[][n-]);
} return ;
}
最新文章
- spfa模板
- 使用 AdaBoost 元算法提高分类器性能
- 补充$.extend()
- 【20160924】GOCVHelper MFC增强算法(5)
- jquery easyui easyui-treegrid 使用异步加载数据
- cf D. On Sum of Fractions
- VMware网络模式介绍(下篇)
- ExtJs 4 的filefield上传后 返回值success接受不正常
- service structure flowchart [mobile to server via HTTP RESTful API and TCP/IP in a map]
- AOP中的ASPECTJ
- 比NPOI更好用的Excel操作库——EPPlus
- java 在实例化异常的时候做的事情
- BrupSuite渗透测试笔记(九)
- APP如何进行通信的
- 关于JSON call 的一个小问题
- 说说secondarynamenode作用和配置
- JDK8 特性详解
- Notification详解(含工具类)
- vi 基础配置
- python程序练习题集
热门文章
- 文本属性和字体属性,超链接导航栏案例 background
- 试着用java实现DNS(一)——DatagramSocket, DatagramPacket, Message
- 01-Python的基础知识2
- hybird app混合开发介绍
- 解决 在 WINDOWS 下 同时安装 python2 python3 后 pip 错误
- Mac下使用Typora的一些简单操作
- python实战问题记录
- 整理学习ASP.NET MVC的资源
- 第六章-Javac符号表
- 最常用的两种C++序列化方案的使用心得(protobuf和boost serialization)