动态规划-石子合并

 

 

#include <iostream>
using namespace std;

const int N = 310;

int n;
int s[N];
int f[N][N];

int main()
{
    cin >> n;
    for (int i = 1; i <= n; ++ i)   cin >> s[i];
    
    for (int i = 1; i <= n; ++ i)   s[i] += s[i - 1];
    
    for (int len = 2; len <= n; ++ len) // 枚举区间长度
    {
        for (int i = 1; i + len - 1 <= n; ++ i) // 枚举起点位置
        {
            int l = i, r = i + len - 1;
            f[l][r] = 1e8;
            for (int k = l; k < r; ++ k)    // 枚举决策
            {
                f[l][r] = min(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);
            }
        }
    }
    
    cout << f[1][n] << endl;
    
    return 0;
}

 

posted @ 2021-08-30 17:51  青衫客36  阅读(51)  评论(0编辑  收藏  举报