n个木板,宽都为1 , 长为a1 ,a2 ,...an 。 刷子宽为1 , 可以横或者竖去刷 , 但是木板要连在一块可以同时刷。 问最少需要刷几次。
case
input
5 2 2 1 2 1
output
3
input
2 2 2
output
2
input
1 5
output
1
分治。 每次区间【L, R】 找最短的板子高为t, 则下面刷一下t次 ,可将【L, R】 一份为2 。
const int maxn = 5008 ;
int x[maxn] ;
int gao(int l , int r , int h){
if(l > r) return 0 ;
if(l == r) return h >= x[l] ? 0 : 1 ;
int t = x[l] - h , pot = l ;
for(int i = l ; i <= r ; i++){
if(x[i] - h < t){
t = x[i] - h ;
pot = i ;
}
}
return min(r-l+1 , t + gao(l , pot-1 , t+h) + gao(pot+1 , r , t+h)) ;
}
int main(){
int n , i ;
cin>>n ;
for(i = 1 ; i <= n ; i++) scanf("%d" , &x[i]) ;
cout<< gao(1 , n , 0) << endl ;
return 0 ;
}