1. 程式人生 > >#256 (Div. 2)C. Painting Fence

#256 (Div. 2)C. Painting Fence

using paint its fence int () scanf 我們 include

題意:給出籬笆的高度,寬度都是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