zoukankan      html  css  js  c++  java
  • 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学习 第25天 Oracle版的Profiler
    Oracle学习 第24天 .net EF连Oracle
    项目中各子系统之间数据交互的方法总结与心得
    Python学习 第7天 爬虫-1 构思
    Oracle学习 第23天 Oracle视图、表、SqlServer、excel、csv的互导
    Python学习 第6天 类、模块、包
    Python学习 第5天 函数
    Javascript/Jquery实现日期前一天后一天
    Javascript/Jquery遇到字符串自动NaN的问题
    Eclipse打包jar
  • 原文地址:https://www.cnblogs.com/smallhester/p/10381761.html
Copyright © 2011-2022 走看看