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. Oracle参数化查询

    Oracle参数化查询默认是根据顺序绑定的 select * from table where name=:p1 and (select id from table2 where name=:p1); ...

  2. UOJ263 【NOIP2016】组合数问题

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  3. TYVJ1427 小白逛公园

    时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述     小新经常陪小白去公园玩,也就是所谓的遛狗啦…在小新家附近有一条“公园路”,路的一边从南到北依次排着n个 ...

  4. bzoj4419[SHOI2013]发微博

    题意:给你一个初始没有边,点权均为0的无向图,三种操作:加边,删边,选择一个点将当前与之相邻的点(不包括自身)的点权+1,询问最后所有点的点权. 据说正解是set维护每个人的朋友,然后考虑每次加边.删 ...

  5. 64位CentOS源码编译方式安装wine

    说明:本文仅作本人笔记的之用,仅供参考.可能因不同环境而不同. 1. 从官网下载最新版的wine-1.6.2.tar.gz 2. 安装相关的包(这里是我安装的,可能由于不同系统已经安装的包不同而不一样 ...

  6. 【Alpha版本】冲刺-Day7

    队伍:606notconnected 会议时间:11月15日 会议总结 张斯巍(433) 今天安排:上传界面设计 完成度:95% 明天计划:回收站界面设计 遇到的问题:无 感想:从一开始界面风格就要确 ...

  7. iOS开发-二维码

    二维码 从ios7开始集成了二维码的生成和读取功能 此前被广泛使用的zbarsdk目前不支持64位处理器 生成二维码的步骤: 倒入CoreImage框架 通过滤镜CIFilter生成二维码 二维码的内 ...

  8. logback 详解

    原创文章,转载请指明出处:http://aub.iteye.com/blog/1103685, 尊重他人即尊重自己 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透 ...

  9. 介绍ping中的TTL是什么意思

    ping是icmp报文的一种应用.用来测试网络中各设备的连通性.在这几天的实验课上,我又用到了这个非常常用的命令,但是这次我发现了一些以前没有太注意的地方,那就是我在Ping不同的地址时所返回的TTL ...

  10. ubuntu删除输入法后,循环登陆

    在登陆界面ctrl+alt+F1进入tty界面,登陆账号,然后输入 dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P 可以参考Ubuntu1 ...