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. 转载:点云上实时三维目标检测的欧拉区域方案 ----Complex-YOLO

    感觉是机器翻译,好多地方不通顺,凑合看看 原文名称:Complex-YOLO: An Euler-Region-Proposal for  Real-time 3D Object Detection ...

  2. Spring Boot配置过滤器的两种方式

    过滤器(Filter)是Servlet中常用的技术,可以实现用户在访问某个目标资源之前,对访问的请求和响应进行拦截,常用的场景有登录校验.权限控制.敏感词过滤等,下面介绍下Spring Boot配置过 ...

  3. 在生成.net core 3.0程序时不包含nuget库

    在.net core 3.0中,默认的生成行为有了两个变化,一个是默认生成可执行文件,另一个是会复制依赖的Nuget项. 前一个变化会默认生成可执行的exe,用起来更加方便了.另一个变化可能是为了减少 ...

  4. 如何访问到静态的文件,如jpg,js,css.

    如果你的DispatcherServlet拦截"*.do"这样的有后缀的URL,就不存在访问不到静态资源的问题. 如果你的DispatcherServlet拦截"/&qu ...

  5. Java日期时间API系列1-----Jdk7及以前的日期时间类

    先看一个简单的图: 主要的类有: Date类负责时间的表示,在计算机中,时间的表示是一个较大的概念,现有的系统基本都是利用从1970.1.1 00:00:00 到当前时间的毫秒数进行计时,这个时间称为 ...

  6. sockjs+stomp的websocket插件

    /** * 依赖文件sockjs.js.stomp.js * */ ;!(function (window) { 'use strict' let WS = function () { //保存所有的 ...

  7. kali linux查看局域网下所有IP,并对指定IP实施局域网内攻击(断网,随时查看对方密码,上网痕迹等)

    首先我们打开我们熟悉的kali linux操作系统,利用指令: ifconfig 来确认本机的ip地址 确认了本机的ip地址之后,利用一下的指令查看局域网下所有ip: fping -g 本机IP地址/ ...

  8. 【LeetCode】TreeNode类实现解析(java实现)

    https://blog.csdn.net/styshoo/article/details/52865386 在LeetCode中,TreeNode是经常用到的一个结构体,表示数据结构树(Tree)中 ...

  9. TypeScript 学习笔记(三)

    类: 1.TypeScript 是面向对象的 JavaScript,类描述了创建的对象共同的属性和方法 2.类通过关键字 class 声明,使用 extends 关键字进行继承 3.在引用一个类成员时 ...

  10. Python—下载安装与使用

    安装依赖包 首先安装gcc编译器,编译时需要使用gcc.gcc有些系统版本已经默认安装,通过  gcc --version  查看,没安装的先安装 [root@localhost ~]# yum -y ...