573B. Bear and Blocks(Codeforces Round #318)

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.



解题思路:

      假设只取序列的一段进行分析,a[i-1]>a[i]<a[i+1]<a[i+2]<a[i+3],且a[i]是可以操作a[i]次的,那a[i+1]是操作

a[i+1]次,在删除a[i]最上面那个格子的时候,可以把a[i+1]比a[i]多的那部分都删除,同理a[i+2]的操作次数就

是a[i]+2,还有一点需要考虑的是,在序列中间的部分元素是有可能进行a[i]次操作的,于是题目就转化找哪个这类元素到当前位置的操作数最少,有可能那个元素在当前值的前面也可能在后面,于是就考虑分两种情况预处理一下,先考虑那个元素在当前值的前方的时候,cur是前面可以取到a[cur]个操作的位置,当从那个位置到当前位置的操作数大于a[i](i为当前位置),这时候a[i]就可全部取到了。在后面也一样。最后再对每个位置考虑取前和后哪个更小就可以了,可以一列一列的删除的特殊考虑一下就可以了。


代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100000+1000;
int a[maxn];
int b[maxn];
int c[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    int cur=1;
    b[1]=1;
    for(int i=2;i<=n;i++)
    {
        if(a[cur]+i-cur<a[i])
        {
            b[i]=a[cur]+i-cur;
        }
        else
        {
            b[i]=a[i];
            cur=i;
        }
        b[i]=min(i,b[i]);
    }
    c[n]=1;
    cur=n;
    for(int i=n-1;i>0;i--)
    {
       if(a[cur]+cur-i<a[i])
       {
           c[i]=a[cur]+cur-i;
       }
       else
       {
       c[i]=a[i];
       cur=i;
       }
       c[i]=min(n+1-i,c[i]);
    }
    int ans=0,temp;
    for(int i=1;i<=n;i++)
    {
        temp=min(b[i],c[i]);
        ans=max(temp,ans);
    }
    cout<<ans<<endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值