Multiplication Puzzle POJ - 1651

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

1.和石子归并差不多的问题。

正确的代码:
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 typedef long long ll;
 7
 8 int N;
 9 int dp[105][105],a[105];
10
11 int main()
12 {   scanf("%d",&N);
13     for(int i=1;i<=N;i++) scanf("%d",&a[i]);
14     memset(dp,0,sizeof(dp));
15     for(int i=1;i<N-1;i++) dp[i][i+2]=a[i]*a[i+1]*a[i+2];
16     for(int len=3;len<N;len++){
17         for(int i=1;i<=N&&i+len<=N;i++){
18             int j=len+i;
19             for(int k=i+1;k<j;k++){
20                 if(dp[i][j]==0) dp[i][j]=dp[i][k]+dp[k][j]+a[i]*a[k]*a[j];
21                 dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]+a[i]*a[k]*a[j]);
22             }
23         }
24     }
25     printf("%d\n",dp[1][N]);
26 }

错误的代码:调整k的时候遗漏某些情况,如1 2 3 4 5,取2之后再取4,这种情况会被漏掉!

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 typedef long long ll;
 7
 8 const int INF=1000000000;
 9
10 int N;
11 int dp[105][105],a[105];
12
13 int main()
14 {   scanf("%d",&N);
15     for(int i=1;i<=N;i++) scanf("%d",&a[i]);
16     memset(dp,0,sizeof(dp));
17     for(int i=1;i<N-1;i++) dp[i][i+2]=a[i]*a[i+1]*a[i+2];
18     for(int len=3;len<N;len++){
19         for(int i=1;i<=N&&i+len<=N;i++){
20             int j=len+i;
21             dp[i][j]=INF;
22             for(int k=i;k<j;k+=len-1){
23                 int tem;
24                 if(k==i) tem=dp[i][k]+dp[k+1][j]+a[i]*a[k+1]*a[j];
25                 else tem=dp[i][k]+dp[k+1][j]+a[i]*a[k]*a[j];
26                 if(dp[i][j]>tem) dp[i][j]=tem;
27             }
28         }
29     }
30     printf("%d\n",dp[1][N]);
31 }
时间: 2024-08-05 19:32:04

Multiplication Puzzle POJ - 1651的相关文章

DP:Multiplication Puzzle(POJ 1651)

卡片游戏 题目大意:给你一排卡片,你可以从从中抽一些卡片(但是不能抽最左和最右的卡片),每张卡片上有一个数字,当你从中抽出一张卡片后,你将得卡片的数字和卡片左右两张卡片的数字的乘积的分数,问当剩下最左和最右两张卡片的时候,你可以得到的最小的分数? 这一题一看好像挺复杂的,但是如果我们换一个思维方式,这一题就会变得很熟悉 首先这一题是显而易见的DP(找最小值,不能取极限方式) 首先他要我们抽卡片是吧,一开始我们可能会想到先抽一张看看,然后在扫一遍其他卡片看能否抽,但是这样带来的直接后果就是,我们很

POJ 1651 Multiplication Puzzle (区间dp)

题目大意:对n个数组成的序列取数,规定最两边不能取,每次取一个a[i],得到 a[l] * a[i] * a[r] 的分数(a[l]是a[i]左边的数,a[r]是a[i]右边的数),并把这个数从序列中移走,求n-2次取数后的得分和的最小值 分析:正着确定状态不好做,不如反着来,设dp[l][r]为向区间[l, r]中填满数所得到分数和的最小值,考虑最近一次填数的位置,不难得出: dp[l][r] = fmin(dp[l][m] + dp[m][r] + a[l] * a[m] * a[r]) (

POJ 1651 Multiplication Puzzle(区间dp)

Language: Default Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6693   Accepted: 4083 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move

POJ 1651:Multiplication Puzzle 矩阵相乘式DP

Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7118   Accepted: 4385 Description The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one

POJ 区间DP Multiplication Puzzle

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 numb

poj 1651 区间dp

题目链接:http://poj.org/problem?id=1651 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 prod

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. Dur

xtu read problem training 4 B - Multiplication Puzzle

Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 165164-bit integer IO format: %lld      Java class name: Main The multiplication puzzle is played with a row of cards, each containing a s

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids