poj 1651 http://poj.org/problem?id=1651

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[105][105];
int main()
{
    int i,j,k,n,a[105],g;
    while(~scanf("%d",&n))
    {
        memset(dp,0,sizeof(dp));
       for(i=0;i<n;i++)
           scanf("%d",&a[i]);
        for(k=2;k<n;k++)//保证最小有3位数。
           {
             for(i=0;i<n-k;i++)
             {
                  j=i+k;
              dp[i][j]=100000000;//求最小,初始化最大。
             for(g=i+1;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[0][n-1]);
    }
   return 0;
}
/*
6
10 1 50 50 20 5
3650
*/

poj 1651 http://poj.org/problem?id=1651

时间: 2024-10-31 13:12:32

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

Roadblocks http://poj.org/problem?id=3255

Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too quickly, because she likes the scenery along the way. She has decided to take the second-shorte

http://poj.org/problem?id=1330

Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17818   Accepted: 9455 Description A rooted tree is a well-known data structure in computer science and engineering. An example is shown below: In the figure, each

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

http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #include<queue> #define max(a, b)(a > b ? a : b) #define N 100010 using namespace s

poj 3320 Jessica&#39;s Reading Problem(尺取法+map/hash)

题目:http://poj.org/problem?id=3320 题意:给定N个元素的数组,找出最短的一段区间使得区间里面的元素种类等于整个数组的元素种类. 分析:暴力枚举区间的起点x,然后找到最小的y,使得区间[x,y]满足条件,x向有移位后变成x',现在的y'肯定不至于在y的左边.存状态的话map和hash都可以. map代码: #include <iostream> #include <set> #include <map> #include <cstdi

poj 3320 Jessica&#39;s Reading Problem (尺取法)

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8787   Accepted: 2824 Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent littl

POJ 3320 Jessica&#39;s Reading Problem 尺取法/map

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accepted: 2369 Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent littl

POJ 3320 Jessica&#39;s Reading Problem (尺取法)

Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that

poj 3320 Jessica&#39;s Reading Problem(尺取法)

Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The au

(尺取法)POJ 3320 Jessica&#39;s Reading Problem

Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that