[AcWing 1069] 凸多边形的划分

image


点击查看代码
#include<iostream>
#include<cstring>

using namespace std;

typedef long long LL;

const int N = 60, M = 50;

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

void add(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[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 tmp[M];
    for (int len = 3; len <= n; len ++) {
        for (int l = 1; l + len - 1 <= n; l ++) {
            int r = l + len - 1;
            f[l][r][M - 1] = 1;
            for (int k = l + 1; k < r; k ++) {
                int t = w[l];
                for (int i = 0; i < M; i ++) {
                    tmp[i] = t % 10;
                    t /= 10;
                }
                mul(tmp, w[k]);
                mul(tmp, w[r]);
                add(tmp, f[l][k]);
                add(tmp, f[k][r]);
                if (cmp(tmp, f[l][r]) < 0)
                    memcpy(f[l][r], tmp, sizeof tmp);
            }
        }
    }
    print(f[1][n]);
    return 0;
}

  1. 状态表示
    f[l][r] 表示将顶点 [l,r] 划分成三角形的所有方案的最小值
  2. 状态计算
    k 表示 (l,r) 中的一点
    f[l][r]=min(f[l][k]+f[k][r]+w[l]w[k]w[r])
  3. 高精度写法
posted @   wKingYu  阅读(24)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
欢迎阅读『[AcWing 1069] 凸多边形的划分』
点击右上角即可分享
微信分享提示