time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard 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

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.

【题目链接】:http://codeforces.com/contest/574/problem/D

【题解】



设l[i]表示把从左到右把第i列完全消去需要的操作数;

h[i]次操作是肯定能够把第i列消去的;因为这一列的最上面那个方块总是暴露出来的;

l[i-1]+1

第i列消掉之后可以直接把第i列消去,因为i-1列完全消掉之后,第i列整个左面就暴露出来了;如何保证消掉第i列一定要+1?第i-1列消掉用了l[i-1]次操作,如果l[i-1]>=h[i]则不用加1,因为在消第i-1列的时候就顺带把第i列消掉了;事实上这种情况l[i]=h[i];否则的话

l[i-1] < h[i];则先操作l[i-1]次把i-1这一列完全消掉;然后第i列剩下的方块左边全部暴露出来;剩下的就可用一次操作完全消掉了;所以是l[i-1]+1;

即l[i] =(l[i-1]+1,h[i])min

边界l[1] = 1;

但这样只考虑了左面暴露的情况;

有可能右面暴露更优;

比如

h[] = {1,2,3,4,5};

只考虑从左到右的话,l[] = {1,2,3,4,5};

但是消掉第5列显然只要1次,消掉第4列显然只要两次操作;

所以咱们从右到左也进行相同的DP;

处理出r[];

最后每个位置i都取min即可;

然后所有位置的最大值就是答案(所有的列都要消掉,时间最长的就是答案);



【完整代码】

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e5+10;

int n,h[MAXN],l[MAXN],r[MAXN];

int main()
{
//freopen("F:\\rush.txt","r",stdin);
scanf("%d",&n);
for (int i = 1;i <= n;i++)
scanf("%d",&h[i]);
l[1] = 1;
for (int i = 2;i <= n;i++)
if (l[i-1]>=h[i])
l[i] = h[i];
else
l[i] = l[i-1]+1;
r[n] = 1;
for (int i = n-1;i >= 1;i--)
if (r[i+1] >= h[i])
r[i] = h[i];
else
r[i] = r[i+1]+1;
int ans = 1;
for (int i = 1;i <=n;i++)
ans = max(ans,min(l[i],r[i]));
cout << ans << endl;
return 0;
}

【32.89%】【codeforces 574D】Bear and Blocks的更多相关文章

  1. 【32.89%】【codeforces 719A】Vitya in the Countryside

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. 【32.22%】【codeforces 602B】Approximating a Constant Range

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【74.89%】【codeforces 551A】GukiZ and Contest

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【29.89%】【codeforces 734D】Anton and Chess

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【codeforces 807D】Dynamic Problem Scoring

    [题目链接]:http://codeforces.com/contest/807/problem/D [题意] 给出n个人的比赛信息; 5道题 每道题,或是没被解决->用-1表示; 或者给出解题 ...

  7. 【81.37%】【codeforces 734B】Anton and Digits

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

  8. [CodeForces - 1225D]Power Products 【数论】 【分解质因数】

    [CodeForces - 1225D]Power Products [数论] [分解质因数] 标签:题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory ...

  9. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

随机推荐

  1. Numpy库进阶教程(一)求解线性方程组

    前言 Numpy是一个很强大的python科学计算库.为了机器学习的须要.想深入研究一下Numpy库的使用方法.用这个系列的博客.记录下我的学习过程. 系列: Numpy库进阶教程(二) 正在持续更新 ...

  2. SVN—怎样安装SVNclient软件

            一.怎样安装TortoiseSVN-1.7.12.24070-win32-svn-1.7.9版本号的SVNclient软件:        a.下载TortoiseSVN-1.7.12 ...

  3. [CSS3] Create a fixed-fluid-fixed layout using CSS calc()

    CSS calc() allows you to mix and match units to get real-time calculations. It's useful when you nee ...

  4. Snail—Hibernate各种异常

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXExNzkxNDIyMDE4/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  5. watchdog的正确使用方法

    关于watchdog应该有过单片机学习经历的人.都比較熟悉.但watchdog的正确使用方法,恐怕大家假设没有经历过实际产品的开发不会有深入的理解. 瑞萨RL78系列的单片机自身带有watchdog, ...

  6. 前端项目课程3 jquery1.8.3到1.11.1有了哪些新改变

    web项目课程3  jquery1.8.3到1.11.1有了哪些新改变 一.总结 一句话总结:领会官方升级的意思.  1.live();    2.die();    3.bind();    4.u ...

  7. linux系统进程的查看与控制

    原文:linux系统进程的查看与控制 一.什么是进程? 进程就是系统未完成并且正在进行的工作. 二.查看系统进程 1.图形方式查看 gnome-system-monitor 2.进程查看命令 ps - ...

  8. 【Codeforces Round #439 (Div. 2) B】The Eternal Immortality

    [链接] 链接 [题意] 求b!/a!的最后一位数字 [题解] b-a>=20的话 a+1..b之间肯定有因子2和因子5 答案一定是0 否则暴力就好 [错的次数] 在这里输入错的次数 [反思] ...

  9. 【record】11.14..11.20

    balabala

  10. 根据PID获取进程名&根据进程名获取PID

    Liunx中 通过进程名查找进程PID可以通过 pidof [进程名] 来查找.反过来 ,相同通过PID查找进程名则没有相关命令.在linux根目录中,有一个/proc的VFS(虚拟文件系统),系统当 ...