P1775 石子合并 区间dp模板
作者:
多米尼克領主的致意
,
2024-04-23 12:06:26
,
所有人可见
,
阅读 2
O(n ^ 3)
#include<bits/stdc++.h>
using namespace std;
const int N = 310;
int w[N][N];
int a[N];
int f[N][N];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i = 1;i <= n;i++)
{
cin >> a[i];
a[i] += a[i - 1];
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
w[i][j] = a[j] - a[i - 1];
}
}
memset(f, 0x3f, sizeof f);
for(int i = 1;i <= n;i++)f[i][i] = 0;
for(int len = 2;len <= n;len++)
{
for(int i = 1;i <= n - len + 1;i++)
{
int j = i + len - 1;
for(int k = i;k <= j;k++)
{
f[i][j] = min(f[i][j], f[i][k] + f[k + 1][j] + w[i][j]);
}
}
}
cout << f[1][n] << endl;
return 0;
}