CF1674E.Breaking the Wall

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Monocarp plays "Rage of Empires II: Definitive Edition" — a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall.

The wall consists of nn sections, aligned in a row. The ii-th section initially has durability aia_i. If durability of some section becomes 00 or less, this section is considered broken.

To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager — a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals 22 damage to the target section and 11 damage to adjacent sections. In other words, if the onager shoots at the section xx, then the durability of the section xx decreases by 22, and the durability of the sections x−1x - 1 and x+1x + 1 (if they exist) decreases by 11 each.

Monocarp can shoot at any sections any number of times, he can even shoot at broken sections.

Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him!

Monocarp 正在玩《帝国时代 II:决定版》——一款策略类电脑游戏。此时他正计划在游戏中进攻对手,但 Monocarp 的部队无法进入对手的领地,因为对手已修建了一堵墙。

这堵墙由 nn 个并排排列的段组成。第 ii 段初始耐久度为 aia_i。若某一段的耐久度降至 00 或更低,则该段被视为已被击破。

为了进攻对手,Monocarp 至少需要击破墙上的两段(任意两段均可:可以相邻,也可以不相邻)。为此,他计划使用一种名为“投石机”(onager)的特殊攻城武器。投石机可向墙的任意一段发射;一次射击对目标段造成 22 点伤害,并对其左右相邻段(若存在)各造成 11 点伤害。换言之,若投石机射击第 xx 段,则第 xx 段的耐久度减少 22,而第 x−1x-1 段和第 x+1x+1 段(若存在)的耐久度各减少 11。

Monocarp 可以向任意段发射任意次数的投石机,甚至可以向已被击破的段继续射击。

Monocarp 希望计算出击破至少两段墙所需的最少投石机射击次数。请帮助他!

输入格式

The first line contains one integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the number of sections.

The second line contains the sequence of integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1061 \le a_i \le 10^6), where aia_i is the initial durability of the ii-th section.

第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 表示区段的数量。

第二行包含整数序列 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1061 \le a_i \le 10^6),其中 aia_i 表示第 ii 个区段的初始耐久度。

输出格式

Print one integer — the minimum number of onager shots needed to break at least two sections of the wall.

输出一个整数——摧毁城墙至少两个区段所需的最小投石机射击次数。

输入输出样例

  • 输入#1

    5
    20 10 30 10 20

    输出#1

    10
  • 输入#2

    3
    1 8 1

    输出#2

    1
  • 输入#3

    6
    7 6 6 8 5 8

    输出#3

    4
  • 输入#4

    6
    14 3 8 10 15 4

    输出#4

    4
  • 输入#5

    4
    1 100 100 1

    输出#5

    2
  • 输入#6

    3
    40 10 10

    输出#6

    7

说明/提示

In the first example, it is possible to break the 22-nd and the 44-th section in 1010 shots, for example, by shooting the third section 1010 times. After that, the durabilities become [20,0,10,0,20][20, 0, 10, 0, 20]. Another way of doing it is firing 55 shots at the 22-nd section, and another 55 shots at the 44-th section. After that, the durabilities become [15,0,20,0,15][15, 0, 20, 0, 15].

In the second example, it is enough to shoot the 22-nd section once. Then the 11-st and the 33-rd section will be broken.

In the third example, it is enough to shoot the 22-nd section twice (then the durabilities become [5,2,4,8,5,8][5, 2, 4, 8, 5, 8]), and then shoot the 33-rd section twice (then the durabilities become [5,0,0,6,5,8][5, 0, 0, 6, 5, 8]). So, four shots are enough to break the 22-nd and the 33-rd section.

在第一个例子中,可以通过射击第 3 个区域 10 次,用 10 发子弹击破第 2 和第 4 个区域。之后,各区域的耐久度变为 [20,0,10,0,20][20, 0, 10, 0, 20]。另一种方法是向第 2 个区域射击 5 次,再向第 4 个区域射击 5 次。之后,各区域的耐久度变为 [15,0,20,0,15][15, 0, 20, 0, 15]。

在第二个例子中,仅需向第 2 个区域射击 1 次,即可使第 1 和第 3 个区域被击破。

在第三个例子中,先向第 2 个区域射击 2 次(此时耐久度变为 [5,2,4,8,5,8][5, 2, 4, 8, 5, 8]),再向第 3 个区域射击 2 次(此时耐久度变为 [5,0,0,6,5,8][5, 0, 0, 6, 5, 8]),即可击破第 2 和第 3 个区域。因此,共需 4 发子弹。

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

首页