zoukankan      html  css  js  c++  java
  • 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.



    #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;
    }
  • 相关阅读:
    设计模式的C语言应用-外观模式-第八章
    00071_基本类型包装类
    win7不能在同一窗口打开文件夹,解决办法
    浏览器主页被搜狗劫持的解决办法,慎用宁美国度的安装软件,慎用不正规来源的软件
    使用inotify-tools与rsync构建实时备份系统
    浅淡 RxJS WebSocket
    SpringMVC大文件(百M以上)的上传下载实现技术
    C#.NET大文件(百M以上)的上传下载实现技术
    JSP大文件(10G以上)的上传下载实现技术
    JAVA大文件(10G以上)的上传下载实现技术
  • 原文地址:https://www.cnblogs.com/dream-maker-yk/p/9676313.html
Copyright © 2011-2022 走看看