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.
Examples
input
Copy
代码语言:javascript复制6
2 1 4 6 2 2
output
Copy
代码语言:javascript复制3
input
Copy
代码语言:javascript复制7
3 3 3 1 3 3 3
output
Copy
代码语言:javascript复制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的速度比左右两排消失的速度快,
这道题其实有个地方很神奇,分左右dp然后取最小值,虽然不太理解,但是我递推了一遍感觉是没错的,总之这道题我觉得我没有理解的十分透彻...主要是正确性不是很有把握...
代码语言:javascript复制#include<bits/stdc .h>
using namespace std;
#define ll long long
#define rg register ll
#define inf 2147483647
#define lb(x) (x&(-x))
template <typename T> inline void read(T& x)
{
x=0;char ch=getchar();ll f=1;
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3) (x<<1) (ch^48);ch=getchar();}x*=f;
}
ll n,a[100005],dpz[100005],dpy[100005],ans;
int main()
{
cin>>n;
for(rg i=1;i<=n;i )cin>>a[i];
for(rg i=1;i<=n;i )dpz[i]=min(a[i],dpz[i-1] 1);
for(rg i=n;i>=1;i--)dpy[i]=min(a[i],dpy[i 1] 1);
for(rg i=1;i<=n;i )ans=max(ans,min(dpz[i],dpy[i]));
cout<<ans<<endl;
return 0;
}