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. Windows Phone 有关独立存储(一)

    private const string foldername = "temp1"; private const string filename = foldername + &q ...

  2. 点击TextView 弹出复制选项

    extends:http://www.eoeandroid.com/thread-226805-1-1.html package com.dotfive.chuanbang.view; import ...

  3. Android - 获取SD卡的内存空间大小

    获取SD卡的内存空间大小 //获得SD卡空间的信息 File path=Environment.getExternalStorageDirectory(); StatFs statFs=new Sta ...

  4. 最小圆覆盖(随机增量法&模拟退火法)

    http://acm.hdu.edu.cn/showproblem.php?pid=3007 相关题型连接: http://acm.hdu.edu.cn/showproblem.php?pid=393 ...

  5. Javascript一(变量,数据类型,正则表达式,数据,语句)

    本文章适合具有一定程序编程语言基础的人士阅读,最好学完Java基础再来阅读本文章更容易理解语言初学者会看起来比较费劲,不易理解 一.导入脚本 在html导入Javascript的格式是: <sc ...

  6. Python爬虫实例(五) requests+flask构建自己的电影库

    目标任务:使用requests抓取电影网站信息和下载链接保存到数据库中,然后使用flask做数据展示. 爬取的网站在这里 最终效果如下: 主页: 可以进行搜索:输入水形物语 点击标题进入详情页: 爬虫 ...

  7. numpy中的convolve的理解

    https://blog.csdn.net/u011599639/article/details/76254442 函数 numpy.convolve(a, v, mode=‘full’),这是num ...

  8. 洛谷P2024 食物链 [NOI2001] 并查集

    正解:并查集 解题报告: 传送门(咕了! 其实没有很难(虽然我是交了三发才过的QAQ 但是一来好久没打并查集了恢复一下智力 二来看着智推里唯一一个蓝就很不爽(,,,虽然做了这题之后又补上了个蓝题QAQ ...

  9. centos 配置mysql

    1.在线安装 1.首先检测一下,mysql之前有没有被安装 命令:rpm -qa | grep mysql 2.删除mysql的命令: rpm -e --nodeps `rpm -qa | grep ...

  10. HTML---初识HTML

    版权声明:本文为博主原创文章.不经博主同意注明链接就可以转载. https://blog.csdn.net/Senior_lee/article/details/33723573          H ...