Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) B. Bear and Blocks dp

108 篇文章 0 订阅

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. 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.

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次所以复杂度为o(n * n);我们可以观察每列最大步数是多少。我们可以发现,每列一步要么减1层,要么是min(h[i-1],h[i+1],所以一步,要使得h[i]变为,min(h[i] -1,h[i-1],h[i+1]),r[i]表示用的次数,那么,每i列用的次数,也就是min(h[i],r[i-1],r[i+1]);所 以用dp的思想,从前向后,再从后向前推一遍就可以了。复杂度为O(n);

#define N 100005
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,h[N],maxx;
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
     while(S(n)!=EOF)
    {
        FI(n) S(h[i]);
        h[0] = 1;
        For(i,1,n){
            h[i] = min(h[i],h[i-1] + 1);
        }
        h[n-1] = 1;
        for(int i = n - 2;i>=0;i--){
            h[i] = min(h[i],h[i+1] + 1);
        }
        maxx = 0;
        FI(n) maxx = max(maxx,h[i]);
        printf("%d\n",maxx);
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值