zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    C#开源框架
    8 种 NoSQL 数据库系统对比
    安装补丁“此更新不适用于你的计算机”解决办法
    .net开源资料
    winform程序退出
    jquery.chained与jquery.chained.remote使用以及区别
    存储过程使用回滚
    C# Panel中绘图如何出现滚动条
    C#结构体的特点浅析
    如何用堆栈和循环结构代替递归调用--递归转换为非递归的10条军规
  • 原文地址:https://www.cnblogs.com/ITUPC/p/5493089.html
Copyright © 2011-2022 走看看