• POJ 1651 Multiplication Puzzle 区间DP


    题意:在一个数字序列中, 取出不包括头和尾的所有数字, 每次取出一个数字的代价等于取出的这个数和左右两个数的乘积, 求总代价和最小的取法

    此小到大递归

     1 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c++交
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <iostream>
     6 #include <queue>
     7 #include <stack>
     8 #include <cmath>
     9 #include <set>
    10 #include <algorithm>
    11 #include <vector>
    12 // #include<malloc.h>
    13 using namespace std;
    14 #define clc(a,b) memset(a,b,sizeof(a))
    15 #define LL long long
    16 const int inf = 0x3f3f3f3f;
    17 const double eps = 1e-5;
    18 const double pi = acos(-1);
    19 const LL mod = 1e9+7;
    20 const int N = 710;
    21 // inline int r(){
    22 //     int x=0,f=1;char ch=getchar();
    23 //     while(ch>'9'||ch<'0'){if(ch=='-') f=-1;ch=getchar();}
    24 //     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    25 //     return x*f;
    26 // }
    27 
    28 int a[110],dp[110][110];//[i-1,j)的最小分数(i-1可以取,j取不到)
    29 int main(){
    30     int n;
    31     while(~scanf("%d",&n)){
    32         for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    33             clc(dp,0);
    34          for(int len=2;len<n;len++){//枚举长度
    35             for(int i=2;i+len<=n+1;i++){
    36                 int j=i+len-1;
    37                 dp[i][j]=inf;
    38                 for(int k=i;k<j;k++){//枚举区间[i-1,j)中最后删除的值
    39                     dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]+a[i-1]*a[k]*a[j]);
    40                 }
    41             }
    42         }
    43         printf("%d
    ",dp[2][n]);
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    UIButton图文上下对齐
    安装cocoapods
    mac忘记密码的解决办法
    css3圆角边框,边框阴影
    input元素的padding border margin的区别
    css font-family 字体全介绍,5b8b4f53 宋体 随笔
    mysql数据库 thinkphp连贯操作where条件的判断不正确的问题
    php获取客户端ip get_client_ip()
    php session小节
    ajax返回值中有回车换行、空格解决方法
  • 原文地址:https://www.cnblogs.com/ITUPC/p/5493089.html
Copyright © 2020-2023  润新知