zoukankan      html  css  js  c++  java
  • Multiplication Puzzle POJ

    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
    ",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
    ",dp[1][N]);
    31 }
  • 相关阅读:
    [BZOJ3510][洛谷P4299]首都(LCT)
    [luogu P5325][模板]Min_25筛
    [洛谷P3288][SCOI2014][BZOJ3597]方伯伯运椰子(网络流+图论)
    [洛谷P5342][TJOI2019]甲苯先生的线段树(数位dp)
    [CQOI2012][洛谷P3159][BZOJ2668]交换棋子(网络流+图论)
    [SDOI2015]约数个数和(莫比乌斯反演)
    使用Visual Studio 2005扩展创建Windows SharePoint Services 3.0 Web Part
    C#操作xml文件
    实用JS代码
    ASP.NET分页存储过程自定义用户控件(转)
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/6914838.html
Copyright © 2011-2022 走看看