CCF-CSP题解 201612-4 压缩编码
\(CSP\)也考\(DP\)的嘛...想了两小时贪心的我在宿舍凌乱...
还是智障+老花啊...这不是一道区间合并裸题嘛...石子合并啊...
再看看这\(3s\)的时限,\(O(n^3)\)都够了,何必想那么久贪心呢...
#include <bits/stdc++.h>
typedef long long LL;
const int maxn = 1000;
using namespace std;
LL a[maxn + 10];
LL dp[maxn + 10][maxn + 10];
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", a + i);
for (int i = 2; i <= n; i++)
a[i] += a[i - 1];
memset(dp, 0x3f, sizeof(dp));
for (int i = 1; i <= n; i++)
dp[i][i] = 0;
for (int len = 2; len <= n; len++)
{
for (int l = 1; l + len - 1 <= n; l++)
{
int r = l + len - 1;
for (int mid = l; mid <= r - 1; mid++)
{
dp[l][r] = min(dp[l][r], dp[l][mid] + dp[mid + 1][r]);
}
dp[l][r] += a[r] - a[l - 1];
}
}
if (n == 1)
printf("%lld\n", a[1]);
else
printf("%lld\n", dp[1][n]);
return 0;
}