avatar
fireworks99
keep hungry keep foolish

石子合并(区间DP)

Description

有n堆石子排成一行,每次选择相邻的两堆石子,将其合并为一堆,记录该次合并的得分为两堆石子个数之和。已知每堆石子的石子个数,求当所有石子合并为一堆时,最小的总得分。

Analyze

一大堆石子由两小堆石子堆成,此次操作的得分为两小堆石子质量之和,而倘若这两小堆石子又各是由两小小堆石子堆成,那么此次操作的得分是:两堆石子之和 + 堆成第一堆的得分 + 堆成第二堆的得分

状态转移方程:

dp[i][j] = dp[i][k] + dp[k + 1][j] + (sum[j] - sum[i - 1])

Code of SDNUOJ 1045

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <vector>
#include <bitset>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define eps 1e-8
#define PI acos(-1.0)
#define ll long long
using namespace std;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
#define Close ios::sync_with_stdio(false);

int a[205];
int sum[205];
int dp[205][205];

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        memset(a, 0, sizeof(a));
        memset(sum, 0, sizeof(sum));
        for(int i = 1; i <= n; ++i)
        {
            scanf("%d", &a[i]);
            sum[i] = sum[i - 1] + a[i];
        }
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                dp[i][j] = (i == j ? 0 : INF);

        for(int len = 1; len <= n; ++len)
            for(int i = 1; i + len - 1 <= n; ++i)
                for(int j = i; j <= i + len - 1; ++j)
                    dp[i][i + len - 1] = min(dp[i][i + len - 1], dp[i][j] + dp[j + 1][i + len - 1] + sum[i + len - 1] - sum[i - 1]);
        cout << dp[1][n] << '\n';
    }
    return 0;
}

Code of SDNUOJ 1048

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <vector>
#include <bitset>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define eps 1e-8
#define PI acos(-1.0)
#define ll long long
using namespace std;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
#define Close ios::sync_with_stdio(false);

int a[405];
int sum[405];
int dp[405][405];

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        memset(a, 0, sizeof(a));
        memset(sum, 0, sizeof(sum));
        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)
        {
            a[i] = a[i - n];
            sum[i] = sum[i - 1] + a[i];
        }
        for(int i = 1; i <= 2 * n; ++i)
            for(int j = 1; j <= 2 * n; ++j)
                dp[i][j] = (i == j ? 0 : INF);

        for(int len = 1; len <= 2 * n; ++len)
            for(int i = 1; i + len - 1 <= 2 * n; ++i)
                for(int j = i; j <= i + len - 1; ++j)
                    dp[i][i + len - 1] = min(dp[i][i + len - 1], dp[i][j] + dp[j + 1][i + len - 1] + sum[i + len - 1] - sum[i - 1]);

        int ans = INF;
        for(int i = 1; i <= n; ++i)
            ans = min(ans, dp[i][i + n - 1]);
        cout << ans << '\n';
    }
    return 0;
}
Site by Baole Zhao | Powered by Hexo | theme PreciousJoy