【CodeForces - 574D】Bear and Blocks (dp,思维)

题干:

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.

题目大意:

给你一个长度为N的一串序列,ai表示在这个位子上有一座高度为ai的木块。

每次操作需要去掉所有这样的木块:

一个木块如果上下左右有一个空白位子。

问一共需要操作几次,使得整个序列变成空的。

解题报告:

    其实也可以直接i分别从2和n-1开始,然后初始化的时候把dpl[1]和dpr[n]给初始化了。

    dp是真的难想啊!!有的题解说转化为一个匹配最长递增序列的题目,但我感觉还是用dp更方便理解一些。

    dpl[i]表示从左边开始消除把第i列消完所需的最小时间,dpr表示从右边开始消除的。很明显可以看出来,我要销毁第i列的格子,只有三种方法,要么是左边是空的,要么是右边是空的,要么就是从最顶上一层一层转化过来,也就是用a[i]的时间。只有这三种状态下才可以一次转移到这一个状态,也就是第i列全部被清空,那么我们现在要维护这个最小值,然后再遍历每一个定点,取一个最大值,因为我们需要把所有的盒子都清空,所以我们需要取一个最大值,这个值就是最终的答案。

AC代码:

#include<bits/stdc++.h>using namespace std;
const int MAX = 1e5 + 5;
int a[MAX];
int dpl[MAX],dpr[MAX];
int main()
{int n;cin>>n;for(int i = 1; i<=n; i++) scanf("%d",a+i);dpl[0] = 0;dpr[n+1] = 0;for(int i = 1; i<=n; i++) dpl[i] = min(dpl[i-1] + 1, a[i]);for(int i = n; i>=1; i--) dpr[i] = min(dpr[i+1] + 1, a[i]);int ans = 0;for(int i = 1; i<=n; i++) {ans = max(ans,min(dpl[i],dpr[i]));}cout << ans << endl;return 0 ;} 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441632.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CodeForces - 574B】Bear and Three Musketeers (枚举边,思维,优秀暴力)

题干&#xff1a; Do you know a story about the three musketeers? Anyway, you will learn about its origins now. Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to …

【CodeForces - 574C】Bear and Poker(思维,剪枝,数学)

题干&#xff1a; Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars. Ea…

stm32linux区别,STM32MPU和OpenSTLinux你了解多少?

早在2019年年初的时候&#xff0c;ST就发布了首款STM32MPU&#xff1a;STM32MP1。 STM32MP1通用微处理器产品系列&#xff0c;系基于混合的 双Arm Cortex-A7核 和 Cortex-M4核架构产品。 一、支持STM32MPU 生态系统熟悉 Cortex-M4 MCU 环境的开发人员能轻松实现他们的目标&…

linux安全模式改文件,嵌入式Linux的安全模式设计 - 嵌入式操作系统 - 电子发烧友网...

本系统的架构如下图&#xff1a;产品所使用的flash总大小为16M。系统包括三大部分&#xff0c;即Bootloader&#xff0c;config, kernel rootfs&#xff1a;另外&#xff0c;/dev/mtdblock/0&#xff0c;在系统中对应整个flash block&#xff0c;即整个16M空间。系统启动时&am…

基于arm下的Linux控制,基于ARMuCLinux的网络控制系统设计与实现

引言 随着网络和通信技术的发展&#xff0c;嵌入式系统现已进入高速发展阶段。并在社会各个领域得到了广泛的应用。本文介绍了一种采用ARMuCLinux作为开发平台。实现基于TCP&#xff0f;IP的远程系统监控&#xff0e;从而取代传统单片机来实现数据采集、预处理和通信功能&am…

nodejs 监控linux,linuxServerMonitoring

linux服务器监控平台技术&#xff1a;nodejs vue java mongodb springboot linux shelllinux服务器监控项目&#xff0c;前后端分离vuespringbootmongodb&#xff1a;1、启动前台&#xff1a;使用命令&#xff1a;A 先安装nodejs并配置好环境变量B 先控制台cmd命令切换到项目目…

c语言中short作用,C语言short

C语言short教程C语言short定义详解语法short int varname value;short varname1 value2; //简写形式参数参数描述short int定义 short 类型变量使用的类型。varname变量名。value可选&#xff0c;变量的初始值&#xff0c;该值不可以超过 short 类型的最大值。说明使用 short…

c语言学生对老师的评教系统,学生对老师的评价

学生对老师的评价1、老师授课的方式十分适合我们&#xff0c;他根据本课程知识结构的特点&#xff0c;重点突出&#xff0c;层次分明。理论和实际相结合&#xff0c;透过例题使知识更条理化。但授课速度有点快&#xff0c;来不及记录。2、老师在生活工作中给人的感觉是生活朴素…

