题意: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;
}