zoukankan      html  css  js  c++  java
  • poj 1651 Multiplication Puzzle【区间DP】

    题目链接:http://poj.org/problem?

    id=1651

    题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n)
    同一时候ans+=a[pos-1]*a[pos]*a[pos+1]。一直消元素直到最后剩余2个,求方案最小的ans是多少?

    代码:

    #include <stdio.h>
    #include <ctime>
    #include <math.h>
    #include <limits.h>
    #include <complex>
    #include <string>
    #include <functional>
    #include <iterator>
    #include <algorithm>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <set>
    #include <map>
    #include <list>
    #include <bitset>
    #include <sstream>
    #include <iomanip>
    #include <fstream>
    #include <iostream>
    #include <ctime>
    #include <cmath>
    #include <cstring>
    #include <cstdio>
    #include <time.h>
    #include <ctype.h>
    #include <string.h>
    #include <string>
    #include <assert.h>
    #pragma comment(linker,"/STACK:1024000000,1024000000")
    
    using namespace std;
    
    const long long inf = 1e18;
    
    int n;
    int a[110];
    long long dp[110][110];
    
    int main()
    {
        while (cin>>n)
        {
            for (int i = 1; i <= n; i++)
                cin >> a[i];
            memset(dp, 0, sizeof(dp));
            for (int k = 2; k <= n - 1; k++)//区间长度
            {
                for (int i = 1; i + k <= n; i++)//区间起点
                {
                    int j = i + k;//区间终点
                    dp[i][j] = inf;
                    for (int r = i + 1; r < j; r++)
                    {
                        dp[i][j] = min(dp[i][j], dp[i][r] + dp[r][j] + a[i] * a[r] * a[j]);
                    }
                }
            }
            cout << dp[1][n] << endl;
        }
        return 0;
    }
  • 相关阅读:
    MySQL表的四种分区类型
    微信开发配置(Yii框架下的开发)
    一道编程题—输出字符串内重复的数字
    无序数组内查找指定值(快速查找)
    指针
    chmod
    cookie和session的区别
    使用keytool生成证书
    人大金仓修改最大连接数
    数据库链接地址
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7226977.html
Copyright © 2011-2022 走看看