SPOJ:House Fence(分治&DP)
"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)的更多相关文章
- BZOJ 4518 [Sdoi2016]征途(分治DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4518 [题目大意] 给出一个数列,分成m段,求方差最小,答案乘上m的平方. [题解] ...
- HDU 3507 Print Article(CDQ分治+分治DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3507 [题目大意] 将长度为n的数列分段,最小化每段和的平方和. [题解] 根据题目很容易得到dp ...
- 洛谷P2634 聪聪可可 [国家集训队] 点分治/dp
正解:点分治/dp 解题报告: 传送门! 这题有两个做法,都是我不擅长的就都说下好了QAQ 首先这题一看到就会想到点分治? 也确实可以用点分治,那就直接用点分治鸭 每次求出到当前根距离余数为0,1,2 ...
- [BZOJ5125]小Q的书架(决策单调性+分治DP+树状数组)
显然有决策单调性,但由于逆序对不容易计算,考虑分治DP. solve(k,x,y,l,r)表示当前需要选k段,待更新的位置为[l,r],这些位置的可能决策点区间为[x,y].暴力计算出(l+r)/2的 ...
- 【学术篇】CF833B TheBakery 分治dp+主席树
题目の传送门~ 题目大意: 将\(n\)个蛋糕分成恰好\(k\)份, 求每份中包含的蛋糕的种类数之和的最大值. 这题有两种做法. 第一种是线段树优化dp, 我还没有考虑. 另一种就是分治+主席树. 然 ...
- BZOJ 2225: [Spoj 2371]Another Longest Increasing (CDQ分治+dp)
题面 Description 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. Input Output ...
- CF-448C Painting Fence 分治
Painting fence 题意 乍一看以为是之前做过的一道单调队列优化的DP,不是. 也是有n块木板,每个木板宽1米,有一个高度ai,现在要把他们刷成橘色,给了你一个宽一米的刷子,你可以横着刷,或 ...
- Codeforces 659G Fence Divercity dp
Fence Divercity 我们设a[ i ] 为第 i 个围栏被切的最靠下的位置, 我们发现a[ i ] 的最大取值有一下信息: 如果从i - 1过来并在 i 结束a[ i ] = min(h ...
- bzoj 3672 购票 点分治+dp
3672: [Noi2014]购票 Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 1177 Solved: 562[Submit][Status][ ...
随机推荐
- java基础之IO流(二)之字符流
java基础之IO流(二)之字符流 字符流,顾名思义,它是以字符为数据处理单元的流对象,那么字符流和字节流之间的关系又是如何呢? 字符流可以理解为是字节流+字符编码集额一种封装与抽象,专门设计用来读写 ...
- 电音中DJ/Producer/MC/EDM/Remix/Mix的名词解释(转)
DJ DJ是Disc Jockey的缩写,是电音圈子里的一种热门职业,一般大家在夜店或者酒吧看到的站在台上甩着膀子拧着按钮或者使劲儿搓碟的就是DJ啦. DJ的主要工作一般就是在现场用打碟机和混音台把许 ...
- MD5加密算法Java代码
原文:http://www.open-open.com/code/view/1428398234916 import java.security.MessageDigest; import java. ...
- 50 个 Bootstrap 插件
Bootstrap是快速开发Web应用程序的前端工具包.它是一个CSS和HTML的集合,它使用了最新的浏览器技术,给你的Web开发提供了时尚的版式,表单,buttons,表格,网格系统等等. 本文向你 ...
- PythonCookbook读书笔记
第一章 数据结构和算法 1.1 将序列分解为单独的变量 适用于元组.列表.字符串等.只要是可迭代的对象,都可以执行分解操作.唯一的要求是变量的总数和结构要与序列相同. 1.2 从任意长度的可迭代对象中 ...
- firebug console说明
控制台(Console)是Firebug的第一个面板,也是最重要的面板,主要作用是显示网页加载过程中产生各类信息. 一.显示信息的命令 Firebug内置一个console对象,提供5种方法,用来显示 ...
- 推断dxf文件的版本号
打开DXF參考手冊,在DXF參考手冊中,点击"索引"-->输入"HEADER",在ACADVER字段有acd的版本号信息: 以下是用C语言,写的推断dxf ...
- HDOJ 1217 Arbitrage(拟最短路,floyd算法)
Arbitrage Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- mysql 查询 优化
1.基本原则:优化数据访问 (1)是否想服务器请求了不需要的数据?提取超过需要的列,多表连接时提取所有列,提取所有列都会消耗不必要的资源,提取你所需要的列就可以了. (2)MySQL检查了太多的数据吗 ...
- break 用法
// break 在循环中的功能测试 # include <stdio.h> int main(void){ int i, j; for (i = 0; i<3; ++i) { j ...