"Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his college. On this holiday, Joke plans to go to his grandmother's house located in Schematics village. Joke's grandmother's house is more than a hundred years old. Joke is very kind hearted, so he wants to help renovate the house by painting the fence. The fence consists of N vertical boards placed on a line. The boards are numbered 1 to N from left to right, and each of them has the length of 1 meter and the height of Ai meters.

Joke's grandmother has a paintbrush that can be used to paint the fence. That paintbrush has a length of 1 meter. Joke paints the fence by brushing either horizontally or vertically, but the paint is expensive so Joke wants to minimize the number of paintbrush stroke. On each stroke, the paintbrush will make either a horizontal or vertical line. Also, the paintbrush must be touching the fence for the entire duration of the stroke. Joke also does not want to paint previously panted segment before. Help Joke to find the minimum number of stroke until the entire fence is covered with paint.

Input

First line contains a number N, the number of boards on the fence. The second line contains N numbers, A1, A2, A3 ... An representing the height of each board.

Output

Minimum number of stroke to paint the entire fence.

Sample Input 1

5
2 2 1 2 1

Sample Output 1

3

Sample Input 2

2
2 2

Sample Output 2

2

Sample Input 3

1
5

Sample Output 3

1

题意:又N个宽度为1的相邻围栏,每个有高度a[i],现在有一把宽度为1的刷子,可以横着刷或者竖着刷,问最少多少次刷完。

思路:对于每个区间,我们的最优情况的全部竖着刷, 或者横着刷全部公有的部分,其他的继续讨论。

由于每次最小横着刷一个,所以讨论次数不超过N。复杂度低于O(N^2)

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
const int inf=1e9+;
int a[maxn];
int solve(int L,int R,int H)
{
if(L==R) return ;
int ht=inf,res=,r;
for(int i=L;i<=R;i++) ht=min(ht,a[i]);
for(int i=L;i<=R;i++){
if(ht==a[i]) continue;
r=i;
while(r<R&&a[r+]>ht) r++;
res+=solve(i,r,ht);
i=r+;
}
res=min(R-L+,res+ht-H); return res;
}
int main()
{
int N,i;
scanf("%d",&N);
for(i=;i<=N;i++) scanf("%d",&a[i]);
printf("%d\n",solve(,N,));
return ;
}

SPOJ:House Fence(分治&DP)的更多相关文章

  1. BZOJ 4518 [Sdoi2016]征途(分治DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4518 [题目大意] 给出一个数列,分成m段,求方差最小,答案乘上m的平方. [题解] ...

  2. HDU 3507 Print Article(CDQ分治+分治DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3507 [题目大意] 将长度为n的数列分段,最小化每段和的平方和. [题解] 根据题目很容易得到dp ...

  3. 洛谷P2634 聪聪可可 [国家集训队] 点分治/dp

    正解:点分治/dp 解题报告: 传送门! 这题有两个做法,都是我不擅长的就都说下好了QAQ 首先这题一看到就会想到点分治? 也确实可以用点分治,那就直接用点分治鸭 每次求出到当前根距离余数为0,1,2 ...

  4. [BZOJ5125]小Q的书架(决策单调性+分治DP+树状数组)

    显然有决策单调性,但由于逆序对不容易计算,考虑分治DP. solve(k,x,y,l,r)表示当前需要选k段,待更新的位置为[l,r],这些位置的可能决策点区间为[x,y].暴力计算出(l+r)/2的 ...

  5. 【学术篇】CF833B TheBakery 分治dp+主席树

    题目の传送门~ 题目大意: 将\(n\)个蛋糕分成恰好\(k\)份, 求每份中包含的蛋糕的种类数之和的最大值. 这题有两种做法. 第一种是线段树优化dp, 我还没有考虑. 另一种就是分治+主席树. 然 ...

  6. BZOJ 2225: [Spoj 2371]Another Longest Increasing (CDQ分治+dp)

    题面 Description 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. Input Output ...

  7. CF-448C Painting Fence 分治

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

  8. Codeforces 659G Fence Divercity dp

    Fence Divercity 我们设a[ i ] 为第 i 个围栏被切的最靠下的位置, 我们发现a[ i ] 的最大取值有一下信息: 如果从i - 1过来并在 i  结束a[ i ] = min(h ...

  9. bzoj 3672 购票 点分治+dp

    3672: [Noi2014]购票 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1177  Solved: 562[Submit][Status][ ...

随机推荐

  1. maven编码gbk的不可映射字符

    如图,老是出现这个错误,百度说是打开源文件,更改编码格式,或者是更改File Encodings 的编码格式,或者是更改java compiler --use computer为eclipse, 都不 ...

  2. 《Java虚拟机原理图解》1.3、class文件中的访问标志、类索引、父类索引、接口索引集合

    讲完了class文件中的常量池,我们就相当于克服了class文件中最麻烦的模块了.现在,我们来看一下class文件中紧接着常量池后面的几个东西:访问标志.类索引.父类索引.接口索引集合. 1. 访问标 ...

  3. http转成https

     1.在阿里云服务器上购买一个AC证书,将证书下载下来 2.解压放在tomcat目录下,cert文件夹是我自己建的 3.修改tomcat的配置文件上面那个原来是被注掉的放开  把端口改为443,htt ...

  4. ETCD 单机安装

    由于测试的需要,有时需要搭建一个单机版的etcd 环境,为了方便以后搭建查看,现在对单机部署进行记录. 一.部署单机etcd 下载 指定版本的etcd下载地址 ftp://ftp.pbone.net/ ...

  5. POJ 题目3450 Corporate Identity(KMP 暴力)

    Corporate Identity Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5493   Accepted: 201 ...

  6. 关于android 使用bitmap的OOM心得和解决方式

    android开发,从2010年開始学习到如今的独立完毕一个app,这漫长的四年,已经经历了非常多次bug的折磨.无数次的加班训练.然而,自以为自己已经比較了解android了,却近期在一个项目上.由 ...

  7. gulp - sass 插件一直安装不好?

    1.没有sass,只存在与scss 任务 gulp.task('scss', function() { return gulp.src(app.srcPath + '/sass/*.scss') .p ...

  8. U盘 文件被隐藏解决办法

    原地址:http://www.deyi.com/thread-351635-1-1.html 方法:运行cmd( 在任意目录都行)单个文件 :attrib c:\"要修改的文件夹名字&quo ...

  9. ActiveMQ(五) 转

    package pfs.y2017.m11.mq.activemq.demo05; import javax.jms.Connection; import javax.jms.ConnectionFa ...

  10. Java基础 面向对象的详解

    1.1 万物皆对象 我们是怎么认识世界的? 人类从小就不断的接触到各种各类存在世界上的各种生物,然后通过事物的公共特性,将它们归类,所以以后就不会出现见到猫叫老虎.那么我们在现实生活中,是通过具体的某 ...