QOJ.ac

QOJ

実行時間制限: 0.8 s メモリ制限: 512 MB 満点: 100

#193. 登山者

統計

山脉可以用一条折线表示,该折线始于海平面(海拔 0),终于海平面(海拔 0),中间的海拔高度均为严格正值(这些被称为内部海拔)。Alice 和 Bob 分别从山脉的两端开始攀登。他们可以在山脉上前后移动,但必须保持在相同的高度(在他们之间)。

Alice 所走路径的努力程度定义为路径上各点海拔绝对差之和。具体来说,如果 Alice 的路径始于海拔 $h_1 = 0$,在海拔 $h_2, h_3, \dots, h_{P-1}$ 处改变方向,并终于海拔 $h_P$,那么该路径的努力程度为 $|h_2-h_1| + |h_3-h_2| + \dots + |h_P-h_{P-1}|$。(由此可知,在此期间 Bob 所付出的努力程度是相等的。)

求 Alice 和 Bob 为了相遇所需要付出的最小努力程度。

输入格式

山脉由一个包含 $N$ 个整数的数组编码,这些整数表示线段端点的高度。第一行包含 $N$。第二行包含 $N$ 个整数。保证第一个和最后一个整数为 0。

输出格式

输出一个整数,表示 Alice 和 Bob 相遇所需的最小努力程度。如果他们无法相遇,则输出 NO

数据范围

  • $3 \le N \le 5,000$
  • 内部海拔在 1 到 1,000,000 之间。

子任务

测试用例将单独计分。

子任务 分值 额外输入限制
1 25% 所有内部海拔互不相同。
2 25% $N \times H \le 40,000$,其中 $H$ 为最高海拔。
3 50%

样例

输入格式 1

5
0 4 2 7 0

输出格式 1

11

说明

Alice 和 Bob 向彼此移动至海拔 4。然后他们都向右移动并下降至海拔 2。最后,他们向彼此移动并在海拔 7 处相遇。

输入格式 2

7
0 10 1 20 5 10 0

输出格式 2

48

说明

Alice 和 Bob 在海拔 10 处相遇。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

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.