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 in range ? Here denotes n divided by two, rounded up.

分析

  先来分析状态的定义,首先输出的时候跟k有关,所以k一定要先占一个状态,山的数目也要占一个状态,因为不枚举每座山峰,就无法知道山峰的高度,那么状态可不可以只有i和j呢?显然也不可以,建房子的时候有高度限制。于是状态大致就有了,是一个三维的状态,dp[i][k][],那么剩下的那个状态怎么选呢,因为我们转移的时候只考虑了前边的i个,所以第i个建不建,只与第i-1个有关,i与i-1建房的情况有三种,都不建,i建,i-1建,i和i-1都建呢?请回去读题。所以定义dp[i][j][0..2],表示前i个山,建j个房子,0:i和i-1都不建,1:i建,2:i-1建时最小花费。接下来考虑状态转移,对于0的情况,就是dp[i-1][j][0]和

dp[i-1][j][2]的情况,前者是i-1和i-2都不建,后者是i-1不建,i-2建。这两种情况都是满足我们这个状态并且合起来正好能够填满这个状态。对于1的情况,i建了,那么i-1一定不能建,考虑i-2建还是不建,如果i-2建了,那么i-1的山峰就要满足比i-2和i都小,如果i-1比i-2要高,那么i-1就至少要被砍到i-2的高度-1的位置,即min(a[i-1],a[i-2]-1),如果这个数还比i高,那么它还要被砍,于是额外的代价就是h-(a[i]-1),所以总代价就是dp[i-1][j-1][2]+max(0,min(a[i-1],a[i-2]-1)-a[i]+1),再考虑i-2不建的情况,就是看i-1需不需要砍一刀,所以这个状态最后的转移方程就是:

  dp[i][j][1]=min(dp[i-1][j-1][0]+max(0,a[i-1]-a[i]+1),dp[i-1][j-1][2]+max(0,min(a[i-1],a[i-2]-1)-a[i]+1));

虽然有点长但是好好理解一下还是挺好懂得,最后就是2的情况了,如果i-1建了,那么就要保证i的高度低于i-1于是取a[i]-a[i-1]+1和0的最大值就行。最后可以小小的优化一下,状态转移的时候,i能且只能由i-1转移过来,所以第一维状态可以省略。

  最后就是初始化了,由于要求最小,所以最开始全部初始化为INF,初态dp[0][0],dp[1][1]均为0,稍微解释一下,建0座房子,显然叭,不花钱,建一个,也不花。

  tips::压维的话注意一点,转移的时候,要考虑转移顺序,先转移0,因为0受2的影响,再转移2,2受1的影响,最后是1,这样就可以避免用现阶段的东西来转移现阶段的东西,从而保证了转移的正确性。

 #include<iostream>
#include<cstring>
using namespace std;
const int N=;
int dp[N][],a[N<<];
int main(){
int n;
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i];
memset(dp,0x3f,sizeof(dp));
dp[][]=dp[][]=;
for(int i=;i<=n;i++)
for(int j=i+>>;j;j--){
dp[j][]=min(dp[j][],dp[j][]);
dp[j][]=dp[j][]+max(,a[i]-a[i-]+);
dp[j][]=min(dp[j-][]+max(,a[i-]-a[i]+),dp[j-][]+max(,min(a[i-],a[i-]-)-a[i]+));
}
for(int j=;j<=n+>>;j++)
cout<<min(dp[j][],min(dp[j][],dp[j][]))<<" ";
}

最新文章

  1. js—模糊查询
  2. 你必须知道的指针基础-7.void指针与函数指针
  3. WebForm控件Repeater
  4. codeforces 425C Sereja and Two Sequences(DP)
  5. UIControlEvents 中各种event被触发的方式解释(zz)
  6. yii2源码学习笔记(十七)
  7. 获取win7时区所有信息
  8. Kcptun 是一个非常简单和快速的,基于KCP 协议的UDP 隧道,它可以将TCP 流转换为KCP+UDP 流
  9. Sql Server 2005的1433端口打开和进行远程连接
  10. 利用monkeyrunner实现Android屏幕连续截图
  11. 左偏树(BZOJ4003)
  12. ESP8266 mDNS
  13. Java集合框架(比较啰嗦)
  14. 4572: [Scoi2016]围棋 轮廓线DP KMP
  15. JS事件覆盖问题和触发问题
  16. request.get_full_path() 和request.path区别
  17. 【Android】11.2 通过重写对应的方法保存和恢复实例的状态
  18. 封装网络请求并在wxml调用
  19. Alpha项目冲刺_博客链接合集
  20. tomcat下配置https环境(windows环境)

热门文章

  1. LeetCode 31:递归、回溯、八皇后、全排列一篇文章全讲清楚
  2. android逆向---charles抓包
  3. Python3——2019年全国大学生计算二级考试
  4. 使用tf serving-gpu时,没有安装NVIDIA时报的错?
  5. jsp内置对象(三)-----response对象
  6. SpringBoot入门系列(四)整合模板引擎Thymeleaf
  7. Win10系统下安装tensorflow(cpu)+keras+jupyter notebook运行环境
  8. Idea - 常用基础配置
  9. EF6.0 下sql语句自动生成的参数类型decimal(18,2)修改
  10. oracle12c数据库第一周小测验