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. 【codeforces 733F】 Drivers Dissatisfaction

    http://codeforces.com/problemset/problem/733/F (题目链接) 题意 给出一张n个点的无向图,每一条变有两个特征值:${w,c}$:分别表示这条边的权值为$ ...

  2. PHP Fatal error: Call to undefined function mb_substr()

    Lamp架构 PHP 5.3.29 #查看php是否有mbstring模块 php -m | grep mbstring yum install php-mbstring -y find / -nam ...

  3. PHPstorm激活

    最近想学习一下PHP 于是下载了很不错的phpstorm  但这老外的工具是要购买正版的 所以就搜了一下破解激活的教程 发现现在网上的在线破解在2016.2版本里面大多已被封杀 尝试了本地破解也发现大 ...

  4. CF 161B Discounts(贪心)

    题目链接: 传送门 Discounts time limit per test:3 second     memory limit per test:256 megabytes Description ...

  5. BZOJ1047: [HAOI2007]理想的正方形

    传送门 蛤省省选果然水啊,我这种蒟蒻都能一遍A. 横向纵向维护两个单调队列,做两次求最大和最小的,总复杂度$O(NM)$ 码农题,考察代码实现能力 //BZOJ 1047 //by Cydiater ...

  6. Docker入门教程(六)另外的15个Docker命令

    Docker入门教程(六)另外的15个Docker命令 [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第六篇,继续介绍Docker命令.之前的第二篇文章 ...

  7. Ubuntu 12.04 root账户开启及密码重设

    以普通用户登录,root账号的开启.关闭和密码设置,命令如下: sudo passwd -u root # 启用root账户 sudo passwd root # 设置root 密码(包括重设) su ...

  8. 【原】react+redux实战

    摘要:因为最近搞懂了redux的异步操作,所以觉得可以用react+redux来做一个小小的项目了,以此来加深一下印象.切记,是小小的项目,所以项目肯定是比较简单的啦,哈哈. 项目效果图如图所示:(因 ...

  9. WinForm------弹出MessageBox窗口的同时隐藏当前窗口

    private void Btn_OK_Click(object sender, EventArgs e) { this.Hide(); //隐藏当前窗口 MessageBox.Show(" ...

  10. 单个pdf提取测试

    # -*- coding: utf-8 -*- """ Created on Wed Feb 3 09:32:22 2016 pdf单个文件提取测试 @author: A ...