区间dp+

区间dp 石头合并

区间dp+

#include<bits/stdc++.h>
using namespace std;
int a[1001];
int s[1001];
int dp[1001][1001];
const int INF = 1e8;
int main() {
    int n;
    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 j = i + len - 1;
            dp[i][j] = INF;
            for (int k = i; k < j; k++) {
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j] + s[j] - s[i-1]);    
            }
        }
    }
    cout << dp[1][n] << endl;
    system("pause");
    return 0;
}
上一篇:PTA 乙级 1001 害死人不偿命的(3n+1)猜想


下一篇:hadoop之hbase(一)