Codeforce - 573B - Bear and Blocks

B. Bear and Blocks
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample.

Limak will repeat the following operation till everything is destroyed.

Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time.

Limak is ready to start. You task is to count how many operations will it take him to destroy all towers.

Input
The first line contains single integer n (1 ≤ n ≤ 105).

The second line contains n space-separated integers h1, h2, …, hn (1 ≤ hi ≤ 109) — sizes of towers.

Output
Print the number of operations needed to destroy all towers.

Examples
input
6
2 1 4 6 2 2
output
3
input
7
3 3 3 1 3 3 3
output
2
Note
The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color.

這裏寫圖片描述
After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.

題意:有一面牆,小熊每次把與空氣接觸的磚塊錘掉,求要錘的次數的最大值。

思路:每一列牆所需的次數,與其高度、前後牆的高度、位置有關,即當前次數可能是當前牆的高度,也可能是前一道牆的次數 +1 。或當前次數可能是當前牆的高度,也可能是後一道牆的次數 +1 。
所以可以從左右兩個方向dp,把次數記錄下來,取其小值。

AC代碼:

#include <bits/stdc++.h>
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    int n;cin>>n;
    int a[n+5];
    memset (a,0,sizeof (a));
    for (int i=1;i<=n;i++) cin>>a[i];

    int L[n+5],R[n+5],ans=0;
    memset (L,0,sizeof (L));
    memset (R,0,sizeof (R));
    for (int i=1;i<=n;i++) L[i]=min(a[i],L[i-1]+1);//當前高度與前一個的次數取小*/
    for (int i=n;i;i--) R[i]=min(a[i],R[i+1]+1);/*當前牆的高度與後一道牆的次數取小*/
    for (int i=1;i<=n;i++) ans=max(ans,min(R[i],L[i]));
    cout<<ans;
}
發佈了47 篇原創文章 · 獲贊 28 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章