Codeforces Round #256 (Div. 2) C. Painting Fence
C. Painting Fence
Bizon the Champion isn't just attentive, he also is very hardworking.
Bizon the Champion decided to paint his old fence his favorite color, orange. The fence is represented as n vertical planks, put in a row. Adjacent planks have no gap between them. The planks are numbered from the left to the right starting from one, the i-th plank has the width of 1 meter and the height of ai meters.
Bizon the Champion bought a brush in the shop, the brush's width is 1 meter. He can make vertical and horizontal strokes with the brush. During a stroke the brush's full surface must touch the fence at all the time (see the samples for the better understanding). What minimum number of strokes should Bizon the Champion do to fully paint the fence? Note that you are allowed to paint the same area of the fence multiple times.
The first line contains integer n (1 ≤ n ≤ 5000) — the number of fence planks. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Print a single integer — the minimum number of strokes needed to paint the whole fence.
- 5
2 2 1 2 1
- 3
- 2
2 2
- 2
- 1
5
- 1
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
- #include <iostream>
- using namespace std;
- int main() {
- int n; cin >> n;
- int a[n+];
- a[] = ;
- for (int i=; i<=n; i++) cin >> a[i];
- int dp[n+][n+];
- for (int j=; j<=n; j++) dp[n][j] = ;
- for (int i=n-; i>=; i--) for (int j=; j<=i; j++) {
- if (a[j] >= a[i+]) dp[i][j] = dp[i+][i+]; //Already painted
- else {
- dp[i][j] = min( + dp[i+][j], a[i+]-a[j] + dp[i+][i+]);
- }
- }
- cout << dp[][] << endl;
- }
Codeforces Round #256 (Div. 2) C. Painting Fence的更多相关文章
- Codeforces Round #256 (Div. 2) C. Painting Fence(分治贪心)
题目链接:http://codeforces.com/problemset/problem/448/C C. Painting Fence time limit per test 1 second m ...
- Codeforces Round #256 (Div. 2) C. Painting Fence 或搜索DP
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
- Codeforces Round #256 (Div. 2) C. Painting Fence (搜索 or DP)
[题目链接]:click here~~ [题目大意]:题意:你面前有宽度为1,高度给定的连续木板,每次能够刷一横排或一竖列,问你至少须要刷几次. Sample Input Input 5 2 2 1 ...
- Codeforces Round #256 (Div. 2/C)/Codeforces448C_Painting Fence(分治)
解题报告 给篱笆上色,要求步骤最少,篱笆怎么上色应该懂吧,.,刷子能够在横着和竖着刷,不能跳着刷,,, 假设是竖着刷,应当是篱笆的条数,横着刷的话.就是刷完最短木板的长度,再接着考虑没有刷的木板,,. ...
- 贪心 Codeforces Round #173 (Div. 2) B. Painting Eggs
题目传送门 /* 题意:给出一种方案使得abs (A - G) <= 500,否则输出-1 贪心:每次选取使他们相差最小的,然而并没有-1:) */ #include <cstdio> ...
- Codeforces Round #256 (Div. 2) 题解
Problem A: A. Rewards time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces Round #256 (Div. 2)
A - Rewards 水题,把a累加,然后向上取整(double)a/5,把b累加,然后向上取整(double)b/10,然后判断a+b是不是大于n即可 #include <iostream& ...
- Codeforces Round #256 (Div. 2) D. Multiplication Table(二进制搜索)
转载请注明出处:viewmode=contents" target="_blank">http://blog.csdn.net/u012860063?viewmod ...
- Codeforces Round #256 (Div. 2) B. Suffix Structures(模拟)
题目链接:http://codeforces.com/contest/448/problem/B --------------------------------------------------- ...
随机推荐
- FPGA quartus开发中常见的错误处理
1.Warning: An incorrect timescale is selected for the Verilog Output (.VO) file of this PLL design. ...
- 【内核】几个重要的linux内核文件【转】
转自:http://www.cnblogs.com/lcw/p/3159394.html Preface 当用户编译一个linux内核代码后,会产生几个文件:vmlinz.initrd.img, 以及 ...
- 64_t7
texlive-ulqda-bin-svn13663.0-33.20160520.fc26.2..> 24-May-2017 15:57 33102 texlive-ulqda-doc-svn2 ...
- 浅谈js设计模式之策略模式
策略模式有着广泛的应用.本节我们就以年终奖的计算为例进行介绍. 很多公司的年终奖是根据员工的工资基数和年底绩效情况来发放的.例如,绩效为 S的人年终奖有 4倍工资,绩效为 A的人年终奖有 3倍工资,而 ...
- UE简单配置
1 头上显示文件位置和名称,视图->视图列表——>打开文件标签,在右面点放大 2 函数列表,视图->视图列表——>打开文件标签
- 洛谷P2149 Elaxia的路线
传送门啦 分析: 我最开始想的是跑两遍最短路,然后记录一下最短路走了哪些边(如果有两条最短路就选经过边多的),打上标记.两边之后找两次都标记的边有多少就行了. 但...我并没有实现出来. 最后让我们看 ...
- three.js是什么,能干嘛,和webgl什么关系
如今浏览器的功能越来越强大,而且这些功能可能通过JavaScript直接调用.你可以用HTML5标签轻松地添加音频和视频,而且可以在HTML5画布上创建各种交互组件.现在这个功能集合里又有了一个新成员 ...
- 判断一个字符是否为数字的两种方法(C/C++)
在平时,我们经常遇见判断字符是否为数字这种题目,虽然感觉还是很简单,不过我是个更喜欢用函数的人,因为我觉得这样更便捷,所以我更推荐第二种方式. 1.直接判断 #include <stdio.h& ...
- Mybatis入门及Dao开发方式
本节内容: Mybatis介绍 使用jdbc编程问题总结 Mybatis架构 Mybatis入门程序 Dao的开发方式 SqlMapConfig.xml文件说明 一.Mybatis介绍 MyBatis ...
- 12 打印1到最大的n位数
输入数字 n,按顺序打印出从 1 最大的 n 位十进制数.比如输入 3,则打印出 1.2.3 一直到最大的 3 位数即 999.由于 n 可能会非常大,因此不能直接用 int 表示数字,而是用 cha ...