poj1651 (区间dp)

题意:n个数中,每次选一个数和左右相邻数相乘,并且从中删除。最右边和最左边的数不能选,选数的顺序随意,最后n-2个乘积之和最小是多少.


解法:区间dp,记忆化搜索,水题。以此题开始,以后代码风格会变一些,开始尝试使用一些简化代码的预定义了,希望能够提高一点编码速度。

代码:

/****************************************************
* author:xiefubao
* Pro:
* algorithm:
* Time:32ms
* Judge Status:
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>

using namespace std;

#define sz(c) ((int)(c).size())
#define   forl(i, a, b) for(int i = (a); i <  (b); ++i)
#define  forle(i, a, b) for(int i = (a); i <= (b); ++i)
#define rst(a, v) memset(a, v, sizeof a)
#define repc(n) for(int repp_b = (n), repp = 0; repp < repp_b; ++repp)
#define eps 1e-8
#define INF (1<<29)
#define LL __int64
inline int     rdi() { int d; scanf("%d", &d); return d; }
inline char    rdc() { scanf(" "); return getchar(); }
inline double rddb() { double d; scanf("%lf", &d); return d; }


int num[210];
int n;
int ans[210][210];
int dp(int l,int r)
{
    if(r-l==1) return 0;
    if(ans[l][r]!=-1) return ans[l][r];
    if(r-l==2) return ans[l][r]=num[l]*num[l+1]*num[r];
    int tool=INF;
    forle(i,l+1,r-1){
        tool=min(tool,dp(l,i)+dp(i,r)+num[l]*num[i]*num[r]);
    }
    //cout<<tool<<endl;
    return ans[l][r]=tool;
}
int main()
{
    rst(ans,-1);
   n=rdi();
   forl(i,0,n){
       num[i]=rdi();
   }
   cout<<dp(0,n-1)<<endl;
   return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值