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. Bootstrap --------- 了解与使用

    Bootstrap是用来做什么的?有几大部分?谁开发的?有什么特点? 一个用于快速开发 Web 应用程序和网站的前端框架. 基于 HTML.CSS.JAVASCRIPT 的. 2011 年八月在 Gi ...

  2. 【转载】修改Windows下键盘按键对应功能的一些方案

    原文见:https://sites.google.com/site/xiangyangsite/home/technical-tips/windows-tips/multi_media_key_cus ...

  3. 英语阅读——The confusing pursuit of beauty

    这篇文章是<新视野大学英语>第四册的第二单元的文章,很好的一篇议论文,读起来也很有意思. 1 If you're a man, at some point a woman will ask ...

  4. Nginx反向代理Tomcat静态资源无法加载以及请求链接错误

     在使用Nginx实现Tomcat的负载均衡的时候,项目发布到了Tomcat,Nginx也配置好了, 当访问的时候发现了与预期不符 表现为: 静态资源加载失败 链接跳转地址错误 下面是我错误的配置文件 ...

  5. 表达式树练习实践:C#判断语句

    目录 表达式树练习实践:C#判断语句 if if...else switch ?? 和 ?: 表达式树练习实践:C#判断语句 判断语句 C# 提供了以下类型的判断语句: 语句 描述 if 一个 if ...

  6. 刷抖音太累,教你用Python把高颜值的小姐姐都爬下来慢慢看

    前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 星安果.AirPython 目 标 场 景 相信大家平时刷抖音短视频 ...

  7. Wireshark分析实战:某达速递登录帐号密码提取

    - 准备工作 首先,备好Wireshark,打开,在外网网卡上抓包. 其次,用浏览器访问http://www.yundaex.com/cn/index.php,并在手机上下载安装其APP,找到登录页面 ...

  8. Linux系统快速操作常用快捷键

    快捷键名称 快捷键作用 Ctrl + a 将光标移至行首 Ctrl + e 将光标移至行尾 Ctrl + u 前提光标在行尾,则清除当前行所有的内容(有空格照章清除) Ctrl + k 前提光标在行首 ...

  9. react解析markdown文件

    当当当又get到了一个新技能,使用react-markdown来直接解析markdown文件(咳咳,小菜鸟的自娱自乐) 项目中遇到了一个API的那种展示方式,类似于入门手册啥的那种,如果是一个个调用接 ...

  10. 在python的虚拟环境venv中使用gunicorn

    昨天遇到的问题,一个服务器上有好几个虚拟机环境. 我active进一个虚拟环境,安装了新的三方库之后, 使用gunicorn启动django服务, 但还是死活提示没有安装这个三方库. 一开始没有找到原 ...