QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 64 MB Total points: 100

#13308. 平整地面

Statistics

Byteasar 决定建造一栋房子。他选择了一个非常狭窄的山谷作为地点。在开始实际施工之前,他必须先平整地面。他手头有两台挖掘机:第一台可以将山谷中任意连通区域的地面高度增加或减少恰好 $a$ 米;另一台也可以做同样的事情,即可以将山谷中任意连通区域的地面高度增加或减少恰好 $b$ 米。注意,在进行此类操作之前或之后,受影响区域的地面并不需要处于平整状态。

给定该区域的地图,确定将整个山谷的地面平整(即使所有位置的地面高度都等于 0)所需的最少操作次数。在操作过程中,山谷中任何一点的地面高度可以是任意值;特别地,它可以是负数。

输入格式

标准输入的第一行包含三个整数 $n, a, b$ ($1 \le n \le 100\,000$, $1 \le a, b \le 10^{9}$),以空格分隔。数字 $n$ 表示山谷的长度(单位:米)。第二行包含 $n$ 个整数 $h_{1}, h_{2}, \dots, h_{n}$,以空格分隔。这些数字的绝对值均不超过 $10^{9}$,表示山谷中连续每一米长区域的初始地面高度。

在占总分 30% 的测试中,额外满足:$n, a, b \le 200$ 且 $-200 \le h_{1}, \dots, h_{n} \le 200$。

在占总分 60% 的测试中,额外满足:$n, a, b \le 2\,000$ 且 $-2\,000 \le h_{1}, \dots, h_{n} \le 2\,000$。

在占总分 90% 的测试中,额外满足:$a, b \le 10^{6}$。

输出格式

标准输出的第一行也是唯一一行应打印一个整数:将整个山谷地面平整所需的最少操作次数;如果无法使用给定的挖掘机平整整个山谷,则输出 -1

样例

输入 1

5 2 3
1 2 1 1 -1

输出 1

5

说明

样例输入的一种可行方案是:

  • 将山谷前两米处的地面高度增加 2 米,
  • 将山谷前两米处的地面高度减少 3 米,
  • 将山谷后四米处的地面高度增加 2 米,
  • 将山谷最后一米处的地面高度增加 2 米,
  • 将山谷后四米处的地面高度减少 3 米。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.