Section dp POJ 1651 Multiplication Puzzle Section DP

POJ 1651 Multiplication Puzzle Section DP

Given a string of numbers, seek to remove all the numbers in the middle to minimize the cost. The cost of taking one of the numbers is the product of this number and its left and right.

solution:

Consider the writing of the regular interval DP, the length of the enumeration interval, the starting point of the enumeration, and the enumeration split point (the split point here is which number is the last one to take away in a range! It may be the only point to consider in this question)

Let dp [i] [j] represent the minimum cost of all numbers from i to j, and a [i] represents the number at position i.

dp[i][j]=min(dp[i][k-1]+dp[k+1][j]+a[i-1]*a[k]*a[j+1])(k=i...j-1)

Because the minimum is considered, the array needs to be preprocessed as follows:

dp[i][i-1]=0;

dp[i][i]=a[i]*a[i+1]*a[i-1]

Other dp [i] [j] = INF;

#include <iostream>
#include<cstring>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N = 105;
int dp[N][N];
int a[N];
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i<n; i++) {
        scanf("%d", &a[i]);
    }
    
    memset(dp, 0x3f, sizeof(dp));
    for (int i = 1; i < n; i++)
        dp[i][i - 1] = 0;
    for (int i = 1; i < n - 1; i++) {
        dp[i][i] = a[i] * a[i - 1] * a[i + 1];
    }
    for (int len = 1; len <= n - 2; len++) {
        for (int i = 1; i + len - 1<n - 1; i++) {
            int end = i + len - 1;
            for (int k = i; k<=end; k++){
                dp[i][end] = min(dp[i][end], dp[i][k-1]+ dp[k + 1][end] + a[k] * a[i - 1] * a[end + 1]);
            }
        }
    }
    printf("%d\n", dp[1][n - 2]);
    return 0;
}

  

Given a string of numbers, seek to remove all the numbers in the middle to minimize the cost. The cost of taking one of the numbers is the product of this number and its left and right.

solution:

Consider the writing of the regular interval DP, the length of the enumeration interval, the starting point of the enumeration, and the enumeration split point (the split point here is which number is the last one to take away in a range! It may be the only point to consider in this question)

Let dp [i] [j] represent the minimum cost of all numbers from i to j, and a [i] represents the number at position i.

dp[i][j]=min(dp[i][k-1]+dp[k+1][j]+a[i-1]*a[k]*a[j+1])(k=i...j-1)

Because the minimum is considered, the array needs to be preprocessed as follows:

dp[i][i-1]=0;

dp[i][i]=a[i]*a[i+1]*a[i-1]

Other dp [i] [j] = INF;

#include <iostream>
#include<cstring>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int N = 105;
int dp[N][N];
int a[N];
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i<n; i++) {
        scanf("%d", &a[i]);
    }
    
    memset(dp, 0x3f, sizeof(dp));
    for (int i = 1; i < n; i++)
        dp[i][i - 1] = 0;
    for (int i = 1; i < n - 1; i++) {
        dp[i][i] = a[i] * a[i - 1] * a[i + 1];
    }
    for (int len = 1; len <= n - 2; len++) {
        for (int i = 1; i + len - 1<n - 1; i++) {
            int end = i + len - 1;
            for (int k = i; k<=end; k++){
                dp[i][end] = min(dp[i][end], dp[i][k-1]+ dp[k + 1][end] + a[k] * a[i - 1] * a[end + 1]);
            }
        }
    }
    printf("%d\n", dp[1][n - 2]);
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/hgangang/p/12741847.html