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;
    }
  • 相关阅读:
    文件操作
    匿名函数
    函数
    运算符
    (模板)扩展kmp算法(luoguP5410)
    poj2406(求字符串的周期,kmp算法next数组的应用)
    poj1961(kmp算法next数组应用)
    hdoj1711(kmp算法)
    (模板)poj3461(kmp模板题)
    fzu1704(高斯消元法解异或方程组+高精度输出)
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7226977.html
Copyright © 2011-2022 走看看