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. go-爬虫-百度贴吧(并发版)

    爬取百度贴吧的网页 非并发版 package main import ( "fmt" "io" "net/http" "os&qu ...

  2. 帝国CMS系统目录结构介绍

    帝国CMS目录结构介绍 / 系统根目录├d/            附件和数据存放目录 (data)│├file/       附件存放目录│├js/         JS调用生成目录│└txt/   ...

  3. PlayJava Day005

    今日所学: /* 2019.08.19开始学习,此为补档. */ 类:一类事物的抽象体(如全人类,学生类,订单类) 对象:具体的个体(如张三,某个外卖订单) 对象具有属性和行为. 声明的属性语句一般放 ...

  4. Linux软件安装——服务管理

    Linux软件安装——服务管理 摘要:本文主要学习了Linux中有关服务管理的知识. 什么是服务 服务一般是放置在后台运行的一个或多个进分程,为用户或系统提供某项特定的服务,有些是系统服务,有些则是独 ...

  5. GBT22239-2019等保2.0三级要求

    1 第三级安全要求 1.1 安全通用要求 1.1.1 安全物理环境 1.1.1.1 物理位置选择 本项要求包括: a) 机房场地应选择在具有防震.防风和防雨等能力的建筑内: b) 机房场地应避免设在建 ...

  6. Falling back to java on path. This behavior is deprecated

    windows启动elasticsearch报错:warning: Falling back to java on path. This behavior is deprecated. Specify ...

  7. [MySQL] 解决Error 1698: Access denied for user 'root'@'localhost'

    当程序中使用root账号连接mysql时报以下错误,但是使用命令行是可以正常连接的,那么就查询下mysql数据库的user表,查看下当前用户的密码加密方式,看看是不是unix_socketMariaD ...

  8. K60时钟分析

    转载:https://blog.csdn.net/hcx25909/article/details/7164650 1.飞思卡尔K60时钟系统          飞思卡尔K60时钟系统如上图所示,可以 ...

  9. 21.Java基础_String类

    String类构造方法 package pack1; //推荐使用直接赋值的方式得到字符串 public class test { public static void main(String[] a ...

  10. python,adb,分别给多个设备安装多个apk文件,os.popen(); os.system; os.path.splitext(); a.split(' \t'); readlines(); append(); os.path.join(); time.sleep();

    #encoding:utf-8import os,time#=======================查找手机设备序列号=============a='adb devices'b=os.popen ...