Codeforces Round #256 (Div. 2) C

C. Painting Fence
time limit per test

1 second

memory limit per test

512 megabytes

input

standard input

output

standard output

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 test(s)
Input
5
2 2 1 2 1
Output
3
Input
2
2 2
Output
2
Input
1
5
Output
1
Note

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.

题意:有一个由多个长1格,高A[i]格的矩形拼成的墙,现在要刷墙,刷一下可以刷(1*无限)格,一个格可以重复刷,但是不能刷到没有墙的地方,求最少需要刷多少下。

题解:分治递归。

首先观察如果矩形中有高为0的,那它两边的墙可以分开考虑(因为不能一起刷嘛,没有相互影响)。

所以每组连续的不为0的墙分开考虑。观察一组墙,其最低的矩形以下可以选择全部横着刷,然后再考虑上面没刷的墙;另一种选择是全部竖着刷,这一组就刷完了。最优解肯定在这两种之中(最低的矩形以下,不是全横就是全竖,否则不横刷的那一行全部列都要竖刷才能把这行刷完,简直亏到爆)。然后上面的部分可以当作若干组墙,可以递归实现。

可能一下理解不了,可以看代码的具体实现,代码很短。其实还是挺容易的。

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back
const int inf=*(1e9)+;
int n;
int a[]; int gank(int L,int R,int low) {///[L,R]间的墙,底为low
int i;
int mi=inf;
int l;
int re=;
for(i=L; i<=R; i++) {
if(a[i]-low!=) {///找一组墙的最低矩形,一组墙的最左边记为l。
if(mi==inf)l=i;
if(a[i]<mi)mi=a[i];
} else {///a[i]==low,则之前的那一组墙(l~i-1)需要计算了
if(mi!=inf) {
int heng= mi-low + gank(l,i-,mi);///l~i-1全横刷
int shu=i-l;///l~i-1全竖着刷
re+=min(heng,shu);
//printf("mi=%d,low=%d,l=%d,i-1=%d,re+=%d, re=%d\n",mi,low,l,i-1,min(heng,shu),re);
mi=inf;
}
}
}
if(mi!=inf) {///最后那一组墙若没算,则现在算
int heng=mi-low + gank(l,i-,mi);///l~i-1全横刷
int shu=i-l;///l~i-1全竖着刷
re+=min(heng,shu);
//printf("l=%d,i-1=%d,re=%d\n",l,i-1,re);
mi=inf;
}
return re;
} int farm() {
int i;
return gank(,n-,);
} int main() {
int i;
RD(n);
REP(i,n)RD(a[i]);
printf("%d\n",farm());
return ;
}

CF448C Painting Fence (分治递归)的更多相关文章

  1. CF-448C Painting Fence 分治

    Painting fence 题意 乍一看以为是之前做过的一道单调队列优化的DP,不是. 也是有n块木板,每个木板宽1米,有一个高度ai,现在要把他们刷成橘色,给了你一个宽一米的刷子,你可以横着刷,或 ...

  2. codeforces 256 div2 C. Painting Fence 分治

    C. Painting Fence time limit per test 1 second memory limit per test 512 megabytes input standard in ...

  3. CF448C [Painting Fence]递归分治

    题目链接:http://codeforces.com/problemset/problem/448/C 题目大意:用宽度为1的刷子刷墙,墙是一长条一长条并在一起的.梳子可以一横或一竖一刷到底.求刷完整 ...

  4. CF448C Painting Fence (贪心分治)

    题面 \(solution:\) 一道蛮水的分治题,但思想很不错(虽然我还是非常天真的以为是积木大赛原题,并且居然还有30分) 看到这个题目,根据贪心的一贯风格,我们肯定能想到将整个栅栏的下面某部分直 ...

  5. painting fence - 分治 - Codeforces 448c

    2017-08-02 14:27:18 writer:pprp 题意: • 每块木板宽度均为1,高度为h[i] • n块木板连接为宽度为n的栅栏 • 每次可以刷一横或一竖(上色) • 最少刷多少次可以 ...

  6. C. Painting Fence 分治

    memory limit per test 512 megabytes input standard input output standard output Bizon the Champion i ...

  7. CF448C Painting Fence

    传送门 Descriptionzed 最近总是受到 Farmer 的困扰,因此他在自家的门前插了一排栅栏以防农气的入侵.栅栏由 N 个竖条栅栏横向组成,每个竖条栅栏宽度为 1.过了一段时间,zed 觉 ...

  8. 【题解】Painting Fence

    [题解]Painting Fence 分治模板.贪心加分治.直接\(O(n^2logn)\)分治过去.考虑一块联通的柱形是子问题的,是递归的,贪心分治就可.记得对\(r-l+1\)取\(min\). ...

  9. 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 ...

随机推荐

  1. 【BZOJ-4569】萌萌哒 ST表 + 并查集

    4569: [Scoi2016]萌萌哒 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 459  Solved: 209[Submit][Status] ...

  2. hdu 5017 模拟退火

    题意:给出椭球面的立体解析式,要求椭球面上距离原点最近的点的距离 sol:这题要想推公式就

  3. Bzoj4300 绝世好题

    Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 1325  Solved: 722 Description 给定一个长度为n的数列ai,求ai的子序列bi ...

  4. sublime编写markdown文件中Ctrl+B的作用

    今天,手残,用markdown编辑是按了Ctrl+B,结果发现直接在同一文件夹目录下编译生成了html文件,之前都是Alt+m,可以直接预览,可是后来由于系统更新还是什么,Alt+m只会在用户目录(而 ...

  5. MOOCULUS微积分-2: 数列与级数学习笔记 7. Taylor series

    此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...

  6. AngularJs $cacheFactory 缓存服务

    可能之前的api写的有些枯燥吧,因为不烧脑,不需要很多逻辑思维来做处理,那么之后的文章会有趣很多,慢慢的开始烧脑了,准备好大量脑细胞的死亡吧~   先来篇简单的缓存服务. 本文将api文档里的$cac ...

  7. 【Alpha版本】冲刺-Day6

    队伍:606notconnected 会议时间:11月14日 会议总结 张斯巍(433) 今天安排:学习UI设计 完成度:100% 明天计划:上传界面设计 遇到的问题:无 感想:刚开始学的时候,都是从 ...

  8. 修改MySQL的默认数据存储引擎

    因为MySQL默认的是MyISAM数据引擎,不支持事务也不支持外键,所以需要用到Innodb引擎,于是决定将mysql的默认引擎设置为innodb.1 . 查看MySQL存储引擎是用的哪个?登录MyS ...

  9. Form Submit表单提交

    <button type="button" ng-click="submit()"class="btn btn-block btn-danger ...

  10. python和numpy的版本、安装位置

    命令行下查看python和numpy的版本和安装位置 1.查看python版本 方法一: python -V 注意:‘-V‘中‘V’为大写字母,只有一个‘-’ 方法二: python --versio ...