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.
The first line contains integer n (1 ≤ n ≤ 5000) — the number of fence planks. The second line contains n space-separated integersa1, a2, ..., an (1 ≤ ai ≤ 109).
Print a single integer — the minimum number of strokes needed to paint the whole fence.
5 2 2 1 2 1
3
2 2 2
2
1 5
1
In the first sample you need to paint the fence in three strokes with the brush: the first stroke goes on height 1 horizontally along all the planks. The second stroke goes on height 2 horizontally and paints the first and second planks and the third stroke (it can be horizontal and vertical) finishes painting the fourth plank.
In the second sample you can paint the fence with two strokes, either two horizontal or two vertical strokes.
In the third sample there is only one plank that can be painted using a single vertical stroke.
题意:
给出 n(0 ~ 5000),后给出 n 个数(1 ~ 10 ^ 9),代表有 n 块板,每块板 长为 ai,宽为 1,全部板并排打竖放着,现有一个刷子宽 1,可以一次性打横扫或者打竖扫,扫的时候不能有间隔,问最少扫多少次能将所有板都扫一遍。
思路:
分治法。整体来看,有两种方法,要不全部都打竖扫一遍,要不把共同最小值打横扫一遍 + 剩余不连续块的最小扫描数。而求剩余不连续块的最小扫描数的时候也同样也是运用这样的方法,故可以运用递归求解,当剩余 1 X ai 的时候则返回 1。
AC:
递归函数传过去的是左右区间边界,还有上一次所减去的最小值数 k 。所以下一次迭代求最小值的时候,要减去上一次的最小值 k,同时比较的时候也是这样。传下下次迭代的时候,最小值要加上上一次的最小值。注意这些就没有问题了。
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int MAX = 1000000005; int num[5005]; int solve(int l, int r, int k) { int Min = MAX; if (l == r) return 1; for (int i = l; i <= r; ++i) { Min = min(Min, num[i] - k); } int ans = 0; for (int i = l; i <= r; ++i) { if (num[i] - k > Min) { int ll = i, rr = i + 1; while (rr <= r && num[rr] - k > Min) ++rr; --rr; ans += solve(ll, rr, Min + k); i = rr + 1; } } return min(r - l + 1, Min + ans); } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &num[i]); printf("%d\n", solve(1, n, 0)); return 0; }
相关推荐
**案例分析:Pku1691—Painting A Board** 题目描述:给定一个长条木板和几种颜色,需要将木板涂色,使得相邻的两段颜色不同。此类问题可以通过动态规划来解决,通过构建一个二维数组来存储每个位置的最佳涂色方案...
python学习资源
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
基于Andorid的音乐播放器项目设计(国外开源)实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
python学习资源
python学习资源
python学习一些项目和资源
【毕业设计】java-springboot+vue家具销售平台实现源码(完整前后端+mysql+说明文档+LunW).zip
HTML+CSS+JavaScarip开发的前端网页源代码
python学习资源
【毕业设计】java-springboot-vue健身房信息管理系统源码(完整前后端+mysql+说明文档+LunW).zip
成绩管理系统C/Go。大学生期末小作业,指针实现,C语言版本(ANSI C)和Go语言版本
1_基于大数据的智能菜品个性化推荐与点餐系统的设计与实现.docx
【毕业设计】java-springboot-vue交流互动平台实现源码(完整前后端+mysql+说明文档+LunW).zip
内容概要:本文主要探讨了在高并发情况下如何设计并优化火车票秒杀系统,确保系统的高性能与稳定性。通过对比分析三种库存管理模式(下单减库存、支付减库存、预扣库存),强调了预扣库存结合本地缓存及远程Redis统一库存的优势,同时介绍了如何利用Nginx的加权轮询策略、MQ消息队列异步处理等方式降低系统压力,保障交易完整性和数据一致性,防止超卖现象。 适用人群:具有一定互联网应用开发经验的研发人员和技术管理人员。 使用场景及目标:适用于电商、票务等行业需要处理大量瞬时并发请求的业务场景。其目标在于通过合理的架构规划,实现在高峰期保持平台的稳定运行,保证用户体验的同时最大化销售额。 其他说明:文中提及的技术细节如Epoll I/O多路复用模型以及分布式系统中的容错措施等内容,对于深入理解大规模并发系统的构建有着重要指导意义。
基于 OpenCV 和 PyTorch 的深度车牌识别
【毕业设计-java】springboot-vue教学资料管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
此数据集包含有关出租车行程的详细信息,包括乘客人数、行程距离、付款类型、车费金额和行程时长。它可用于各种数据分析和机器学习应用程序,例如票价预测和乘车模式分析。
把代码放到Word中,通过开发工具——Visual Basic——插入模块,粘贴在里在,把在硅基流动中申请的API放到VBA代码中。在Word中,选择一个问题,运行这个DeepSeekV3的宏就可以实现在线问答
【毕业设计】java-springboot+vue机动车号牌管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip