Poj-1651-multiplication puzzle

Source: Internet
Author: User

Start with the question:

Multiplication puzzle
Time limit:1000 ms   Memory limit:65536 K
Total submissions:6162   Accepted:3758

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 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 wocould take the cards in the opposite order, I. e. 50, then 20, then 1, the score wocould 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

610 1 50 50 20 5

Sample output

3650

Question: Give a string of numbers, and then give you an operation to select the numbers of the two non-edge numbers, then we get the score that is the product of this number and the two left and right numbers, and then the selected number disappears, asking when the total score is the maximum when there are only two left.
This is the interval DP. We can enumerate a certain number and then find the maximum score if the number disappears.
State transition equation: DP [I] [J] [k] = max (DP [I] [J] [K], (I + 1 ~ Maximum Value of J-1) + (J + 1 ~ K-1 value) + I * j * K)
It is convenient to use DFS to calculate the value, and the recursive depth is not long, so there is no need to worry about stack explosion or time issues.
Another reason for implementing recursive methods is that I usually prefer to use recursive methods when writing DP, but it is not enough to master only one implementation method, after all, different implementations may have different effects in different situations.

Code:

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define MAX 102 6 #define LL long long 7 using namespace std; 8  9 const int limit = 100000002;10 int p[MAX];11 int dp[MAX][MAX][MAX];12 int n;13 14 int dfs(int a,int b,int c){15     int minn=limit;16     if(dp[a][b][c]!=-1) return dp[a][b][c];17     dp[a][b][c]=p[a]*p[b]*p[c];18     for(int i=a+1;i<b;i++){19         dfs(a,i,b);20         minn = min(minn,dp[a][i][b]);21     }22     if(a+1<b) dp[a][b][c]+=minn;23     minn = limit;24     for(int i=b+1;i<c;i++){25         dfs(b,i,c);26         minn = min(minn,dp[b][i][c]);27     }28     if(b+1<c) dp[a][b][c]+=minn;29     return dp[a][b][c];30 }31 32 int main()33 {34     int sum;35     //freopen("data.txt","r",stdin);36     while(~scanf("%d",&n)){37         for(int i=1;i<=n;i++) scanf("%d",&p[i]);38         memset(dp,-1,sizeof(dp));39         sum=limit;40         for(int i=2;i<n;i++){41             dfs(1,i,n);42             sum = min(sum,dp[1][i][n]);43         }44         printf("%d\n",sum);45     }46     return 0;47 }
1651

 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.