Painting Fence
Description
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.
Input
The first line contains integer n(1 ≤ n ≤ 5000) — the number of fence planks. The second line contains n space-separated integersa1, a2, ..., an(1 ≤ ai ≤ 109).
Output
Print a single integer — the minimum number of strokes needed to paint the whole fence.
Sample Input
5
2 2 1 2 1
3
2
2 2
2
1
5
1
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = ;
int a[MAXN];
int solve(int l, int r)
{
int k = l;
if(l > r) return ;
for(int i = l; i <= r; i++)
if(a[i] < a[k])
k = i;
int tmp = a[k];
for(int i = l; i <= r; i++)
a[i] -= tmp;
return min(r-l+, solve(l, k-) + solve(k+, r) + tmp);
}
int main()
{
int n;
scanf("%d",&n);
for(int i = ; i < n; i++)
scanf("%d",&a[i]);
printf("%d\n", solve(, n-));
return ;
}
递归,每次做的到操作都是比较全部横着涂完和全部竖着涂完的涂刷到次数较小的次数,递归出答案。
Painting Fence的更多相关文章
- CF448C Painting Fence (分治递归)
Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...
- 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
C. Painting Fence Bizon the Champion isn't just attentive, he also is very hardworking. Bizon the Ch ...
- codeforces 256 div2 C. Painting Fence 分治
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(分治贪心)
题目链接:http://codeforces.com/problemset/problem/448/C C. Painting Fence time limit per test 1 second m ...
- Codeforces 448 C. Painting Fence
递归.分治. . . C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input ...
- 【题解】Painting Fence
[题解]Painting Fence 分治模板.贪心加分治.直接\(O(n^2logn)\)分治过去.考虑一块联通的柱形是子问题的,是递归的,贪心分治就可.记得对\(r-l+1\)取\(min\). ...
- Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
- CF-448C Painting Fence 分治
Painting fence 题意 乍一看以为是之前做过的一道单调队列优化的DP,不是. 也是有n块木板,每个木板宽1米,有一个高度ai,现在要把他们刷成橘色,给了你一个宽一米的刷子,你可以横着刷,或 ...
随机推荐
- java web(学习笔记)项目路径问题
最近刚接触java web特别是是关于项目路径这一块很晕,就把自己遇到的一些疑惑和理解写下来. 首先贴上路径,这里用的是eclipse. 其中我们要注意看WebContent目录,这是web程序的根目 ...
- SQLServer组成:
SQL Server DB Engine (Relational Engine),SQL语言用于向Engine描述问题. Algebrizer:代数器,检查语法,并将查询转换成内部表达式 Query ...
- Matlab与C++混合编程 编写独立外部应用程序时出现“无法定位序数3906于动态链接库LIBEAY32.dll上”错误
出现“无法定位序数3906于动态链接库LIBEAY32.dll上”错误,这种错误一般是同名函数出现在两个不同的头文件中了. 笔者的这个错误是由于 #include "mat.h" ...
- Redis编程实践【pub/sub】
原文:http://shift-alt-ctrl.iteye.com/blog/1867454 Redis或许已经在很多企业开始推广并试水,本文也根据个人的实践,简单描述一下Redis在实际开发过程中 ...
- 【FTP】java FTPClient 文件上传内容为空,文件大小为0
问题:如题所述,使用FTPClient上传至FTP服务器, 表现如下:①文件大小为0 ②上传很小的文件,但是要花费很长的时间,20K要花费2分钟甚至更久 ③没有任何的报错,没有任何的乱码 解决方法: ...
- 认识多渲染目标(Multiple Render Targets)技术 【转】
认识多渲染目标(Multiple Render Targets)技术 首先,渲染到纹理是D3D中的一项高级技术.一方面,它很简单,另一方面它很强大并能产生很多特殊效果. 比如说发光效果,环境映射,阴影 ...
- tessellation 曲面细分 on Android
Mac OS X 10.8 (OpenGL 3.2), MacOSX 10.9 (OpenGL 3.2 to 4.1) Windows with NVIDIA since 2006 (GeForce ...
- [Python爬虫] 之十九:Selenium +phantomjs 利用 pyquery抓取超级TV网数据
一.介绍 本例子用Selenium +phantomjs爬取超级TV(http://www.chaojitv.com/news/index.html)的资讯信息,输入给定关键字抓取资讯信息. 给定关键 ...
- elasticsearch中的filter与aggs
今天在ES上做了一个聚合,先过滤一个嵌套对象,再对另一个域做聚合,但是过滤似乎没有起作用 { "size":0, "filter":{ "nested ...
- linux下javadoc生成文件出现中文乱码
javadoc命令的正确使用姿势 javadoc -d apidoc -windowtitle Testing -doctitle 'The API of javadoc' -header 'My c ...