http://poj.org/problem?id=1651Multiplication Puzzle
 
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6188   Accepted: 3777

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
题意:

从中间选择一个数乘以左右俩边的数,第一个和最后一个不能选择,这个数就拿出来,怎样拿出来的顺序,使总和最小。

分析:abc sum=a*b*c;

abcd sum1=abc+acd;

sum2=bcd+abd;

sum=min(sum1,sum2)

dp[i][j]=min(dp[i][j],a[i]*a[g]*a[j]+dp[i][g]+dp[g][j]);(i<g<j)

也就是说igj,向该数列插入数字,是i和g之间或者是g和i之间。
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int dp[][];
int main()
{
int i,j,k,n,a[],g;
while(~scanf("%d",&n))
{
memset(dp,,sizeof(dp));
for(i=;i<n;i++)
scanf("%d",&a[i]);
for(k=;k<n;k++)//保证最小有3位数。
{
for(i=;i<n-k;i++)
{
j=i+k;
dp[i][j]=;//求最小,初始化最大。
for(g=i+;g<j;g++)
dp[i][j]=min(dp[i][j],a[i]*a[g]*a[j]+dp[i][g]+dp[g][j]);
}
} printf("%d\n",dp[][n-]);
}
return ;
}
/*
6
10 1 50 50 20 5
3650
*/
												

poj 1651 http://poj.org/problem?id=1651的更多相关文章

  1. poj-3056 http://poj.org/problem?id=3056

    http://poj.org/problem?id=3056 The Bavarian Beer Party Time Limit: 6000MS   Memory Limit: 65536K Tot ...

  2. poj 1679 http://poj.org/problem?id=1679

    http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submis ...

  3. POJ3278http://poj.org/problem?id=3278

    http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include&l ...

  4. OpenJudge/Poj 1207 The 3n + 1 problem

    1.链接地址: http://bailian.openjudge.cn/practice/1207/ http://poj.org/problem?id=1207 2.题目: 总时间限制: 1000m ...

  5. POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)

    http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...

  6. poj 1681 Painter&#39;s Problem(高斯消元)

    id=1681">http://poj.org/problem? id=1681 求最少经过的步数使得输入的矩阵全变为y. 思路:高斯消元求出自由变元.然后枚举自由变元,求出最优值. ...

  7. POJ 3100 Root of the Problem || 1004 Financial Management 洪水!!!

    水两发去建模,晚饭吃跟没吃似的,吃完没感觉啊. ---------------------------分割线"水过....."--------------------------- ...

  8. <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针

    地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展  若满足要求则从左缩减区域 代码如下  正确性调整了几次 然后 ...

  9. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...

随机推荐

  1. CTSC2016&&APIO2016游记

    4.30 下午衡中放假,我们因为比赛的缘故提前到中午12:00放假 然后我爸爸说要来接我,直到下午两点多他才到,然后衡中宿舍的楼管阿姨死活不给我开门 莫名其妙的等到了三点多快四点的时候我才跟实验班的一 ...

  2. ubuntu 修改主机及主机名

    修改主机: sudo vim /etc/hostname sudo vim /etc/hosts 修改用户名: sudo vim /etc/passwd sudo mv /home/yinggc /h ...

  3. JAVA! static什么作用?

    是静态修饰符,什么叫静态修饰符呢?大家都知道,在程序中任何变量或者代码都是在编译时由系统自动分配内存来存储的,而所谓静态就是指在编译后所分配的内存会一直存在,直到程序退出内存才会释放这个空间,也就是只 ...

  4. PHP裁剪图片

    PHP裁剪图片 $src_path = '1.jpg'; //创建源图的实例 $src = imagecreatefromstring(file_get_contents($src_path)); / ...

  5. NSArray 利用数组创建数组

    NSArray *array=[NSArray arrayWithObjects:@"1",@"2",@"3", nil];         ...

  6. swift:类型转换(is用作判断检测、as用作类型向下转换)

    类型转换是一种检查类实例的方式,并且哦或者也是让实例作为它的父类或者子类的一种方式.   类型转换在Swift中使用is 和 as操作符实现.这两个操作符提供了一种简单达意的方式去检查值的类型或者转换 ...

  7. hadoop拾遗(二)---- 文件模式

    在单个操作中处理一批文件,这是一个常见的要求.举例来说,处理日志的MapReduce作业可能需要分析一个月内包含在大量目录中的日志文件.在一个表达式中使用通配符来匹配多个文件是比较方便的,无需列举第个 ...

  8. Linux改IP后务必重启网络服务

    [root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0BOOTPROTO=noneONBOOT=yes ...

  9. R: count number of distinct values in a vector

    numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435,         453,435,324,34,456,56,567,65,34,435) a & ...

  10. dom4j修改,获取,增加xml中某个元素的属性值

    XML文件: <?xml version="1.0" encoding="UTF-8"?> <vrvscript> <item I ...