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;
}

Codeforces 1012C Hills【DP】*的更多相关文章

  1. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  2. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  3. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  4. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  5. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  6. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  7. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  8. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  9. HackerRank - common-child【DP】

    HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...

随机推荐

  1. Elasticsearch6.0之一:安装

    基于windows平台,安装Elasticsearch, Elasticsearch-head. 1. 安装JAVA SDK,配置好环境变量 2. 安装NodeJs NPM, Grunt.   npm ...

  2. arm-linux-gcc安装使用教程

    arm-linux-gcc如何下载安装2(转) [转]ubuntu下交叉编译环境构建(arm-linux-gcc-3.4.1.tar.bz2 ) 2009-03-03 10:05 1.下载arm-li ...

  3. javaScript tips —— 标签上的data属性

    HTML5规定可以为元素添加非标准型的属性,只需添加前缀data-,这些属性可以随意添加,随意命名,目的是为元素提供与渲染无关的信息,或提供语义信息. 传统获取方式 'getAttribute' da ...

  4. scp 上传 下载 文件

    linux 中的ssh命令: scp 可以用来上传本地文件到远程服务器 或下载远程服务器中的文件到本地 1. 上传本地文件到远程服务器 scp readme.md user@www.*****.com ...

  5. PHP 7.3.0.beta3 发布,下个版本将进入 RC 阶段

    PHP 7.3.0 第三个测试版 beta3 已发布,源码下载地址 >>> https://downloads.php.net/~cmb/ 更新内容如下: - Core: . Fix ...

  6. 刷完了leetcode的数据库题目~

    很久很久很久之前,我上传了几条数据库题目,并没有坚持,今天跟新一下进度吧,其实没啥难度w(* ̄︶ ̄)

  7. Android6.0------权限申请RxPermissions

    前面写了Android6.0权限介绍和权限单个,多个申请,用的是纯Java代码,本文主要说的是借助第三方库来实现权限申请. 借助第三方库 RxPermissions来申请6.0权限. RxPermis ...

  8. 第八天 1-7 实战:创建一个root无法删除的文件

    实战:创建一个root无法删除的文件 简介:Linux文件的最底层(内核级别)属性的查看与修改 命令:lsattr.chattr Linux文件除了具有基本权限rwx,及特殊权限(SUID.SGID. ...

  9. 十七 Python分布式爬虫打造搜索引擎Scrapy精讲—深度优先与广度优先原理

      网站树形结构 深度优先 是从左到右深度进行爬取的,以深度为准则从左到右的执行(递归方式实现)Scrapy默认是深度优先的   广度优先 是以层级来执行的,(列队方式实现)

  10. 【MySQL】CSV 文件导入MySQL

    此问题是前几天整理数据的时候碰到的,数据存在 CSV文件中(200多万记录),通过python 往数据库中导入太慢了,后来使用MySQL 中自带的命令 LOAD DATA INFILE, 30多秒就能 ...