Codeforces Round #318-(D. Bear and Blocks)

Source: Internet
Author: User

Codeforces Round #318-(D. Bear and Blocks)

I came up with this question about brute force. Every time I cut down the layer on the surface until all the heights were 0. But T.

Question:

Now there are n squares, and each square has a height, and then each time we can link the incomplete blocks (that is, the four directions are not fully surrounded) to eliminate them. It takes several times to delete all squares.

Ideas:

We only need to perform one elimination on the left and right, and then make a final judgment. The maximum number of times cannot exceed the maximum height.

First, the initialization is h1 [0] = 0, h2 [n + 1] = 0. We set two arrays h1 to indicate that the maximum height of each time is eliminated from the left, h2 is on the right.

H1 [I] = min (h [I], h1 [I-1] + 1 );

H2 [I] = min (h [I], h2 [I + 1] + 1 );

The two equations should be clear after a moment. Each time, of course, we should satisfy the smaller ones.

 

#include
 
  #include
  
   #include
   
    #include#include
    
     #include
     
      using namespace std;#define inf 99999999#define maxn 100010int h[maxn];int h1[maxn],h2[maxn];int main(){int n;scanf(%d,&n);for(int i=1;i<=n;i++) scanf(%d,&h[i]);int ans=0;h1[0]=0;for(int i=1;i<=n;i++){h1[i]=min(h[i],h1[i-1]+1);}h2[n+1]=0;for(int i=n;i>=1;i--){h2[i]=min(h[i],h2[i+1]+1);}for(int i=1;i<=n;i++){ans=max(ans,min(h1[i],h2[i]));}printf(%d,ans);}
     
    
   
  
 


 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.