CF573B.Bear and Blocks

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 h__i 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.

Limak 是一只可爱的小熊,热爱玩耍。今天,他正在通过摧毁积木塔来玩耍。他在一行中搭建了 nn 座塔。第 ii 座塔由 hih_i 个完全相同的积木组成。具体示意图参见第一个样例的图片。

Limak 将不断重复以下操作,直到所有塔都被彻底摧毁。

若一个积木块具有全部四个邻接块(即其上、左、下、右四个方向均与另一个积木块或地面相邻),则称该积木块为内部块;否则称为边界块。在一次操作中,Limak 会摧毁所有边界块。他的爪子非常迅速,能够同时摧毁所有这些边界块。

Limak 已准备就绪,即将开始。你的任务是计算他摧毁所有塔所需的总操作次数。

输入格式

The first line contains single integer n (1 ≤ n ≤ 105).

The second line contains n space-separated integers _h_1, _h_2, ..., h__n (1 ≤ h__i ≤ 109) — sizes of towers.

第一行包含一个整数 nn(1≤n≤1051 \leq n \leq 10^5)。

第二行包含 nn 个以空格分隔的整数 h1,h2,…,hnh_1, h_2, \dots, h_n(1≤hi≤1091 \leq h_i \leq 10^9)—— 各塔的高度。

输出格式

Print the number of operations needed to destroy all towers.

输出摧毁所有塔所需的运算次数。

输入输出样例

  • 输入#1

    6
    2 1 4 6 2 2

    输出#1

    3
  • 输入#2

    7
    3 3 3 1 3 3 3

    输出#2

    2

说明/提示

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.

下图展示了第一个样例测试中的全部三种操作。每次操作中,边界方块均以红色标出。

第一次操作后剩余四个方块,第二次操作后仅剩一个方块。该最后一个方块在第三次操作中被摧毁。

输入解题思路,AI测评打分。不知道怎么写?

首页