Codeforces 1012C Hills【DP】*

Codeforces 1012C Hills


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 ai. 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 (1<=k<=n/2)in range ? Here n/2 denotes n divided by two, rounded up.

Input

The first line of input contains the only integer n (1 ≤ n ≤ 5000)—the number of the hills in the sequence.
Second line contains n integers ai (1 ≤ ai ≤ 100 000)—the heights of the hills in the sequence.

Output

Print exactly n/2 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.


这个可能是全场唯一一道不是智商题的题了
思路其实很简单,我们考虑一个山峰的高度只会对左右的山峰产生影响
所以我们就可以转移状态,并且只记录前面山峰对后面山峰的影响
这样的正确性是有保障的,因为我们看可以发现在转移的时候我们考虑前一个对后一个的影响其实就是考虑了后一个对前一个的影响
然后用 f [ i ] [ j ] [ 0 / 1 ] 记录前i座山选择了j座山峰(是否选择当前山峰)的最小代价
并且用 g [ i ] [ j ] [ 0 / 1 ] 记录前i座山峰选择了j座山峰(是否选择当前山峰)时第i座山峰的最大高度
然后发现由于后面的状态只会被前一个的状态所影响,所以DP的转移显然是成立的
然后考虑怎么对状态进行转移
首先 f [ i ] [ j ] [ 0 ] 是可以从 f [ i 1 ] [ j ] [ 0 ] f [ i 1 ] [ j ] [ 1 ] 转移过来的
f [ i ] [ j ] [ 1 ] 只能从 f [ i 1 ] [ j 1 ] [ 0 ] 转移过来
所以直接分类讨论,顺便计算一下当前山的高度和前面山的高度多余的贡献
注意在更新答案的时候对于当前山要放的情况还需要多考虑后面的一座山峰的贡献
然后转移一下就好


#include<bits/stdc++.h>
using namespace std;
#define N 5010
int f[N][N>>1][2],g[N][N>>1][2];
int n,a[N],res[N];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    memset(f,0x3f,sizeof(f));
    memset(g,0x3f,sizeof(g));
    memset(res,0x3f,sizeof(res));
    f[0][0][0]=g[0][0][0]=0;
    for(int i=1;i<=n;i++)
        for(int j=0;j<=(i+1)/2;j++){
            if(j==0){
                f[i][j][0]=f[i-1][j][0];
                g[i][j][0]=a[i];
                continue;
            }
            //当前不放置
            int calc1=f[i-1][j][0];
            int calc2=f[i-1][j][1]+max(0,a[i]-g[i-1][j][1]+1);
            if(calc1<calc2){
                f[i][j][0]=calc1;
                g[i][j][0]=a[i];
            }else{
                f[i][j][0]=calc2;
                g[i][j][0]=min(a[i],g[i-1][j][1]-1);
            }
            //当前要放置
            f[i][j][1]=f[i-1][j-1][0]+max(0,g[i-1][j-1][0]-a[i]+1);
            g[i][j][1]=a[i];
            //统计答案
            int tip1=f[i][j][1]+max(0,a[i+1]-g[i][j][1]+1);
            int tip2=f[i][j][0];
            res[j]=min(res[j],min(tip1,tip2));
        }
    for(int i=1;i<=(n+1)/2;i++)printf("%d ",res[i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Dream_maker_yk/article/details/81293065