A14982 | Breaking the Wall
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $n$ sections, aligned in a row. The $i$ -th section initially has durability $a_i$ . If durability of some section becomes $0$ 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 $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$ , then the durability of the section $x$ decreases by $2$ , and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ 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!
The wall consists of $n$ sections, aligned in a row. The $i$ -th section initially has durability $a_i$ . If durability of some section becomes $0$ 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 $2$ damage to the target section and $1$ damage to adjacent sections. In other words, if the onager shoots at the section $x$ , then the durability of the section $x$ decreases by $2$ , and the durability of the sections $x - 1$ and $x + 1$ (if they exist) decreases by $1$ 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!
输入格式
The first line contains one integer $n$ ( $2 \le n \le 2 \cdot 10^5$ ) — the number of sections.
The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 10^6$ ), where $a_i$ is the initial durability of the $i$ -th section.
The second line contains the sequence of integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 10^6$ ), where $a_i$ is the initial durability of the $i$ -th section.
输出格式
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 $2$ -nd and the $4$ -th section in $10$ shots, for example, by shooting the third section $10$ times. After that, the durabilities become $[20, 0, 10, 0, 20]$ . Another way of doing it is firing $5$ shots at the $2$ -nd section, and another $5$ shots at the $4$ -th section. After that, the durabilities become $[15, 0, 20, 0, 15]$ .
In the second example, it is enough to shoot the $2$ -nd section once. Then the $1$ -st and the $3$ -rd section will be broken.
In the third example, it is enough to shoot the $2$ -nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$ ), and then shoot the $3$ -rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$ ). So, four shots are enough to break the $2$ -nd and the $3$ -rd section.
In the second example, it is enough to shoot the $2$ -nd section once. Then the $1$ -st and the $3$ -rd section will be broken.
In the third example, it is enough to shoot the $2$ -nd section twice (then the durabilities become $[5, 2, 4, 8, 5, 8]$ ), and then shoot the $3$ -rd section twice (then the durabilities become $[5, 0, 0, 6, 5, 8]$ ). So, four shots are enough to break the $2$ -nd and the $3$ -rd section.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted