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);}