CF448C Painting Fence (分治递归)

时间:2023-03-08 20:29:28
CF448C Painting Fence (分治递归)

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 ;
}