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;
    }
  • 相关阅读:
    echo "不允许上传该类型的文件
    php构造函数,引入数据库操作类函数
    php函数描述及例子
    php如何判断远程文件是否存在
    多线程面试题系列(16):多线程十大经典案例之一 双线程读写队列数据
    多线程面试题系列(15):关键段,事件,互斥量,信号量的“遗弃”问题
    多线程面试题系列(14):读者写者问题继 读写锁SRWLock
    多线程面试体系列(13):多线程同步内功心法——PV操作下
    多线程面试题系列(12):多线程同步内功心法——PV操作上
    多线程面试题系列(11):读者写者问题
  • 原文地址:https://www.cnblogs.com/dream-maker-yk/p/9676313.html
Copyright © 2011-2022 走看看