luogu P1880 [NOI1995]石子合并

咕咕咕

说好把济南集训时做的题写写题解还是鸽了然后因为太无聊又来续上了w

传送门~xiu

一道dp

具体是啥dp呢我去看看题解咋说

区间dp(跟我想的一样

把环拆成链来做

n的范围比较小就直接枚举从哪断开

然后就是正常的dp

区间和用前缀和优化一哈

看代码吧

#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 210
#define INF 999999999

int a[maxn];
int f[maxn][maxn],s[maxn][maxn];

int main() {
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) {
        scanf("%d",&a[i]);
        a[i + n] = a[i];
    }
    for(int i = 1; i <= 2 * n; i++)
        a[i] = a[i] + a[i - 1];
    for(int r = 2; r <= n; r++)
        for(int i = 1; i <= 2 * n - r + 1; i++) {
            int j = i + r - 1;
            f[i][j] = f[i + 1][j] + a[j] - a[i - 1];
            s[i][j] = s[i + 1][j] + a[j] - a[i - 1];
            for(int k = i; k < j; k++) {
                f[i][j] = max(f[i][j],f[i][k] + f[k + 1][j] + a[j] - a[i - 1]);
                s[i][j] = min(s[i][j],s[i][k] + s[k + 1][j] + a[j] - a[i - 1]);
            }
        }
    int maxx = 0,minn = INF;
    for(int i = 2; i <= n; i++) {
        maxx = max(maxx,f[i][i + n - 1]);
        minn = min(minn,s[i][i + n - 1]);
    }
    printf("%d\n%d",minn,maxx);
    return 0;
}

嗯草率地结尾了

啊对了

情人节快乐啊

都要快乐啊

就算一个人也要快乐啊

有人希望你快乐啊

 

posted @ 2019-02-14 20:55  ./seven  阅读(119)  评论(0编辑  收藏  举报