前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 1) B. Bear and Blocks (技巧dp 难想)

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

作者头像
glm233
发布2020-09-28 14:54:30
3990
发布2020-09-28 14:54:30
举报
文章被收录于专栏:glm的全栈学习之路

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;
    
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/03/19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档