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.

OJ-ID:
CodeForce 574D

author:
Caution_X

date of submission:
20191019

tags:
dp

description modelling:
给定一个有小正方形组成的不规则图形,现在进行操作:每次都消去暴露在外面的小正方形,问需要几次操作才能消去所有小正方形?

major steps to solve it:
1.dp1[i]:=以第i列为最后一列从前往后可以得到的连续上升子序列
2.dp2[i]:=以第i列为最后一列从后往前可以得到的连续上升子序列
备注:此处连续上升子序列是指可以找到排列成阶梯状的连续上升格子,例如小正方形排列为3 3 3 ,此时仍有连续上升子序列1(3) 2(3) 3
3.对每一列dp取min(dp1,dp2),ans=max(dp[i])

AC code:

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int dp1[],dp2[],a[];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++) {
scanf("%d",&a[i]);
}
dp1[n-]=dp2[n-]=dp1[]=dp2[]=;
for(int i=;i<n;i++) {
dp1[i]=;
if(a[i]>a[i-]) {
dp1[i]=dp1[i-]+;
}
else {
if(dp1[i-]+<=a[i])
dp1[i]=dp1[i-]+;
else dp1[i]=a[i];
}
}
for(int i=n-;i>=;i--) {
dp2[i]=;
if(a[i]>a[i+]) {
dp2[i]=dp2[i+]+;
}
else {
if(dp2[i+]+<=a[i])
dp2[i]=dp2[i+]+;
else dp2[i]=a[i];
}
}
// for(int i=0;i<n;i++) cout<<dp1[i]<<' ';
// cout<<endl;
// for(int i=0;i<n;i++) cout<<dp2[i]<<' ';
// cout<<endl;
int ans=;
for(int i=;i<n;i++){
ans=max(ans,min(dp1[i],dp2[i]));
}
printf("%d\n",ans);
return ;
}

CodeForces 574D Bear and Blocks的更多相关文章

  1. Codeforces 573B Bear and Blocks

    http://codeforces.com/problemset/problem/573/B  题目大意: 给出n个连续塔,每个塔有高度hi,每次取走最外层的块,问需要多少次操作能够拿光所有的块. 思 ...

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

    B. Bear and Blocks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/573/pr ...

  3. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  4. Codeforces 385B Bear and Strings

    题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...

  5. Codeforces 680D Bear and Tower of Cubes 贪心 DFS

    链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...

  6. Codeforces 385C Bear and Prime Numbers(素数预处理)

    Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...

  7. [Codeforces 639F] Bear and Chemistry (Tarjan+虚树)(有详细注释)

    [Codeforces 639F] Bear and Chemistry(Tarjan+虚树) 题面 给出一个n个点,m条边的无向图(不保证连通,可能有自环和重边),有q次询问,每次询问给出p个点和q ...

  8. 【32.89%】【codeforces 574D】Bear and Blocks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. Codeforces Round #318 (Div. 2) D Bear and Blocks (数学)

    不难发现在一次操作以后,hi=min(hi-1,hi-1,hi+1),迭代这个式子得到k次操作以后hi=min(hi-j-(k-j),hi-k,hi+j-(k-j)),j = 1,2,3... 当k ...

随机推荐

  1. PHP JWT token实现

      原文链接:https://www.jb51.net/article/146790.htm   机制:   代码如下:   <?php /**  * PHP实现jwt  */ class Jw ...

  2. Spring中ApplicationListener的使用

    背景 ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制. 如果容器中存在Appl ...

  3. C#判断dataGridView1 点击的是哪一列上的按钮

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { ) { DataGr ...

  4. C#上手练习4(Break、CONITINUE语句)

    C# 中的 continue 语句有点像 break 语句.但它不是强制终止,continue 会跳过当前循环中的代码,强制开始下一次循环. 对于 for 循环,continue 语句会导致执行条件测 ...

  5. Vue响应式原理及总结

    Vue 的响应式原理是核心是通过 ES5 的保护对象的 Object.defindeProperty 中的访问器属性中的 get 和 set 方法,data 中声明的属性都被添加了访问器属性,当读取 ...

  6. 浅入浅出 Go 语言接口的原理

    浅入浅出 Go 语言接口的原理 接口是 Go 语言的重要组成部分,它在 Go 语言中通过一组方法指定了一个对象的行为,接口 interface 的引入能够让我们在 Go 语言更好地组织并写出易于测试的 ...

  7. java 和 spring 的异步

    spring 的 async 注解 https://www.baeldung.com/spring-async@Async will make it execute in a separate thr ...

  8. MongoDB 读偏好设置中增加最大有效延迟时间的参数

    在某些情况下,将读请求发送给副本集的备份节点是合理的,例如,单个服务器无法处理应用的读压力,就可以把查询请求路由到可复制集中的多台服务器上.现在绝大部分MongoDB驱动支持读偏好设置(read pr ...

  9. [Go] gocron源码阅读-go语言中的切片和类型综合

    在gocron.go文件的main函数中,有下面这一句,从这句代码中可以学习到切片和类型的综合运用 cliApp.Flags = append(cliApp.Flags, []cli.Flag{}.. ...

  10. 自动化部署-svn hook触发构建

    目的 之前是通过轮询的形式,2分钟更新一次svn,即时性不高,现在想要实现提交代码时直接触发构建 方案 使用svn的服务器hook,当有代码提交时请求jenkins api实现构建 具体实现 1.je ...