题目大意:每次从中间(取出两边)序列中取出一个数,则它的分值是它乘以与它相邻的两数,求去玩所有的中间数后分值和最小为多少
思路:区间dp,dp[i][j]表示从i到j的最优解,枚举从i到j最后移除的元素,有dp[i][j] = min(dp[i][j] , a[i]*a[j]*a[k]+dp[i][k]+dp[k][j]);答案是dp[0][n-1].
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <iomanip>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define maxn 105
#define MOD 1000000007
#define mem(a , b) memset(a , b , sizeof(a))
#define LL long long
#define INF 100000000
int n;
int a[maxn];
int dp[maxn][maxn];
int solve(int i , int j)
{
if(dp[i][j] != INF) return dp[i][j];
if(j == i +1) return dp[i][j] = 0;
for(int k = i + 1 ; k < j ; k ++)
{
dp[i][j] = min(dp[i][j] , a[i]*a[k]*a[j]+solve(i , k) + solve(k , j));
}
return dp[i][j];
}
int main()
{
while(scanf("%d" , &n) != EOF )
{
for(int i = 0 ; i < n ;i ++)
{
scanf("%d" , &a[i]);
}
for(int i = 0 ; i < n ; i ++)
{
for(int j = 0 ; j < n ; j ++)
dp[i][j] = INF;
}
solve(0 , n-1);
printf("%d\n" , dp[0][n-1]);
}
return 0;
}