首页 > 代码库 > #256 (Div. 2)C. Painting Fence

#256 (Div. 2)C. Painting Fence

题意:给出篱笆的高度,宽度都是1,我们用宽度为1的刷子刷,只能横着或者竖着刷,连续的,问最少多少次全刷完

思路:我们可以全部竖着刷,N次,然后我们如果横着的话就是连续的非0数列中最短的和这个连续的数列都竖着刷比较,DFS

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=5002;
 4 int a[N];
 5 
 6 int n;
 7 
 8 int  dfs(int l,int r){
 9     int Min=1e9+7;
10     int sum=0;
11     if(l>r) return 0;
12     for(int i=l;i<=r;i++){
13         Min=min(Min,a[i]);
14     }
15     for(int i=l;i<=r;i++)
16         a[i]-=Min;
17     sum+=Min;
18   //  cout<<l<<" "<<r<<" "<<sum<<endl;
19     int ll=l;
20     for(int i=l;i<=r;i++){
21         if(a[i]==0){
22             sum+=min(i-ll,dfs(ll,i-1));
23 
24             ll=i+1;
25         }
26     }
27     if(ll<=r){
28         sum+=min(r-ll+1,dfs(ll,r));
29     }
30     return sum;
31 }
32 int main(){
33     scanf("%d",&n);
34     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
35     cout<<min(n,dfs(1,n))<<endl;;
36 
37 }

 

#256 (Div. 2)C. Painting Fence