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;
}
Codeforces 1012C Hills【DP】*的更多相关文章
- [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】
[CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...
- Kattis - honey【DP】
Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDOJ 1257 最少拦截系统 【DP】
HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDOJ 1159 Common Subsequence【DP】
HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】
HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】
POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...
- HackerRank - common-child【DP】
HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...
随机推荐
- NNCRF之NNSegmentation, NNPostagging, NNNameEntity
这里主要介绍NNSegmentation 介绍: NNSegmentation是一个基于LibN3L的利用神经网络来进行分词的工具. 他可以通过不同的模型(NN, RNN, GatedNN, LSTM ...
- Python类变量,实例变量,类方法,实例方法,静态方法的分析
Python作为动态语言,跟静态语言如c/c++有很大区别,其中的一个重要的特性就是Python的变量无需声明直接可用.同样,类的成员变量无需声明,直接可用.目的是为了动态语言跟灵活,在思路想到的时候 ...
- 基于GTID搭建MHA
一.简介 MHA 是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件.在MySQL故障切换过程中,MHA能做到在0~30秒之内自动完成数据库的故障切换操作,并且在进行故障切换的过程 ...
- 关于Gulp
Gulp & webpack 配置详解http://www.jianshu.com/p/2d9ed1fe3e8c 使用 Gulphttp://hwaphon.site/?p=439 前端构建工 ...
- 聚类效果评测-Fmeasure和Accuracy及其Matlab实现
聚类结果的好坏,有很多种指标,其中F-Measue即F值是常用的一种,其中包括precision(查准率或者准确率)和recall(查全率或者召回率). F-Measue是信息检索中常用的评价标准. ...
- selenium常用API实例
1.访问网页地址 driver.get( url ); driver.navigate( ).to( url ); 2.访问网页前进.后退 driver.navigate( ).forward( ); ...
- SSM的Maven项目搭建过程
POM文件 父项目管理jar包,pom <modelVersion>4.0.0</modelVersion> <groupId>cn.e3mall</grou ...
- Python写入CSV文件的问题
这篇文章主要是前几天我处理数据时遇到的三个问题: Python写入的csv的问题 Python2与Python3处理写入写入空行不同的处理方式 Python与Python3的编码问题 其实上面第3个问 ...
- laravel中上传图片之后图片的处理
$file=Input::file('file'); if ($file->isValid()){ $entension=$file->getClientOriginalExtension ...
- Week14《Java程序设计》第14次作业总结
Week14<Java程序设计>第14次作业总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2.1 简述如何 ...