Painting Fence

Time Limit:1000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u

Submit Status

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

Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
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的更多相关文章

  1. CF448C Painting Fence (分治递归)

    Codeforces Round #256 (Div. 2) C C. Painting Fence time limit per test 1 second memory limit per tes ...

  2. 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 ...

  3. 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 ...

  4. codeforces 256 div2 C. Painting Fence 分治

    C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...

  5. 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 ...

  6. Codeforces 448 C. Painting Fence

    递归.分治. . . C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input ...

  7. 【题解】Painting Fence

    [题解]Painting Fence 分治模板.贪心加分治.直接\(O(n^2logn)\)分治过去.考虑一块联通的柱形是子问题的,是递归的,贪心分治就可.记得对\(r-l+1\)取\(min\). ...

  8. Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目

    C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...

  9. CF-448C Painting Fence 分治

    Painting fence 题意 乍一看以为是之前做过的一道单调队列优化的DP,不是. 也是有n块木板,每个木板宽1米,有一个高度ai,现在要把他们刷成橘色,给了你一个宽一米的刷子,你可以横着刷,或 ...

随机推荐

  1. 求满足n^2>12000的n的最大值 Exercise05_13

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:求满足n^2>12000的n的最大值 * */ public class Exercise05_13 { public ...

  2. 微服务之SpringCloud实战(四):SpringCloud Eureka源码分析

    Eureka源码解析: 搭建Eureka服务的时候,我们会再SpringBoot启动类加上@EnableEurekaServer的注解,这个注解做了一些什么,我们一起来看. 点进@EnableEure ...

  3. access日志配置

    链接地址:  https://wenku.baidu.com/view/3e20fac758f5f61fb73666cf.html org.apache.catalina.valves.AccessL ...

  4. Android 架构 3.实现

    以实现最小化可用产品(MVP)的目标,用最简单的方式来搭建架构和实现代码.IDE采用Android Studio,Demo实现的功能为用户注册.登录和展示一个券列表,数据采用我们现有项目的测试数据,接 ...

  5. PHP将字符串首字母大小写转换

    每个单词的首字母转换为大写:ucwords() <?php $foo = 'hello world!'; $foo = ucwords($foo); // Hello World! $bar = ...

  6. TDiocpCoderTcpServer返回数据记录有条数限制的问题

    TDiocpCoderTcpServer返回数据记录有条数限制的问题 在使用TDiocpCoderTcpServer控件返回查询数据的时候,发现当记录条数超过一定数量的时候(比方有人反试图返回30万条 ...

  7. [Android Memory] Android内存管理、监测剖析

    转载自:http://blog.csdn.net/anlegor/article/details/23398785 Android内存管理机制: Android内存管理主要有:LowMemory Ki ...

  8. ylbtech-dbs-m-ele(饿了么)

    ylbtech-dbs:ylbtech-m-ele(饿了么) -- =============================================-- DatabaseName:Ele - ...

  9. python——PEP8 Python 编码规范整理

    决定开始Python之路了,利用业余时间,争取更深入学习Python.编程语言不是艺术,而是工作或者说是工具,所以整理并遵循一套编码规范是十分必要的.所以今天下午我根据PEP 8整理了一份,以后都照此 ...

  10. ODOO Unable To Find Wkhtmltopdf On This System. Error/Bug ?

    If you are using ODOO version 8 and getting some error like – Unable to find Wkhtmltopdf on this sys ...