5047. Minimum Score Triangulation of Polygon

博客围绕凸N边形三角剖分展开,将其划分为N - 2个三角形,每个三角形的值为顶点标签之积,三角剖分总分是所有三角形值之和。通过多个示例给出不同顶点标签数组下的输入输出,要求计算最小总分,还给出了数组长度和元素值范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given N, consider a convex N-sided polygon with vertices labelled A[0], A[i], …, A[N-1] in clockwise order.
Suppose you triangulate the polygon into N-2 triangles. For each triangle, the value of that triangle is the product of the labels of the vertices, and the total score of the triangulation is the sum of these values over all N-2 triangles in the triangulation.
Return the smallest possible total score that you can achieve with some triangulation of the polygon.
Example 1:

Input: [1,2,3]
Output: 6
Explanation: The polygon is already triangulated, and the score of the only triangle is 6.

Example 2:

Input: [3,7,4,5]
Output: 144
Explanation: There are two triangulations, with possible scores: 375 + 457 = 245, or 345 + 347 = 144. The minimum score is 144.

Example 3:

Input: [1,3,1,4,1,5]
Output: 13
Explanation: The minimum score triangulation has score 113 + 114 + 115 + 111 = 13.

Note:
  1. 3 <= A.length <= 50
  2. 1 <= A[i] <= 100
class Solution {
public:
    static const int MAX = 55;
    static const int INF = 1e9 + 5;

    int n;
    vector<int> A;
    int dp[MAX][MAX];

    int prev(int x) {
        return (x + n - 1) % n;
    }

    int next(int x) {
        return (x + 1) % n;
    }

    int solve(int start, int end) {
        if (start == end || next(start) == end || prev(start) == end)
            return 0;

        int &answer = dp[start][end];

        if (answer >= 0)
            return answer;

        answer = INF;

        for (int i = next(start); i != end; i = next(i))
            answer = min(answer, A[start] * A[i] * A[end] + solve(start, i) + solve(i, end));

        return answer;
    }

    int minScoreTriangulation(vector<int>& _A) {
        A = _A;
        n = A.size();
        memset(dp, -1, sizeof(dp));
        int best = INF;

        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++)
                for (int k = j + 1; k < n; k++)
                    best = min(best, A[i] * A[j] * A[k] + solve(i, j) + solve(j, k) + solve(k, i));

        return best;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值