• 区间DP——入门


    区间DP的解法较为固定,较为通用的方法是枚举区间长度,然后枚举左端点,再根据题目要求枚举断点什么的进行转移

    比如比较常见的一个转移方程

    $F_{ij} = max(F_{ik}+F_{k+1j} + sum_{j}-sum_{i-1})$

    DP时每层的区间长度是固定的,因此计算$F_{ij}$时,比它长度更短的$F_{ik}$和$F_{k+1j}$一定有效(被计算过)

    下面是几道简单题目

    P2858 [USACO06FEB]奶牛零食Treats for the Cows

    题目描述

    FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time.

    The treats are interesting for many reasons:The treats are numbered 1..N and stored sequentially in single file in a long box that is open at both ends. On any day, FJ can retrieve one treat from either end of his stash of treats.Like fine wines and delicious cheeses, the treats improve with age and command greater prices.The treats are not uniform: some are better and have higher intrinsic value. Treat i has value v(i) (1 <= v(i) <= 1000).Cows pay more for treats that have aged longer: a cow will pay v(i)*a for a treat of age a.Given the values v(i) of each of the treats lined up in order of the index i in their box, what is the greatest value FJ can receive for them if he orders their sale optimally?

    The first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.

    约翰经常给产奶量高的奶牛发特殊津贴,于是很快奶牛们拥有了大笔不知该怎么花的钱.为此,约翰购置了N(1≤N≤2000)份美味的零食来卖给奶牛们.每天约翰售出一份零食.当然约翰希望这些零食全部售出后能得到最大的收益.这些零食有以下这些有趣的特性:

    •零食按照1..N编号,它们被排成一列放在一个很长的盒子里.盒子的两端都有开口,约翰每

    天可以从盒子的任一端取出最外面的一个.

    •与美酒与好吃的奶酪相似,这些零食储存得越久就越好吃.当然,这样约翰就可以把它们卖出更高的价钱.

    •每份零食的初始价值不一定相同.约翰进货时,第i份零食的初始价值为Vi(1≤Vi≤1000).

    •第i份零食如果在被买进后的第a天出售,则它的售价是vi×a.

    Vi的是从盒子顶端往下的第i份零食的初始价值.约翰告诉了你所有零食的初始价值,并希望你能帮他计算一下,在这些零食全被卖出后,他最多能得到多少钱.

    输入输出格式

    输入格式:

    Line 1: A single integer, N

    Lines 2..N+1: Line i+1 contains the value of treat v(i)

    输出格式:

    Line 1: The maximum revenue FJ can achieve by selling the treats

    输入输出样例

    输入样例#1: 
    5
    1
    3
    1
    5
    2
    输出样例#1: 
    43

    说明

    Explanation of the sample:

    Five treats. On the first day FJ can sell either treat #1 (value 1) or treat #5 (value 2).

    FJ sells the treats (values 1, 3, 1, 5, 2) in the following order of indices: 1, 5, 2, 3, 4, making 1x1 + 2x2 + 3x3 + 4x1 + 5x5 = 43.

    code

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    int n, v[2010];
    int dp[2010][2010];
    
    int main() {
        cin >> n;
        for (int i = 1; i <= n; ++ i) cin >> v[i];
        for (int i = 1; i <= n; ++ i) dp[i][i] = v[i] * n; 
        for (int l = 2; l <= n; ++ l) 
            for (int i = 1; i + l - 1 <= n; ++ i) {
                int j = i + l - 1;
                dp[i][j] = max(dp[i][j - 1] + v[j] * (n - l + 1), dp[i + 1][j] + v[i] * (n - l + 1)); 
            }
        cout << dp[1][n] << endl;
        return 0;
    }

    P1880 [NOI1995]石子合并

    题目描述

    在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分。

    试设计出1个算法,计算出将N堆石子合并成1堆的最小得分和最大得分.

    输入输出格式

    输入格式:

    数据的第1行试正整数N,1≤N≤100,表示有N堆石子.第2行有N个数,分别表示每堆石子的个数.

    输出格式:

    输出共2行,第1行为最小得分,第2行为最大得分.

    输入输出样例

    输入样例#1: 
    4
    4 5 9 4
    输出样例#1: 

    43 54

    code

    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm> 
    
    using namespace std;
    
    int n,a[210],sum[210],f1[210][210],f2[210][210];
    
    int main() {
        scanf("%d",&n);
        for (int i = 1; i <= n; i++) {
            scanf("%d",&a[i]);
            a[n + i] = a[i];
        }    
        for (int i = 1; i <= n * 2; i++) 
            sum[i] = sum[i - 1] + a[i];
        for (int len = 2; len <= n; len++)
               for (int i = 1; i <= n * 2 - len + 1; i++) {
                int minn = 0x7fffffff,maxx = 0,j = i + len - 1;
                for(int k = i; k < j; k++) {
                    minn = min(minn,f1[i][k] + f1[k + 1][j] + sum[j] - sum[i - 1]);
                    maxx = max(maxx,f2[i][k] + f2[k + 1][j] + sum[j] - sum[i - 1]);
                    }
                    f1[i][j] = minn; f2[i][j] = maxx;
            }
        int maxx = 0,minn = 1e+9;
        for (int i = 1; i <= n; i++) {
            maxx = max(maxx,f2[i][i + n - 1]);
            minn = min(minn,f1[i][i + n - 1]);
        }
        printf("%d
    %d",minn,maxx);
        return 0;
    } 

    P1063 能量项链

    题目描述

    在 $Mars$ 星球上,每个 $Mars$ 人都随身佩带着一串能量项链。在项链上有 $N$ 颗能量珠。能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数。并且,对于相邻的两颗珠子,前一颗珠子的尾标记一定等于后一颗珠子的头标记。因为只有这样,通过吸盘(吸盘是 $Mars$ 人吸收能量的一种器官)的作用,这两颗珠子才能聚合成一颗珠子,同时释放出可以被吸盘吸收的能量。如果前一颗能量珠的头标记为 $m$ ,尾标记为 $r$ ,后一颗能量珠的头标记为$r$,尾标记为$n$ ,则聚合后释放的能量为 $m imes r imes n$ ( $Mars$ 单位),新产生的珠子的头标记为 $m$,尾标记为 $n$ 。

    需要时, $Mars$ 人就用吸盘夹住相邻的两颗珠子,通过聚合得到能量,直到项链上只剩下一颗珠子为止。显然,不同的聚合顺序得到的总能量是不同的,请你设计一个聚合顺序,使一串项链释放出的总能量最大。

    例如:设 $N=4$ , $4$ 颗珠子的头标记与尾标记依次为 $(2,3) (3,5) (5,10) (10,2)$ 。我们用记号⊕表示两颗珠子的聚合操作,( $j$ ⊕ $k$ )表示第 $j,k$两颗珠子聚合后所释放的能量。则第 $4$ 、 $1$ 两颗珠子聚合后释放的能量为:

    ( $4$ ⊕ $1$ ) $=10 imes 2 imes 3=60$

    这一串项链可以得到最优值的一个聚合顺序所释放的总能量为:

    (( $4$ ⊕ $1$ )⊕ $2$ )⊕ $3$ )$= 10 imes 2 imes 3+10 imes 3 imes 5+10 imes 5 imes 10=710$ 。

    输入输出格式

    输入格式:

    第一行是一个正整数 $N(4≤N≤100)$ ,表示项链上珠子的个数。第二行是 $N$ 个用空格隔开的正整数,所有的数均不超过 $1000$ 。第 $i$ 个数为第 $i $颗珠子的头标记 $(1≤i≤N)$ ,当 $i<N< span>$时,第$ i$ 颗珠子的尾标记应该等于第 $i+1$ 颗珠子的头标记。第 $N$ 颗珠子的尾标记应该等于第 $1$ 颗珠子的头标记。

    至于珠子的顺序,你可以这样确定:将项链放到桌面上,不要出现交叉,随意指定第一颗珠子,然后按顺时针方向确定其他珠子的顺序。

    输出格式:

    一个正整数 $E(E≤2.1 imes (10)^9)$ ,为一个最优聚合顺序所释放的总能量。

    输入输出样例

    输入样例#1: 
    4
    2 3 5 10
    
    输出样例#1: 
    710

    说明

    NOIP 2006 提高组 第一题

    code

    #include <bits/stdc++.h>
    using namespace std;
    int f[405][405];
    int n,a[205]; 
    int main()
    {
        cin >> n;
        for(int i=1;i<=n;i++)  //断环为链
    { cin >> a[i]; a[n+i]=a[i]; } for(int i=2;i<=n+1;i++) { for(int l=1;l+i-1<=2*n;l++) { int r=l+i-1; for(int k=l+1;k<=l+i-2;k++) f[l][r]=max(f[l][r],f[l][k]+f[k][r]+a[l]*a[k]*a[r]); } } int res=0; for (int i=1;i<=n;i++) res=max(res,f[i][n+i]); cout << res; return 0; }

     P3146 [USACO16OPEN]248

    题目描述

    Bessie likes downloading games to play on her cell phone, even though she doesfind the small touch screen rather cumbersome to use with her large hooves.

    She is particularly intrigued by the current game she is playing.The game starts with a sequence of N positive integers ( $2 leq Nleq 248$ ), each in the range $1 ldots 40 $ . In one move, Bessie cantake two adjacent numbers with equal values and replace them a singlenumber of value one greater (e.g., she might replace two adjacent 7swith an 8). The goal is to maximize the value of the largest numberpresent in the sequence at the end of the game. Please help Bessiescore as highly as possible!

    给定一个1*n的地图,在里面玩2048,每次可以合并相邻两个(数值范围1-40),问最大能合出多少。注意合并后的数值并非加倍而是+1,例如2与2合并后的数值为3。

    输入输出格式

    输入格式:

    The first line of input contains NN , and the next NN lines give the sequence

    of NN numbers at the start of the game.

    输出格式:

    Please output the largest integer Bessie can generate.

    输入输出样例

    输入样例#1: 
    4
    1
    1
    1
    2
    输出样例#1: 
    3

    说明

    In this example shown here, Bessie first merges the second and third 1s to

    obtain the sequence 1 2 2, and then she merges the 2s into a 3. Note that it is

    not optimal to join the first two 1s.

    code

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    const int MAXN = 500;
    int n;
    int dp[MAXN][MAXN], ans;
    
    int main() {
        cin >> n;
        for (int i = 1; i <= n; ++ i) cin >> dp[i][i];
        for (int L = 2; L <= n; ++ L) 
            for (int i = 1; i + L - 1 <= n; ++ i) {
                int j = i + L - 1;
                for (int k = i; k < j; ++ k) 
                    if (dp[i][k] == dp[k + 1][j]) {
                        dp[i][j] = max(dp[i][j], dp[i][k] + 1);
                        ans = max(ans, dp[i][j]);
                    }
            }
        cout << ans << endl;
        return 0;
    }

    总结

    以上都是些很水的区间DP,直接按套路出牌即可。做题时注意DP的初始化

    可以增长一波自信QwQ

  • 相关阅读:
    纪念我用word发布的第一篇文章
    第一个SpringMVCHelloWorld
    JSTL学习笔记
    bonecp的使用
    hdu 1556 树状数组
    hdu 1561 树形DP
    MYSQL使用笔记
    Android中简单实现Spinner的数据绑定
    Android中利用Application实现多个Activity间共享数据
    技术到底重要不重要?
  • 原文地址:https://www.cnblogs.com/hkttg/p/9278431.html
Copyright © 2020-2023  润新知