CF-573B(递推)

B. Bear and Blocks
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard 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. Thei-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.

Sample test(s)
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.
 
题意:有一只熊消方块,每次都能消掉最外层的,问多少次能把所有的消完。
思路:因为最底层的方块一定最后消,所以只考虑最底层。对于n个底层方块,ceng[i]表示i个方块的保护层数,即消ceng[i]次后i块变成外层方块。从左到右遍历一边,ceng[i] = min(ceng[i-1]+1, hi-1)。再从右到左遍历一遍,ceng[i] = min(ceng[i],ceng[i+1]+1)。此时ceng[i]即为i块的保护层数。在从右向左遍历的过程中,将保护层数的最大值带出来。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 #define maxn 100010
 6 int n;
 7 int h[maxn];
 8 int ceng[maxn];
 9 
10 int main()
11 {
12     while(~scanf("%d", &n)){
13         memset(ceng, -1, sizeof(ceng));
14         //从左到右遍历。
15         for (int i = 1; i <= n; i++){
16             scanf("%d", &h[i]);
17             ceng[i] = min(h[i]-1, ceng[i-1]+1);
18         }
19         //从右到左遍历,带出最大值。
20         int da = 0;
21         for (int i = n; i >= 1; i--){
22             ceng[i] = min(ceng[i], ceng[i+1]+1);
23             if(ceng[i] > da) da = ceng[i];
24         }
25         //最大的保护层数加一就是最少消次数
26         printf("%d\n", da+1);
27     }
28     return 0;
29 }

 

 
posted @ 2016-02-22 23:21  喷水小火龙  阅读(284)  评论(0)    收藏  举报