思路还是比较简单的 dp 吧,但是就是想不出来…甚至类似的方程都被自己推翻了

Description

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 \(a_i\). 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 \(1\le k\le \left\lceil\frac n2\right\rceil\)? Here \(\left\lceil\frac n2\right\rceil\) denotes \(n\) divided by two, rounded up.

Input

The first line of input contains the only integer \(n(1\le n\le 5000)\)—the number of the hills in the sequence.

Second line contains \(n\) integers \(a_i(1\le a_i\le 100\ 000)\)—the heights of the hills in the sequence.

Output

Print exactly \(\left\lceil\frac n2\right\rceil\) 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.

Examples

input

  1. 5
  2. 1 1 1 1 1

output

  1. 1 2 2

input

  1. 3
  2. 1 2 3

output

  1. 0 2

input

  1. 5
  2. 1 2 3 2 2

output

  1. 0 1 3

Note

In the first example, to get at least one hill suitable for construction, one can decrease the second hill by one in one hour, then the sequence of heights becomes \(1,0,1,1,1\) and the first hill becomes suitable for construction.

In the first example, to get at least two or at least three suitable hills, one can decrease the second and the fourth hills, then the sequence of heights becomes \(1,0,1,0,1\), and hills \(1,3,5\) become suitable for construction.

题意

现在有 \(n\) 座连在一起的山,高度从左到右分别为 \(a_i\)。现在你有一台挖掘机,每分钟可以令某一个 \(a_i\) 减一。你可以在 \(a_i>a_{i-1}\) 且 \(a_i>a_{i+1}\) 的山峰 \(i\) 盖房子。问对于 \(1\le k\le \left\lceil\frac n2 \right\rceil\) 中的每个整数 \(k\),盖 \(k\) 栋房子至少需要多长时间。

题解

针对本题有一个贪心,也就是不会有相邻的两个山同时被挖。

因此对于每个 \(i\),如果不挖这个位置,那么 \(a_i\) 是可以直接用原数据的。还有一点推论,即当 \(i\) 位置盖房子时,它一定不会被挖。挖了只会影响左右两边使他们更矮,不会产生更优的答案。

令 \(f[i][j][0/1]\) 表示前 \(i\) 座山中,盖了 \(j\) 栋房子,第 \(i\) 座山是否盖了房子。

那么如果第 \(i\) 座山要盖房子,那么第 \(i\) 座山不会被挖,高度为 \(a_i\);同时,第 \(i-1\) 座山的高度最多为 \(a_i-1\)。但是为了如果第 \(i-2\) 座要盖房子,那么第 \(i-1\) 座山的高度最多为 \(\min(a_{i-2}-1,a_i-1)\);第 \(i-2\) 座山不盖房子时,第 \(i-1\) 座山的高度就只受 \(a_i​\) 影响了。

如果第 \(i\) 座山不盖房子,那么考虑第 \(i-1\) 座山盖不盖房子就可以了。如果不盖,直接转移;否则把第 \(i\) 座山拉低到 \(a_{i-1}-1\) 的高度。

此时不考虑对后面的影响,因为我们的阶段就是 \(i\) 座山。

那么用类似于增量法的思路时就可以当 \(i\) 是 \(n\) 来做,思路会清晰很多。

状态转移方程为

