Bear and Blocks (dp+思维)

本文详细解析了CodeForces-574D BearandBlocks问题,通过两次遍历维护每个柱子完全消除的最小时间,最终确定完全消除所有塔所需的最少操作次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Bear and Blocks

 CodeForces - 574D

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 4 对于4这个柱子来说,第一次操作消除1和4上面的3个,第二次操作将第二个柱子完全消除,所以l[2]=min(l[1]+1,a[2]);因为对于一个柱子来说,如果它的左侧一直消除不完,或者它的顶部一直消除不完,操作就要一直持续下去。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iostream>
 5 using namespace std;
 6 const int maxn=1e5+10;
 7 int a[maxn];
 8 int l[maxn];
 9 int r[maxn];
10 int ans=0;
11 int n;
12 int main()
13 {
14     cin>>n;
15     for(int i=1;i<=n;i++)
16     {
17         scanf("%d",&a[i]);
18     }
19     l[0]=0;
20     r[n+1]=0;
21     for(int i=1;i<=n;i++)
22     {
23         l[i]=min(l[i-1]+1,a[i]);
24     }
25     for(int i=n;i>=1;i--)
26     {
27         r[i]=min(r[i+1]+1,a[i]);
28     }
29     for(int i=1;i<=n;i++)
30     {
31         ans=max(ans,min(l[i],r[i]));
32     }
33     cout<<ans<<endl;
34 }

 

转载于:https://www.cnblogs.com/1013star/p/9700490.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值