区间DP-凸多边形的划分

凸多边形的划分

题解:

这道题确实想了很久。关键我不知道这个last点怎么找。接下来讲讲我的过程吧。
1,集合:n-2条边组成的凸多边形的答案。(因为划分的时候我们是按照三角形来进行划分的所以我们可以理解为,我们的last点其实是一个三角形,那么我们凸边形的权值就是两个小凸边形加一个三角形的权值)。
2,属性:最小值。
3,状态:L,R端点。
4,last:所有可能组成的三角形。
那么就和上一道题很相似了。所以状态转移方程相信也不用写了。

这道题需要用高精度写,所以加一个高精度模板就可以了(偷懒型写法)

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 55, M = 35;

typedef long long LL;
int n;
int w[N];
LL f[N][N][M];

void add(LL a[], LL b[]) {
    LL c[M];
    memset(c, 0, sizeof c);
    for (int i = 0, t = 0; i < M; i ++) {
        t += a[i] + b[i];
        c[i] = t % 10;
        t /= 10;
    }
    memcpy(a, c, sizeof c);
}

void mul(LL a[], LL b) {
    LL c[M];
    memset(c, 0, sizeof c);
    LL t = 0;
    for (int i = 0; i < M; i ++) {
        t += a[i] * b;
        c[i] = t % 10;
        t /= 10;
    }
    memcpy(a, c, sizeof c);
}

int cmp(LL a[], LL b[]) {
    for (int i = M - 1; i >= 0; i --)
        if (a[i] > b[i]) return 1;
        else if (a[i] < b[i]) return -1;
    return 0;
}

void print(LL a[]) {
    int k = M - 1;
    while(k && !a[k]) k --;
    while(k >= 0) cout << a[k --];
    cout << endl;
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> w[i];

    LL temp[M];
    for (int len = 3; len <= n; len ++) {
        for (int i = 1; i + len - 1 <= n; i ++) {
            int l = i, r = i + len - 1;
            f[l][r][M - 1] = 1;

            for (int k = l + 1; k < r; k ++) {
                memset(temp, 0, sizeof temp);
                temp[0] = w[l];
                mul(temp, w[k]), mul(temp, w[r]);
                add(temp, f[l][k]), add(temp, f[k][r]);
                if (cmp(f[l][r], temp) > 0)
                    memcpy(f[l][r], temp, sizeof temp);
            }
        }
    }
    print(f[1][n]);
    return 0;
}

发布了92 篇原创文章 · 获赞 6 · 访问量 1184

猜你喜欢

转载自blog.csdn.net/weixin_42979819/article/details/103893415