1. 程式人生 > >Codeforces 1012C Hills【DP】*

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[i1][j][0]f[i1][j][1]轉移過來的
f[i][j][1]只能從f[i1][j1][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;
}