Monkey Party HDU 3506 区间dp
#include<iostream> #include<algorithm> #include<cstring> #include<stack> #include<cmath> #include<queue> using namespace std; #define ll long long #define N 2005 #define rep(i,l,r) for(i=l;i<=r;i++) const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; #define eps 0.00000001//偏差值1e8 #define pi acos(-1.0)//高精度圆周率 const int maxp = 1010; //点的数量 int a[N]; int dp[N][N]; int sum[N]; int s[N][N]; int main() { int i, j, k; int n, ret = 0, m, maxn = 0, count = 0; int t, x, len; while (cin >> n) { sum[0] = 0; memset(dp, INF, sizeof(dp)); rep(i, 1, n) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; dp[i][i] = 0; s[i][i] = i; } rep(i, 1, n) { sum[i + n] = sum[i + n - 1] + a[i]; s[i + n][i + n] = i + n; dp[i + n][i + n] = 0; } rep(len, 2, n) { rep(i, 1, 2 * n - 1) { j = i + len - 1; if (j > 2 * n - 1)break; rep(k, s[i][j - 1], s[i + 1][j]) { if (dp[i][k] + dp[k + 1][j] + sum[j] - sum[i - 1] < dp[i][j]) { dp[i][j] = dp[i][k] + dp[k + 1][j] + sum[j] - sum[i - 1]; s[i][j] = k; } } } } int ans = INF; rep(i, 1, n) { ans = min(ans, dp[i][i + n - 1]); } cout << ans << endl; } return 0; }