【ACWing】1069. Division of convex polygons

Subject address:

https://www.acwing.com/problem/content/1071/

Given a NNConvex polygon with N vertices, divide the vertices from1 to 11 toNNN label, the weight of each vertex is a positive integer. Divide this convex polygon intoN − 2 N−2N 2 disjoint triangles. For each triangle, a weight product can be obtained by multiplying the weights of its three vertices. Try to find at least the sum of the weight products of all the vertices of the triangles.

Input format: the
first line contains the integer NNN represents the number of vertices. The second line containsNNN integers, vertices1 1in turn1 to vertexNNThe weight of N.

Output format:
output only one line, which is the minimum value of the sum of the product of the vertex weights of all triangles.

Data range:
N ≤ 50 N ≤ 50N. 5 0
data to ensure that all the vertices are less than the weights10 910 ^ 9109

The product of the weights of the three points may reach 1 0 27 10^{27}102 7 , so use high-precision addition and multiplication, that is, use vector to do it. The idea is dynamic programming, we put1 ∼ N 1\sim N1N 'sNNN vertices are regarded as longNNThe interval of N , consider vertices1, N 1, N1,N and which third vertex form a triangle. Obviously, the choice of the third point can be2, 3,..., N − 1 2,3,...,N-12,3,...,N1. Supposekkis selectedk , after selecting, divide polygon1, 2,..., k, 1 1,2,...,k,11,2,...,k,1 and dividing polygonk, k + 1,..., N, kk, k+1,...,N,kk,k+1,...,N,k is completely independent and does not affect each other, and these two problems are smaller in scale. Letf [i] [j] f[i][j]f [ i ] [ j ] is the dividing polygoni, i + 1,..., j − 1, ji, i+1,...,j-1,ji,i+1,...,j1,The smallest weight product sum that can be obtained by j , then we can followi, ji, ji,J and which third point form a triangle to classify, so: f [i] [j] = min ⁡ i + 1 ≤ k ≤ j − 1 {f [i] [k] + f [k] [j] + a [i] ∗ a [j] ∗ a [k]} f[i][j]=\min_{i+1\le k\le j-1}\{f[i][k]+f[ k][j]+a[i]*a[j]*a[k]\}f[i][j]=i+1kj1me{ f[i][k]+f[k][j]+a[i]a[j]a [ k ] } The length of the interval is from3 33 Start enumeration. Regarding the number of bits, the answer will definitely not exceed1 0 27 × 50 10^{27}\times 501027×5 0 , you can take30 303 0 bits. code show as below:

#include <iostream>
#include <vector>
using namespace std;

const int N = 55;
int n;
int a[N];
vector<long> f[N][N];

// 高精度乘法
vector<long> mul(vector<long> A, long b) {
    
    
    vector<long> C;
    if (A.empty()) return C;
    long t = 0;
    for (int i = 0; i < A.size() || t; i++) {
    
    
        if (i < A.size()) t += A[i] * b;
        C.push_back(t % 10);
        t /= 10;
    }

    return C;
}

// 高精度加法
vector<long> add(vector<long> A, vector<long> B) {
    
    
    if (A.empty()) A.push_back(0);
    else if (B.empty()) B.push_back(0);

    vector<long> C;
    long t = 0;
    for (int i = 0; i < A.size() || i < B.size() || t; i++) {
    
    
        if (i < A.size()) t += A[i];
        if (i < B.size()) t += B[i];
        C.push_back(t % 10);
        t /= 10;
    }

    return C;
}

// 高精度比较
int cmp(vector<long> A, vector<long> B) {
    
    
    if (A.size() != B.size()) return A.size() > B.size() ? 1 : -1;
    for (int i = A.size() - 1; i >= 0; i--)
        if (A[i] != B[i]) return A[i] > B[i] ? 1 : -1;
    return 0;
}

void printv(vector<long> A) {
    
    
    for (int i = A.size() - 1; i >= 0; i--) cout << A[i];
    cout << endl;
}

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

    for (int len = 3; len <= n; len++)
        for (int l = 1; l + len - 1 <= n; l++) {
    
    
            int r = l + len - 1;
            for (int i = 0; i < 30; i++) f[l][r].push_back(0);
            f[l][r].push_back(1);

            for (int k = l + 1; k < r; k++) {
    
    
                vector<long> tmp;
                tmp.push_back(1);
                tmp = mul(tmp, a[l]);
                tmp = mul(tmp, a[k]);
                tmp = mul(tmp, a[r]);
                if (len == 3) f[l][r] = tmp;
                else {
    
    
                    tmp = add(tmp, f[l][k]);
                    tmp = add(tmp, f[k][r]);
                    if (cmp(tmp, f[l][r]) < 0) f[l][r] = tmp;
                }
            }
        }

    printv(f[1][n]);

    return 0;
}

Time complexity O (n 3 log ⁡ v) O(n^3\log v)O ( n3logv )vvv is the maximum weight, spaceO (n 3 + log ⁡ v) O(n^3+\log v)O ( n3+logv )

Guess you like

Origin blog.csdn.net/qq_46105170/article/details/114605384