QOJ.ac

QOJ

実行時間制限: 1.0 s メモリ制限: 256 MB 満点: 100 ハック可能 ✓

#10845. 移动盒子

統計

在一个大型仓库中,有 $n$ 个箱子排成一行。每个箱子 $i$ 最初位于坐标 $x_i$ 处。由于重组计划,每个箱子都需要被移动到新的位置 $y_i$。

为了协助这次重组,你拥有一台专用机器人。该机器人以每秒一个单位长度的速度移动,并且在任何时候只能携带一个箱子。机器人拾取和放下箱子所需的时间可以忽略不计,但改变方向需要 $C$ 秒。最初,你可以将机器人放置在任何位置并使其面向任何方向。但在所有箱子重新安置后,机器人必须返回其初始位置和方向。箱子不需要一次性全部运送到目的地,这意味着它们可以在中途放下。此外,在移动过程中,多个箱子可以同时存在于同一个位置。

你的挑战是制定机器人的移动策略,使得重新安置所有箱子的总时间最小化。请计算机器人重新安置所有箱子所需的最短总时间。

输入格式

第一行包含两个整数 $n$ 和 $C$ ($1 \le n \le 10^5$, $1 \le C \le 10^9$),分别表示箱子的数量和改变方向所需的时间成本。

接下来的 $n$ 行,每行包含两个整数 $x_i$ 和 $y_i$ ($1 \le x_i, y_i \le 10^9, x_i \neq y_i$),表示箱子 $i$ 的初始位置和目标位置。保证所有 $x_i$ 互不相同,且所有 $y_i$ 互不相同。

输出格式

输出一个整数,表示机器人重新安置所有箱子所需的最短总时间。

样例

样例输入 1

3 1
1 2
4 6
5 3

样例输出 1

12

样例输入 2

3 4
5 10
9 1
8 6

样例输出 2

38

样例输入 3

4 1
1 1001
1002 2
3 1003
1004 4

样例输出 3

4008

说明

样例 2:从位置 1 出发,面向正方向。移动到位置 5。拾取箱子 1。移动到位置 8。放下箱子 1。拾取箱子 3。改变方向。移动到位置 6。放下箱子 3。改变方向。移动到位置 8。拾取箱子 1。移动到位置 10。放下箱子 1。改变方向。移动到位置 9。拾取箱子 2。移动到位置 1。放下箱子 2。改变方向。总耗时为 38 秒。

样例 3:从位置 3 出发,面向正方向。拾取箱子 3。移动到位置 1003。放下箱子 3。移动到位置 1004。拾取箱子 4。改变方向。移动到位置 1002。放下箱子 4。拾取箱子 2。移动到位置 2。放下箱子 2。移动到位置 1。拾取箱子 1。改变方向。移动到位置 1001。放下箱子 1。移动到位置 1002。拾取箱子 4。改变方向。移动到位置 4。放下箱子 4。移动到位置 3。改变方向。总耗时为 4008 秒。

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.