codeforces 256 div2 C. Painting Fence 分治

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.

Examples
input
5
2 2 1 2 1
output
3
input
2
2 2
output
2
input
1
5
output
1
题意:给你一个栅栏,长度不等,宽度为1,用一个宽为1的刷子刷,最少刷几次;
思路:有点dp的思想,将一个大问题,分成多个小问题,每个问题的解决都一样,计算横着刷,与竖着刷的最小值;
#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define mod 1000000007
#define pi (4*atan(1.0))
const int N=1e2+,M=1e5+,inf=1e9+;
int a[M],ans;
int dfs(int l,int r)
{
int minn=inf;
int sum=;
int shu=(r-l+);
for(int i=l;i<=r;i++)
minn=min(a[i],minn);
for(int i=l;i<=r;i++)
a[i]-=minn;
sum+=minn;
int st=l;
for(int i=l;i<=r;i++)
if(a[i]==)
{
sum+=dfs(st,i-);
st=i+;
}
if(st<=r)
sum+=dfs(st,r);
return min(sum,shu);
}
int main()
{
int x,y,z,i,t;
scanf("%d",&x);
for(i=;i<=x;i++)
scanf("%d",&a[i]);
cout<<dfs(,x)<<endl;
return ;
}
上一篇:Present ViewController Modally


下一篇:painting fence - 分治 - Codeforces 448c