[Codeforces 448C]Painting Fence
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)$。
//It is made by Awson on 2017.10.9
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define query QUERY
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
const int N = ; int n, a[N+]; int doit(int l, int r, int base) {
if (l == r) return ;
int high = 2e9;
for (int i = l; i <= r; i++)
high = Min(high, a[i]);
int ans = high-base;
for (int i = l; i <= r; i++)
if (a[i] != high) {
int j;
for (j = i; j <= r && a[j+] > high; j++);
ans += doit(i, j, high);
i = j+;
}
return Min(ans, r-l+);
}
void work() {
scanf("%d", &n);
for (int i = ; i <= n ; i++) scanf("%d", &a[i]);
printf("%d\n", doit(, n, ));
}
int main() {
work();
return ;
}
[Codeforces 448C]Painting Fence的更多相关文章
- Codeforces 448C Painting Fence(分治法)
题目链接:http://codeforces.com/contest/448/problem/C 题目大意:n个1* a [ i ] 的木板,把他们立起来,变成每个木板宽为1长为 a [ i ] 的栅 ...
- Codeforces 448C Painting Fence:分治
题目链接:http://codeforces.com/problemset/problem/448/C 题意: 有n个木板竖着插成一排栅栏,第i块木板高度为a[i]. 你现在要将栅栏上所有地方刷上油漆 ...
- codeforces C. Painting Fence
http://codeforces.com/contest/448/problem/C 题意:给你n宽度为1,高度为ai的木板,然后用刷子刷颜色,可以横着刷.刷着刷,问最少刷多少次可以全部刷上颜色. ...
- Code Forces 448C Painting Fence 贪婪的递归
略有上升称号,最近有很多问题,弥补啊,各类竞赛滥用,来不及做出了所有的冠军.这个话题 这是一个长期记忆的主题.这是不是太困难,基本技能更灵活的测试,每次我们来看看这个问题可以被删除,处理然后分段层,贪 ...
- cf 448c Painting Fence
http://codeforces.com/problemset/problem/448/C 题目大意:给你一个栅栏,每次选一横排或竖排染色,求把全部染色的最少次数,一个点不能重复染色. 和这道题有点 ...
- 448C - Painting Fence(分治)
题意:给出宽为1高为Ai的木板n条,排成一排,每次上色只能是连续的横或竖并且宽度为1,问最少刷多少次可以使这些木板都上上色 分析:刷的第一步要么是所有的都竖着涂完,要么是先横着把最矮的涂完,如果是第一 ...
- Codeforces 448C:Painting Fence 刷栅栏 超级好玩的一道题目
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
- Codeforces Round #256 (Div. 2) C. Painting Fence(分治贪心)
题目链接:http://codeforces.com/problemset/problem/448/C C. Painting Fence time limit per test 1 second m ...
- Codeforces Round #256 (Div. 2) C. Painting Fence 或搜索DP
C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...
随机推荐
- java调用kettle的job和transfer工具类
package com.woaiyitiaocai.util; import java.util.Map; import java.util.UUID; import org.apache.log4j ...
- APP案例分析
产品 蓝叠安卓模拟器 选择理由 看了一眼桌面,就这个比较有意思.现在很多人喜欢玩手游,经常喜欢开个小号搞事情.这时候身边又没有多余的手机,怎么办?安卓模拟器下一个.手机屏幕太小玩起来没意思怎么 ...
- JAVA中GridBagLayout布局管理器应用详解
很多情况下,我们已经不需要通过编写代码来实现一个应用程序的图形界面,而是通过强大的IDE工具通过拖拽辅以简单的事件处理代码即可很轻松的完成.但是我们不得不面对这样操作存在的一些问题,有时候我们希望能够 ...
- Scrum 冲刺 第一日
Scrum 冲刺 第一日 站立式会议 燃尽图 Alpha 阶段认领任务 明日任务安排 项目预期任务量 成员贡献值计算规则 今日贡献量 参考资料 站立式会议 返回目录 燃尽图 返回目录 Alpha 阶段 ...
- edittext实现自动查询,刷新listview
mEdittextqueryvalue.addTextChangedListener(new TextWatcher() { @Override pub ...
- JAVA中if多分支和switch的优劣性。
Switch多分支语句switch语句是多分支选择语句.常用来根据表达式的值选择要执行的语句.例如,在某程序中,要求将输入的或是获取的用0-6代表的星期,转换为用中文表示的星期.该需求通过伪代码描述的 ...
- mui 页面无法下滑拖拽 主要体现在华为手机浏览器
项目做到中期遇到一个问题,华为手机有些页面显示不全且无法下滑. 因为之前一直用的Google浏览器的模拟模式进行开发和调试的,一直未发现这个问题. 刚开始 选用mui的下拉刷新上拉加载的方式来进行页面 ...
- JAVA_SE基础——30.构造代码块
黑马程序员入学blog...构造代码块作用:给所有的对象进行统一的初始化. 问题:要求每个小孩出生都会哭,这份代码有两个构造函数,如果需要每个小孩出生都要哭的话,那么就需要在不同的构造函数中都调用cr ...
- zf框架的思想及学习总结
在Php的配置文件中可以设置日志文件 dos命令进入文件夹,然后利用命令:>zf.bat create project d:/hspzf这样就可以在d盘进行创建项目文件了:然后需要把框架的Zen ...
- LeetCode & Q268-Missing Number-Easy
Array Math Bit Manipulation Description: Given an array containing n distinct numbers taken from 0, ...