其他分享
首页 > 其他分享> > HRBUST 1819 石子合并问题--圆形版

HRBUST 1819 石子合并问题--圆形版

作者:互联网

题目链接:HRBUST 1819 石子合并问题--圆形版

题目大意:

题解:
区间dp模板题,由于是圆形操场,所以将数据再拓展一遍,用前缀和存储石子堆数量。

#include <iostream>
using namespace std;
#define INF 0x3f3f3f3f

int dpmin[210][210], dpmax[210][210], n, sum[210], num[210], ansmin, ansmax;

int main() {
    while (cin >> n) {
        for (int i = 1; i <= n; ++i) {
            cin >> num[i];
            num[i + n] = num[i];
        }
        for (int i = 1; i <= 2 * n; ++i) {
            sum[i] = sum[i - 1] + num[i];
        }
        for (int len = 2; len <= n; ++len) {
            for (int i = 1; i + len - 1 < 2 * n; ++i) {
                int j = i + len - 1;
                dpmin[i][j] = INF, dpmax[i][j] = 0;
                for (int k = i; k < j; ++k) {
                    dpmin[i][j] = min(dpmin[i][j], dpmin[i][k] + dpmin[k + 1][j] + sum[j] - sum[i - 1]);
                    dpmax[i][j] = max(dpmax[i][j], dpmax[i][k] + dpmax[k + 1][j] + sum[j] - sum[i - 1]);
                }
            }
        }
        ansmin = INF, ansmax = -INF;
        for (int i = 1; i <= n; ++i) {
            ansmin = min(ansmin, dpmin[i][i + n - 1]);
            ansmax = max(ansmax, dpmax[i][i + n - 1]);
        }
        cout << ansmin << ' ' << ansmax << endl;
    }
    return 0;
}

标签:HRBUST,210,--,石子,1819,int,num
来源: https://www.cnblogs.com/IzumiSagiri/p/15059648.html