【HDU - 1254 】推箱子 (双bfs)

题干&#xff1a; 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙…

知识点 组合数学 卡特兰数

关于卡特兰数 卡特兰数是一种经典的组合数&#xff0c;经常出现在各种计算中&#xff0c;其前几项为 : 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58786, 208012, 742900, 2674440, 9694845, 35357670, 129644790, 477638700, 1767263190, 6564120420, 24466267020, 914…

Android万能遥控菜单选择添加,Android万能遥控器小应用

在很久很久以前&#xff0c;手机是有红外功能的&#xff0c;后来随着蓝牙技术的成熟&#xff0c;红外逐渐被蓝牙取代&#xff0c;不再是标配了。红外本身还是有些优点&#xff0c;比如操作简便&#xff0c;成本低。要想在手机上添加红外功能&#xff0c;就要外接一个转换模块。…

android studio 库工程,Android Studio 添加已有工程方法

准备工作&#xff1a;修改 excluded-paths和android.iml&#xff0c;修改内容详见下图。(目的&#xff1a;过滤和优先在sourcefolder查找&#xff0c;若没有再到JAR包中查找)BorqsUI/LINUX/android/development/tools/idegen$ mm编译出来&#xff1a;[100% 3/3] Install: out/h…

【HDU - 3951】Coin Game (博弈,猜规律,对称博弈)

题干&#xff1a; After hh has learned how to play Nim game, he begins to try another coin game which seems much easier. The game goes like this: Two players start the game with a circle of n coins. They take coins from the circle in turn and every time…

一加6怎么刷android p6,一加6秒速跟进安卓P 教你尝鲜速成开发者

今年5月份&#xff0c;谷歌在I/O开发者大会上发布了全新的Android P操作系统&#xff0c;而在Android P系统发布后的没多久&#xff0c;一加手机官方就公开承诺&#xff0c;年度旗舰一加手机6将会成为首批次升级谷歌Android P系统的机型。而现在&#xff0c;一加已经开始兑现此…

【CodeForces - 1047B 】Cover Points (数学,构造,思维)

题干&#xff1a; There are nn points on the plane, (x1,y1),(x2,y2),…,(xn,yn)(x1,y1),(x2,y2),…,(xn,yn). You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle …

html5引擎笔试题,最新!HTML5经典面试题型(附答案)

HTML已更新至HTML5&#xff0c;那么HTML5的测试题您也应该知道&#xff0c;这篇文章可以作为您的参考。1.doctype有什么作用呢&#xff1f;如何区分其混合模式和标准模式&#xff1f;所有这些都意味着什么&#xff1f;Doctype的作用是告诉浏览器使用HTML规范的哪个版本来渲染文…

html 弹出加载页面,magnific popup:将整个html页面加载到弹出窗口中

我想用弹出的插件在弹出窗口中加载一个完整的html页面。如果我尝试&#xff1a;Edit images$(#edit-images-btn).magnificPopup({type: ajax});它产生了这个&#xff1a;这在图形上非常符合我的要求&#xff0c;但问题是的内容直接插入到dom中&#xff0c;而不是放在保护性的if…

网页html 图片横向摆放,css实现多张图片横向居中显示的方法

先讲一下实现的步骤&#xff1a;最终效果2. 代码实现HTML部分分类小贴士CSS部分.main{width:100%;margin-top:40px;}.main .tag{margin:0 auto;width:200px;font-size:18px;border-bottom:1px solid #878787;text-align:center;margin-bottom:20px;}.main .images{margin:0 aut…

数论中的无数公式 总结

斯特林公式是一条用来取n阶乘近似值的数学公式。一般来说&#xff0c;当n很大的时候&#xff0c;n阶乘的计算量十分大&#xff0c;所以斯特灵公式十分好用&#xff0c;而且&#xff0c;即使在 n很小的时候&#xff0c;斯特灵公式的取值已经十分准确。 公式为&#xff1a; 以下…

用计算机唱出惊雷,除了《惊雷》还有多少喊麦神曲?这十首神作你一定听过!...

一首《惊雷》可以说是火遍了大江南北&#xff0c;一时间风头无二。而这两天杨坤对《惊雷》的diss、惊雷原唱六道的回应更是成为全网热搜&#xff0c;这也让以《惊雷》为首的“喊麦文化”再次进入了公众的视线。事实上除了《惊雷》&#xff0c;还有不少脍炙人口的喊麦神曲&#…