\[\begin{aligned}
&f[i][j][1]=\begin{aligned}\min(&f[i-2][j-1][0]+\max(0,h[i-1]-h[i]+1),\\\ &f[i-2][j-1][1]+\max(0,h[i-1]-\min(h[i-2],h[i])+1)\end{aligned}\\\
&f[i][j][0]=\begin{aligned}\min(&f[i-1][j][1]+\max(0,h[i]-h[i-1]+1),\\\ &f[i-1][j][0])\end{aligned}
\end{aligned}
\]

最后对于每个 \(k\),输出 \(\min(f[n][k][0],f[n][k][1])\) 即可。

时间复杂度 \(O(n^2)​\)。

Code

  1. #include<cstdio>
  2. #include<cstring>
  3. int Min(int x,int y){return x<y?x:y;}
  4. int Max(int x,int y){return x>y?x:y;}
  5. int h[5050];
  6. int f[5050][5050][2];
  7. int main()
  8. {
  9. memset(f,0x3f,sizeof(f));
  10. f[0][0][0]=0;//初始化
  11. f[1][1][1]=0;
  12. f[1][0][0]=0;
  13. int n;
  14. scanf("%d",&n);
  15. for(int i=1;i<=n;++i)
  16. scanf("%d",&h[i]);
  17. h[0]=0x3fffffff;
  18. for(int i=2;i<=n;++i)
  19. {
  20. f[i][0][0]=f[i-1][0][0];
  21. for(int j=1;j<=(i+1)/2;++j)
  22. {
  23. f[i][j][1]=Min(f[i-2][j-1][0]+Max(0,h[i-1]-h[i]+1),
  24. f[i-2][j-1][1]+Max(0,h[i-1]-Min(h[i],h[i-2])+1));
  25. f[i][j][0]=Min(f[i-1][j][0],
  26. f[i-1][j][1]+Max(0,h[i]-h[i-1]+1));
  27. }
  28. }
  29. for(int i=1;i<=(n+1)/2;++i)
  30. printf("%d ",Min(f[n][i][0],f[n][i][1]));
  31. return 0;
  32. }

CF1012C Hills 题解【DP】的更多相关文章

  1. [FJOI2007]轮状病毒 题解(dp(找规律)+高精度)

    [FJOI2007]轮状病毒 题解(dp(找规律)+高精度) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1335733 没什么好说的,直接把规律找出来,有 ...

  2. 【线型DP】CF1012C Hills 小山坡

    来了来了. 题目: 给你n个数,你一次操作可以把某一个数-1(可以减为负数),你的目标是使任意的k个数严格小于它旁边的两个数(第一个数只用严格小于第二个数,第n个数只用严格小于第n-1个数),问最少需 ...

  3. CF1012C Hills

    显然的DP是,dp[i][j][val] val是1e6的 简化 发现,其实决策很有限,最优解的i-1的val选择有限 题解 这里的一个trick是,f[i][j][0]转移不考虑a[i]和a[i-1 ...

  4. HDU 6787 Chess 2020百度之星 初赛三 T5 题解 dp

    传送门:HDU 6787 Chess Problem Description 你现在有一个棋盘,上面有 n 个格子,格子从左往右,1,-,n 进行标号.你可以在棋盘上放置恰好 m 个传送器,并且对于每 ...

  5. 牛客练习赛69 火柴排队 题解(dp)

    题目链接 题目大意 给你一个长为n(n<=5e3)的数组a.随机使得k个元素增加d.要你求多大的概率使得,这些数组元素的相对大小不发生改变 输出 n 行每行一个整数,第 i 行的整数表示 k=i ...

  6. 古韵之乞巧 题解 dp题

    [noip模拟赛1]古韵之乞巧   描述 闺女求天女,更阑意未阑. 玉庭开粉席,罗袖捧金盘. 向月穿针易,临风整线难. 不知谁得巧,明旦试相看. ——祖咏<七夕> 女子乞巧,是七夕的重头戏 ...

  7. Codeforces 1012C Hills【DP】*

    Codeforces 1012C Hills Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suff ...

  8. Codeforces 691E题解 DP+矩阵快速幂

    题面 传送门:http://codeforces.com/problemset/problem/691/E E. Xor-sequences time limit per test3 seconds ...

  9. Codeforces 833B 题解(DP+线段树)

    题面 传送门:http://codeforces.com/problemset/problem/833/B B. The Bakery time limit per test2.5 seconds m ...

随机推荐

  1. gp sql

    appendonly -- drop table if exists test_appendonly; -- create table test_appendonly with(appendonly= ...

  2. CMD 与 ENTRYPOINT 的区别

    Dockerfile里有 CMD 与 ENTRYPOINT 两个功能咋看起来很相似的指令,开始的时候觉得两个互用没什么所谓,但其实并非如此: CMD指令: The main purpose of a ...

  3. HDU 1060 Leftmost Digit (数学log)

    题意:给定一个数n,让你求出n的n次方的第一位数. 析:一看这个n快到int极限了,很明显不能直接做,要转化一下.由于这是指数,我们可以把指数拿下来. 也就是取对数,设ans = n ^ n,两边取以 ...

  4. visual studio 2013 git 记住密码

    原有配置: C:\Users\Administrator 下.gitconfig内容为 [user] name = lijf4 email = lijf4@lenovo.com 删除,修改为 [cre ...

  5. 咏南树形下拉列表数据敏感控件--TYNdbTreeList

    咏南树形下拉列表数据敏感控件--TYNdbTreeList 软件系统拥有自己通用的下拉列表控件可以大大地加速系统的开发和易用性. 控件支持DELPHI5及以上版本安装并使用. 控件的用法: proce ...

  6. Objective-C 学习笔记(五) 快速枚举

    Objective-C 快速枚举 快速枚举是一个Objective-C的功能,有助于列举一个集合. 快速枚举语法 for (classType variable in collectionObject ...

  7. 在Excel中导入文本文件(CSV/TXT),自定义隔离符号

    经常需要在Excel中导入文本文件,但是需要自定义隔离符号,例如空格或者逗号,参考一下方法:

  8. CDH4.1.2 集群安装配置详细过程

    http://wenku.baidu.com/link?url=Wu43MFbzKH8hu7AgGfajmOr0WpRMX_gJlMDUs6pSrBK2LOJWIMpfWZa7IW-BSPko1yGl ...

  9. xml与json的区别和总结

    JSON和XML的比较 ◆可读性 JSON和XML的可读性可谓不相上下,一边是简易的语法,一边是规范的标签形式,很难分出胜负. ◆可扩展性 XML天生有很好的扩展性,JSON当然也有,没有什么是XML ...

  10. (zxing.net)一维码EAN 13的简介、实现与解码

    一维码EAN 13:属于国际标准条码, 由13个数字组成,为EAN的标准编码型式(EAN标准码). 依结构的不同,EAN条码可区分为: EAN 13码: 由13个数字组成,为EAN的标准编码型式(EA ...