博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Bear and Blocks (dp+思维)
阅读量:4965 次
发布时间:2019-06-12

本文共 2312 字,大约阅读时间需要 7 分钟。

Bear and Blocks

 CodeForces - 574D

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.

题意:给你一些由小方块组成的柱子,每次把裸露在外面的小方块即周围有白色方块的小方块消掉,问把这些方块全部消掉需要操作多少次。

思路:先从左到右维护每一个柱子完全消除的最小时间,再从右往左维护一遍,最后取两者的最大值即该柱子被完全消除的时间。

以从左往右为例,一个柱子被完全消除由两个因素决定,即和它相邻的左边的柱子完全需要的时间和该柱子高度。比如说1 4 对于4这个柱子来说,第一次操作消除1和4上面的3个,第二次操作将第二个柱子完全消除,所以l[2]=min(l[1]+1,a[2]);因为对于一个柱子来说,如果它的左侧一直消除不完,或者它的顶部一直消除不完,操作就要一直持续下去。

1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 const int maxn=1e5+10; 7 int a[maxn]; 8 int l[maxn]; 9 int r[maxn];10 int ans=0;11 int n;12 int main()13 {14 cin>>n;15 for(int i=1;i<=n;i++)16 {17 scanf("%d",&a[i]);18 }19 l[0]=0;20 r[n+1]=0;21 for(int i=1;i<=n;i++)22 {23 l[i]=min(l[i-1]+1,a[i]);24 }25 for(int i=n;i>=1;i--)26 {27 r[i]=min(r[i+1]+1,a[i]);28 }29 for(int i=1;i<=n;i++)30 {31 ans=max(ans,min(l[i],r[i]));32 }33 cout<
<

 

转载于:https://www.cnblogs.com/1013star/p/9700490.html

你可能感兴趣的文章
庖丁解牛Linux内核分析 0x00:《庖丁解牛》
查看>>
C语言scanf函数详细解释(转载)
查看>>
【XSY2921】yja 拉格朗日乘法
查看>>
java基本语法——数组
查看>>
编写一个爬虫类库——(一)想法
查看>>
Jquery EasyUI中treegrid的中右键菜单和一般按钮同时绑定事件时的怪异事件
查看>>
test for python thread
查看>>
C++拷贝构造函数与 = 重载
查看>>
新概念4-39
查看>>
(poj)1502 MPI Maelstrom
查看>>
Longest Common Substring SPOJ - LCS (后缀自动机)
查看>>
jsoncpp简介、下载、编译、使用
查看>>
如何通过 AAR 形式集成 leakcanary-android 服务
查看>>
CodeForces Round #527 (Div3) D1. Great Vova Wall (Version 1)
查看>>
xvid x264
查看>>
置顶目录
查看>>
安装vue-cli过程中卡住
查看>>
脚本基础
查看>>
Windows的系统中DLL文件详解
查看>>
sql server 语句
查看>>