add: 230402-CN

This commit is contained in:
Eatswap 2023-04-02 01:21:32 +08:00
parent f8243bfdb3
commit e2e4efacce
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 44 additions and 1 deletions

43
cpp/2304/230402-CN.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <vector>
/**
* 1039. Minimum Score Triangulation of Polygon
*
* You have a convex n-sided polygon where each vertex has an integer value. You are given an integer array values where values[i] is the value of the ith vertex (i.e., clockwise order).
* You will triangulate the polygon into n - 2 triangles. For each triangle, the value of that triangle is the product of the values of its 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.
*/
class Solution {
private:
std::vector<int> val;
int n = -1;
int dp[51][51]{};
int func(int f, int t) {
int& ans = dp[f][t];
if (ans)
return ans;
const int d = t - f;
if (d < 2)
return 0;
if (d == 2) {
ans = val[t];
for (int i = f; i < t; ++i)
ans *= val[i];
return ans;
}
ans = 0x7FFFFFFF;
for (int i = 1 + f, bp = val[f] * val[t]; i < t; ++i)
ans = std::min(ans, bp * val[i] + func(f, i) + func(i, t));
return ans;
}
public:
int minScoreTriangulation(std::vector<int> values) {
val = std::move(values);
n = val.size();
return func(0, n - 1);
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2304)
SET(CMAKE_CXX_STANDARD 17)
ADD_EXECUTABLE(2304 230401-CN.cpp)
ADD_EXECUTABLE(2304 230402-CN.cpp)