C. Painting Fence
time limit per test

1 second

memory limit per test

512 megabytes

input

standard input

output

standard output

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 integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Output

Print a single integer — the minimum number of strokes needed to paint the whole fence.

Examples
input
5
2 2 1 2 1
output
3
input
2
2 2
output
2
input
1
5
output
1
题意:给你一个栅栏,长度不等,宽度为1,用一个宽为1的刷子刷,最少刷几次;
思路:有点dp的思想,将一个大问题,分成多个小问题,每个问题的解决都一样,计算横着刷,与竖着刷的最小值;
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define mod 1000000007
#define pi (4*atan(1.0))
const int N=1e2+,M=1e5+,inf=1e9+;
int a[M],ans;
int dfs(int l,int r)
{
int minn=inf;
int sum=;
int shu=(r-l+);
for(int i=l;i<=r;i++)
minn=min(a[i],minn);
for(int i=l;i<=r;i++)
a[i]-=minn;
sum+=minn;
int st=l;
for(int i=l;i<=r;i++)
if(a[i]==)
{
sum+=dfs(st,i-);
st=i+;
}
if(st<=r)
sum+=dfs(st,r);
return min(sum,shu);
}
int main()
{
int x,y,z,i,t;
scanf("%d",&x);
for(i=;i<=x;i++)
scanf("%d",&a[i]);
cout<<dfs(,x)<<endl;
return ;
}

codeforces 256 div2 C. Painting Fence 分治的更多相关文章

  1. CF-448C Painting Fence 分治

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

  2. painting fence - 分治 - Codeforces 448c

    2017-08-02 14:27:18 writer:pprp 题意: • 每块木板宽度均为1,高度为h[i] • n块木板连接为宽度为n的栅栏 • 每次可以刷一横或一竖(上色) • 最少刷多少次可以 ...

  3. C. Painting Fence 分治

    memory limit per test 512 megabytes input standard input output standard output Bizon the Champion i ...

  4. 【题解】Painting Fence

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

  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 Round #256 (Div. 2/C)/Codeforces448C_Painting Fence(分治)

    解题报告 给篱笆上色,要求步骤最少,篱笆怎么上色应该懂吧,.,刷子能够在横着和竖着刷,不能跳着刷,,, 假设是竖着刷,应当是篱笆的条数,横着刷的话.就是刷完最短木板的长度,再接着考虑没有刷的木板,,. ...

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

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

  9. CF448C Painting Fence (分治递归)

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

随机推荐

  1. IIS7的HTTP到HTTPS的重定向

    方法一设置IIS 从HTTP到HTTPS的IIS7中的所有流量重定向,将确保用户始终安全地访问该网站.有许多不同的方法来设置一个IIS7从HTTP重定向到HTTPS和一些比别人更好.理想的HTTP到H ...

  2. 拼接的html的onclick事件中无法传递对象给js方法的处理办法

    如下: 拼接的html: " onclick=\"valDocName2('"+JSON.stringify(doc).replace(new RegExp(" ...

  3. Thinkphp自定义工具类的使用!

    在使用Thinkphp做开发的时候,很多时候会用到一些自己写的类,为了方便管理,可以把这些类,单独放到一个文件里. 这就是自定义工具类: 首先在 Application 目录下新建 Component ...

  4. Hive格式化输出数据库和表详细信息

    hive> desc database extended wx_test; OK wx_test hdfs://ns1/user/hive/warehouse/wx_test.db hadoop ...

  5. spfile与pfile

    SYS@ora11g>show parameter spfile NAME TYPE------------------------------------ ------------------ ...

  6. jquery插件方式实现table查询功能

    1.写插件部分,如下: ;(function($){ $.fn.plugin = function(options){ var defaults = { //各种属性,各种参数 } var optio ...

  7. 使用MySQLMTOP监控MySQL性能(二)

    一.服务器角色 服务器角色 172.18.35.29 10.160.22.14 (MySQL Master) 10.160.22.47 (MySQL Slave) 监控点 YES NO NO 被监控点 ...

  8. 使用异步消息处理更新UI线程

    1. Android的UI时线程不安全的,如果在子线程中更新UI会出现异常,导致程序崩溃. 为了解决如上这些问题,我们常用的做法就是使用Android的异步消息机制实现即可(创建一个Message对象 ...

  9. 转载:SQL Server编程基本语法

    一.定义变量 --简单赋值 declare @a int print @a --使用select语句赋值 ) select @user1='张三' print @user1 ) print @user ...

  10. Maven 的聚合(多模块)和 Parent 继承

    2017年06月26日 21:16:57 Maven 的聚合(多模块)和 Parent 继承 - 偶尔记一下 - CSDN博客 https://blog.csdn.net/isea533/articl ...