zoukankan      html  css  js  c++  java
  • CF1012C Hills 题解【DP】

    思路还是比较简单的 dp 吧,但是就是想不出来…甚至类似的方程都被自己推翻了

    Description

    Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlasting city construction.

    From the window in your room, you see the sequence of (n) hills, where (i)-th of them has height (a_i). The Innopolis administration wants to build some houses on the hills. However, for the sake of city appearance, a house can be only built on the hill, which is strictly higher than neighbouring hills (if they are present). For example, if the sequence of heights is (5,4,6,2), then houses could be built on hills with heights (5) and (6) only.

    The Innopolis administration has an excavator, that can decrease the height of an arbitrary hill by one in one hour. The excavator can only work on one hill at a time. It is allowed to decrease hills up to zero height, or even to negative values. Increasing height of any hill is impossible. The city administration wants to build (k) houses, so there must be at least (k) hills that satisfy the condition above. What is the minimum time required to adjust the hills to achieve the administration's plan?

    However, the exact value of (k) is not yet determined, so could you please calculate answers for all (k) in range (1le kle leftlceilfrac n2 ight ceil)? Here (leftlceilfrac n2 ight ceil) denotes (n) divided by two, rounded up.

    Input

    The first line of input contains the only integer (n(1le nle 5000))—the number of the hills in the sequence.

    Second line contains (n) integers (a_i(1le a_ile 100 000))—the heights of the hills in the sequence.

    Output

    Print exactly (leftlceilfrac n2 ight ceil) numbers separated by spaces. The (i)-th printed number should be equal to the minimum number of hours required to level hills so it becomes possible to build (i) houses.

    Examples

    input

    5
    1 1 1 1 1
    

    output

    1 2 2 
    

    input

    3
    1 2 3
    

    output

    0 2 
    

    input

    5
    1 2 3 2 2
    

    output

    0 1 3 
    

    Note

    In the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes (1,0,1,1,1) and the first hill becomes suitable for construction.

    In the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes (1,0,1,0,1), and hills (1,3,5) become suitable for construction.

    题意

    现在有 (n) 座连在一起的山,高度从左到右分别为 (a_i)。现在你有一台挖掘机,每分钟可以令某一个 (a_i) 减一。你可以在 (a_i>a_{i-1})(a_i>a_{i+1}) 的山峰 (i) 盖房子。问对于 (1le kle leftlceilfrac n2 ight ceil) 中的每个整数 (k),盖 (k) 栋房子至少需要多长时间。

    题解

    针对本题有一个贪心,也就是不会有相邻的两个山同时被挖。

    因此对于每个 (i),如果不挖这个位置,那么 (a_i) 是可以直接用原数据的。还有一点推论,即当 (i) 位置盖房子时,它一定不会被挖。挖了只会影响左右两边使他们更矮,不会产生更优的答案。

    (f[i][j][0/1]) 表示前 (i) 座山中,盖了 (j) 栋房子,第 (i) 座山是否盖了房子。

    那么如果第 (i) 座山要盖房子,那么第 (i) 座山不会被挖,高度为 (a_i);同时,第 (i-1) 座山的高度最多为 (a_i-1)。但是为了如果第 (i-2) 座要盖房子,那么第 (i-1) 座山的高度最多为 (min(a_{i-2}-1,a_i-1));第 (i-2) 座山不盖房子时,第 (i-1) 座山的高度就只受 (a_i​) 影响了。

    如果第 (i) 座山不盖房子,那么考虑第 (i-1) 座山盖不盖房子就可以了。如果不盖,直接转移;否则把第 (i) 座山拉低到 (a_{i-1}-1) 的高度。

    此时不考虑对后面的影响,因为我们的阶段就是 (i) 座山。

    那么用类似于增量法的思路时就可以当 (i)(n) 来做,思路会清晰很多。

    状态转移方程为

    [egin{aligned} &f[i][j][1]=egin{aligned}min(&f[i-2][j-1][0]+max(0,h[i-1]-h[i]+1),\ &f[i-2][j-1][1]+max(0,h[i-1]-min(h[i-2],h[i])+1)end{aligned}\ &f[i][j][0]=egin{aligned}min(&f[i-1][j][1]+max(0,h[i]-h[i-1]+1),\ &f[i-1][j][0])end{aligned} end{aligned} ]

    最后对于每个 (k),输出 (min(f[n][k][0],f[n][k][1])) 即可。

    时间复杂度 (O(n^2)​)

    Code

    #include<cstdio>
    #include<cstring>
    int Min(int x,int y){return x<y?x:y;}
    int Max(int x,int y){return x>y?x:y;}
    int h[5050];
    int f[5050][5050][2];
    int main()
    {
        memset(f,0x3f,sizeof(f));
        f[0][0][0]=0;//初始化
        f[1][1][1]=0;
        f[1][0][0]=0;
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
            scanf("%d",&h[i]);
        h[0]=0x3fffffff;
        for(int i=2;i<=n;++i)
        {
            f[i][0][0]=f[i-1][0][0];
            for(int j=1;j<=(i+1)/2;++j)
            {
                f[i][j][1]=Min(f[i-2][j-1][0]+Max(0,h[i-1]-h[i]+1),
                               f[i-2][j-1][1]+Max(0,h[i-1]-Min(h[i],h[i-2])+1));
                f[i][j][0]=Min(f[i-1][j][0],
                               f[i-1][j][1]+Max(0,h[i]-h[i-1]+1));
            }
        }
        for(int i=1;i<=(n+1)/2;++i)
            printf("%d ",Min(f[n][i][0],f[n][i][1]));
        return 0;
    }
    
  • 相关阅读:
    BZOJ1841 : 蚂蚁搬家
    BZOJ3068 : 小白树
    BZOJ4449 : [Neerc2015]Distance on Triangulation
    BZOJ3692 : 愚蠢的算法
    BZOJ3145 : [Feyat cup 1.5]Str
    BZOJ4684 : Company Organization
    BZOJ2934 : [Poi1999]祭坛问题
    ML(2)——感知器
    ML(附录1)——梯度下降
    微服务架构
  • 原文地址:https://www.cnblogs.com/wjyyy/p/cf1012c.html
Copyright © 2011-2022 走看看