• Multiplication Puzzle ZOJ


    Multiplication Puzzle ZOJ - 1602 

    传送门

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

    Process to the end of file.


    Output

    Output file must contain a single integer - the minimal score.


    Sample Input


    6
    10 1 50 50 20 5


    Sample Output

    3650

    题意:一排牌/卡片(一串数字),每次从这些牌中拿走一张牌(首尾两张不能拿),把前一张,这一张,后一张牌上的数字相乘的结果累加,直到只剩下两张牌为止。问所能得到的最小结果是多少。

        例如:5张牌是10,1,50,20,5。拿走的牌的顺序如果是50,20,1。得到的结果就是:

        1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150;

    题解:区间DP

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<sstream>
    #include<cmath>
    #include<stack>
    #include<map>
    #include<cstdlib>
    #include<vector>
    #include<string>
    #include<queue>
    using namespace std;
    
    #define ll long long
    #define llu unsigned long long
    #define INF 0x3f3f3f3f
    const double PI = acos(-1.0);
    const int maxn =  1e2+10;
    const int mod = 1e9+7;
    int a[maxn];
    int dp[maxn][maxn];
    int main()
    {
    
       int n;
       while(~scanf("%d",&n)) {
           memset(dp,INF,sizeof dp);
           for (int i = 1; i <= n; i++)
               scanf("%d", &a[i]);
           for (int i = 1; i <= n; i++)
               dp[i][i] = dp[i - 1][i] = dp[i][i + 1] = 0;
           for (int i = 2; i <= n - 1; i++)
               dp[i - 1][i + 1] = a[i] * a[i - 1] * a[i + 1];
           for (int len = 3; len <= n - 1; len++)
               for (int i = 1; i + len <= n; i++) {
                   int j = i + len;
                   for (int k = i + 1; k < j; k++)
                       dp[i][j] = min(dp[i][j], dp[i][k] + a[i] * a[k] * a[j] + dp[k][j]);
               }
           printf("%d
    ", dp[1][n]);
       }
    }
  • 相关阅读:
    Oracle 多表查询优化
    FZU 2150 Fire Game
    HDU 1058 Humble Numbers
    UVA 11624 Fire!
    POJ 1321 棋盘问题
    线性表的基本操作
    POJ 3414 Pots(广搜输出路径)
    HDU 1495 非常可乐
    POJ 1847 Tram
    POJ 3159 Candies(查分约束)
  • 原文地址:https://www.cnblogs.com/smallhester/p/10381761.html
Copyright © 2020-2023  润新知