codevs 2102
典型的石子归并,但此题与原题不同的是:这是一个圆圈,所以我们断环为链,但从哪里断了,最后决定随便断,本题是从1断,再将链延长为2倍
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e6 + 10;
int ans = 0x3f3f3f3f,ans2,dp[150][150],a[150],n,sum[250],f[150][150];
int main()
{ scanf("%d",&n);
for(int i = 1; i <= n; ++i)
{
scanf("%d",&a[i]);
sum[i] = sum[i - 1] + a[i];
}
for(int i = n + 1; i <= 2 * n; ++i) sum[i] = sum[i - 1] + a[i - n];// 将链延长为2倍 for(int i = 2; i <= n; ++i)
{
for(int j = 1; j <= 2 * n - i + 1; ++j)
{
dp[i][j] = 0x3f3f3f3f;
for(int k = 1; k < i; ++k)
{
dp[i][j] = min(dp[i - k][j] + dp[k][i + j - k] + sum[i + j - 1] - sum[j - 1],dp[i][j]);
f[i][j] = max(f[i - k][j] + f[k][i + j - k] + sum[i + j - 1] - sum[j - 1],f[i][j]);
}
}
} for(int i = 1; i <= n; ++i)
{
ans = min(dp[n][i],ans);
ans2 = max(f[n][i],ans2);
} printf("%d\n%d\n",ans,ans2);
return 0;
}