• [NYIST737]石子合并(一)(区间dp)


    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=737

    很经典的区间dp,发现没有写过题解。最近被hihocoder上几道比赛题难住了,特此再回头重新理解一遍区间dp。

    这道题的题意很明确,有一列石子堆,每堆石子都有数量,还有一个操作:相邻两堆石子合并成一堆石子,这个操作的代价是这两堆石子的数目和。要找一个合并次序,使得代价最小,最终输出最小代价。

    这个题可以用动态规划,简单分析可以得知,这一列石子堆都可以划分为小区间,每个小区间需要解决的问题和大问题是一样的。

    假如有2堆石子a1 a2,那合并操作只有1种,就是直接合并这两堆石子,代价是a1+a2。

    假如有3堆石子a1 a2 a3,那合并操作只有2种,先合并前两堆或先合并后两堆,代价分别是(a1+a2)+(a1+a2)+a3和(a2+a3)+a1+(a2+a3)。

    ……

    我们发现他们在自己小区间内解决问题的时候,对于他们内部的合并顺序谁先谁后是不影响全局的,而且仔细分析可以知道已经得到的子问题的最优解也一定包含于原问题的最优解之内。

    比如还是刚才的例子,有三堆石子分别是1 2 3

    每次2堆考虑的时候,有两种情况,合并1 2或2 3,代价分别是1+2和2+3。

    再合并,考虑3堆的时候,那就是前面2堆的情况扩展一堆的情况了,这个扩展可以是(1+2)+3,也可以是(2+3)+1。加上之前的代价,他们的总和分别是9和11。

     1 /*
     2 ━━━━━┒ギリギリ♂ eye!
     3 ┓┏┓┏┓┃キリキリ♂ mind!
     4 ┛┗┛┗┛┃\○/
     5 ┓┏┓┏┓┃ /
     6 ┛┗┛┗┛┃ノ)
     7 ┓┏┓┏┓┃
     8 ┛┗┛┗┛┃
     9 ┓┏┓┏┓┃
    10 ┛┗┛┗┛┃
    11 ┓┏┓┏┓┃
    12 ┛┗┛┗┛┃
    13 ┓┏┓┏┓┃
    14 ┃┃┃┃┃┃
    15 ┻┻┻┻┻┻
    16 */
    17 #include <algorithm>
    18 #include <iostream>
    19 #include <iomanip>
    20 #include <cstring>
    21 #include <climits>
    22 #include <complex>
    23 #include <fstream>
    24 #include <cassert>
    25 #include <cstdio>
    26 #include <bitset>
    27 #include <vector>
    28 #include <deque>
    29 #include <queue>
    30 #include <stack>
    31 #include <ctime>
    32 #include <set>
    33 #include <map>
    34 #include <cmath>
    35 using namespace std;
    36 #define fr first
    37 #define sc second
    38 #define cl clear
    39 #define BUG puts("here!!!")
    40 #define W(a) while(a--)
    41 #define pb(a) push_back(a)
    42 #define Rint(a) scanf("%d", &a)
    43 #define Rll(a) scanf("%I64d", &a)
    44 #define Rs(a) scanf("%s", a)
    45 #define Cin(a) cin >> a
    46 #define FRead() freopen("in", "r", stdin)
    47 #define FWrite() freopen("out", "w", stdout)
    48 #define Rep(i, len) for(LL i = 0; i < (len); i++)
    49 #define For(i, a, len) for(LL i = (a); i < (len); i++)
    50 #define Cls(a) memset((a), 0, sizeof(a))
    51 #define Clr(a, x) memset((a), (x), sizeof(a))
    52 #define Fuint(a) memset((a), 0x7f7f, sizeof(a))
    53 #define lrt rt << 1
    54 #define rrt rt << 1 | 1
    55 #define pi 3.14159265359
    56 #define RT return
    57 #define lowbit(x) x & (-x)
    58 #define onenum(x) __builtin_popcount(x)
    59 typedef long long LL;
    60 typedef long double LD;
    61 typedef unsigned long long Uint;
    62 typedef pair<LL, LL> pii;
    63 typedef pair<string, LL> psi;
    64 typedef map<string, LL> msi;
    65 typedef vector<LL> vi;
    66 typedef vector<LL> vl;
    67 typedef vector<vl> vvl;
    68 typedef vector<bool> vb;
    69 
    70 const int maxn = 220;
    71 int dp[maxn][maxn];
    72 int a[maxn];
    73 int n;
    74 
    75 int main() {
    76     FRead();
    77     while(~Rint(n)) {
    78         For(i, 1, n+1) Rint(a[i]);
    79         For(i, 2, n+1) a[i] += a[i-1];
    80         Cls(dp);
    81         for(int h = 2; h <= n; h++) {
    82             for(int i = 1; i <= n - h + 1; i++) {
    83                 int j = i + h - 1;
    84                 dp[i][j] = 0x7f7f7f;
    85                 for(int k = i; k <= j; k++) {
    86                     dp[i][j] = min(dp[i][j], dp[i][k]+dp[k+1][j]+a[j]-a[i-1]);
    87                 }
    88             }
    89         }
    90         printf("%d
    ", dp[1][n]);
    91     }
    92     RT 0;
    93 }
  • 相关阅读:
    Adobe PS
    深入学习二叉树(04)平衡二叉树
    深入学习二叉树(03)二叉查找树
    C 知识点
    实战【docker 镜像制作与使用】
    从0到1了解 CI/CD
    理解Spring 容器、BeanFactory 以及 ApplicationContext
    Java 中 CAS
    volatile 关键字
    JenKins安装
  • 原文地址:https://www.cnblogs.com/kirai/p/5602255.html
Copyright © 2020-2023  润新知