博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Codeforces 448C]Painting Fence
阅读量:5036 次
发布时间:2019-06-12

本文共 2644 字,大约阅读时间需要 8 分钟。

Description

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.

Sample Input

5 2 2 1 2 1

Sample Output

3

HINT

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.

题解

我们在$[1,n]$区间内,如果要横着涂,那么必定要从最底下往上涂$min(h_1,h_2,...h_n)$次,那么又会将$[1,n]$的序列分成多个子局面。

对于每个子局面,我们分治,单独求解,求解函数递归实现。复杂度$O(n^2)$。

1 //It is made by Awson on 2017.10.9 2 #include  3 #include 
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #define query QUERY16 #define LL long long17 #define Max(a, b) ((a) > (b) ? (a) : (b))18 #define Min(a, b) ((a) < (b) ? (a) : (b))19 using namespace std;20 const int N = 5000;21 22 int n, a[N+5];23 24 int doit(int l, int r, int base) {25 if (l == r) return 1;26 int high = 2e9;27 for (int i = l; i <= r; i++)28 high = Min(high, a[i]);29 int ans = high-base;30 for (int i = l; i <= r; i++)31 if (a[i] != high) {32 int j;33 for (j = i; j <= r && a[j+1] > high; j++);34 ans += doit(i, j, high);35 i = j+1;36 }37 return Min(ans, r-l+1);38 }39 void work() {40 scanf("%d", &n);41 for (int i = 1; i <= n ; i++) scanf("%d", &a[i]);42 printf("%d\n", doit(1, n, 0));43 }44 int main() {45 work();46 return 0;47 }

 

转载于:https://www.cnblogs.com/NaVi-Awson/p/7643011.html

你可能感兴趣的文章
APScheduler调度器
查看>>
设计模式——原型模式
查看>>
【jQuery UI 1.8 The User Interface Library for jQuery】.学习笔记.1.CSS框架和其他功能
查看>>
如何一个pdf文件拆分为若干个pdf文件
查看>>
web.xml中listener、 filter、servlet 加载顺序及其详解
查看>>
前端chrome浏览器调试总结
查看>>
获取手机验证码修改
查看>>
数据库连接
查看>>
python中数据的变量和字符串的常用使用方法
查看>>
等价类划分进阶篇
查看>>
delphi.指针.PChar
查看>>
Objective - C基础: 第四天 - 10.SEL类型的基本认识
查看>>
java 字符串转json,json转对象等等...
查看>>
极客前端部分题目收集【索引】
查看>>
第四天 selenium的安装及使用
查看>>
关于js的设计模式(简单工厂模式,构造函数模式,原型模式,混合模式,动态模式)...
查看>>
KMPnext数组循环节理解 HDU1358
查看>>
android调试debug快捷键
查看>>
【读书笔记】《HTTP权威指南》:Web Hosting
查看>>
Inoodb 存储引擎
查看>>