• POJ 1651 Multiplication Puzzle (区间dp)


    Multiplication Puzzle
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6466   Accepted: 3927

    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
    题目大意:开始一行输入一个数n,然后接下来一行输入n个数;
    你可以在第 2 个数和第 n-1 个数中去掉任意一个数,此时计算出它和它两边的数的乘积,然后知道去掉这个区间的所有数,计算总的和;
    AC代码:区间dp
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<queue>
     6 #include<string>
     7 #include<cmath>
     8 using namespace std;
     9 const int INF = 1e8;
    10 int a[110];
    11 int dp[110][110];
    12 int main()
    13 {
    14   int n;
    15   while(cin>>n)
    16   {
    17       for(int i=1; i<=n; i++)
    18         scanf("%d",&a[i]);
    19       for(int i=n-2 ; i>=1; i--)//倒着遍历所有的区间;
    20       {
    21           for(int j=i+2; j<=n; j++)
    22           {
    23               dp[i][j] = INF;       //开始初始化全部为最大值;
    24                 for(int k=i+1; k<=j-1; k++)
    25                  dp[i][j] = min(dp[i][j],a[i]*a[k]*a[j]+dp[i][k]+dp[k][j]);
    26           }
    27       }
    28       printf("%d
    ",dp[1][n]); 
    29   }
    30   return 0;
    31 }
  • 相关阅读:
    spring-注解配置-junit整合测试-aop
    安装springsource-tool-suite插件成功之后找不到spring的处理办法
    spring-搭建-概念-配置详解-属性注入
    JSP-案例-商品增删改
    struts2-自定义拦截器-struts2标签
    struts2-OGNL表达式-OGNL与Struts2的结合
    struts2-result-servletAPI-获得参数-参数封装
    struts2-环境搭建-访问流程-配置详解-常量配置-类详解
    Hibernate-一对多|多对一-多对多
    Hibernate-HQL-Criteria-查询优化
  • 原文地址:https://www.cnblogs.com/lovychen/p/4029040.html
Copyright © 2020-2023  润新知