"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. Codeforces 655E Beautiful Subarrays【01trie树】

    题目链接: http://codeforces.com/contest/665/problem/E 题意: 求异或值大于给定K的区间个数. 分析: 首先我们可以得到区间前缀的异或值. 这样我们将这个前 ...

  2. [bzoj1345][Baltic2007]序列问题_单调栈

    bzoj-1345 Baltic-2007 序列问题 题目大意:对于一个给定的序列a1,…,an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用一个元素max(ai,ai ...

  3. CodeChef - METEORAK Meteor

    Read problems statements in Mandarin Chineseand Russian. A meteor fell on Andrew's house. That's why ...

  4. java基础之IO流(一)字节流

    java基础之IO流(一)之字节流 IO流体系太大,涉及到的各种流对象,我觉得很有必要总结一下. 那什么是IO流,IO代表Input.Output,而流就是原始数据源与目标媒介的数据传输的一种抽象.典 ...

  5. javaScript 翻转

    一个字符串转成如下形式: 一个字符串转成如下形式"olleh dlrow"; public class reverseWord { public static void main( ...

  6. Spring Boot集成Spring Data Reids和Spring Session实现Session共享(多个不同的应用共用一个Redis实例)

    从Redis的Key入手,比如Spring Session在注解@EnableRedisHttpSession上提供了redisNamespace属性,只需要在这里设置不同的值即可,效果应该是这样的: ...

  7. 【转载】VS工具使用——代码图

    代码图:     心想,反正也调不出来,就试试这个东西吧,一打开,就认识到自己发现了一个新大陆:这个代码图可以让我们对一个工程文件有大体的了解,即函数的调用关系等.它是一个VS2013自带工具生成函数 ...

  8. 【源代码】LruCache源代码剖析

    上一篇分析了LinkedHashMap源代码,这个Map集合除了拥有HashMap的大部分特性之外.还拥有链表的特点,即能够保持遍历顺序与插入顺序一致. 另外.当我们将accessOrder设置为tr ...

  9. centos 安装php7 memcached扩展

    在使用下面这种方式安装php7 memcached扩展时,先要安装好memcached服务器yum -y install php70w-pecl-memcached

  10. cmd下并行执行appium +maven+Testng test

    1: first: open two command wins to start appium: appium -p 4725 -bp 4726 -U EP7333W0UR appium -p 472 ...