QOJ.ac

QOJ

時間限制: 2 s 記憶體限制: 256 MB 總分: 100 可 Hack ✓

#18673. 만보기 대행 서비스

统计

在現今的社會中,有許多應用科技(App Tech)服務讓人們可以透過執行日常生活中的任務來獲得點數,並兌換小獎品。計步器任務是許多應用科技服務廣泛使用的類型,每天成功行走 $D$ 公尺就能獲得少量點數。

由於每天走 $D$ 公尺其實相當麻煩,對於懶得親自執行任務的人,韓別決定創立一間名為「Startup Hanbyeol」的新創公司,專門代辦計步器任務。Startup Hanbyeol 首先在一條東西向的長直道路上,以 $1$ 公尺的間隔設置了儲物櫃,並賦予整數編號,這條路經過 Startup Hanbyeol 的辦公大樓。從辦公大樓向東 $A$ 公尺的儲物櫃編號為 $A$,向西 $A$ 公尺的儲物櫃編號為 $-A$,位於辦公大樓的儲物櫃編號為 $0$。

你必須從 Startup Hanbyeol 辦公大樓出發,執行所有客戶的任務,然後返回公司。在你開始工作前,所有客戶都已經將手機放在編號 $X_i$ 的儲物櫃中。你必須走到 $X_i$ 號儲物櫃親自拿起手機,拿起後必須移動至少 $D$ 公尺,然後將手機歸還到 $X_i$ 號儲物櫃。你有一個足夠大的背包,可以同時攜帶多支手機移動。你的移動記錄會作為工作記錄,因此你只能在道路上移動。

請寫出一個程式,計算出執行完所有客戶的任務並返回公司所需的最小移動距離。

輸入格式

第一行給定客戶數量 $N$ 與執行任務所需的最小移動距離 $D$,以空格分隔。($1 \leq N \leq 1\,000\,000$;$1 \leq D \leq 10^9$)

第二行給定 $N$ 個整數 $X_i$,表示每個客戶放置手機的儲物櫃編號,以空格分隔。($-10^9 \leq X_i \leq 10^9$)

手機的位置可能彼此重疊,也可能與 Startup Hanbyeol 辦公大樓相同。

所有輸入值皆為整數。

輸出格式

第一行輸出執行完 $N$ 位客戶的所有任務並返回所需的最小移動距離。

若答案不是整數,則輸出小於或等於答案的最大整數。

範例

輸入格式 1

3 5
-8 1 5

輸出格式 1

36

說明

以下方法是最佳的:

  1. 拿起第 $2$ 位客戶的手機。
  2. 拿起第 $3$ 位客戶的手機。
  3. 移動到 Startup Hanbyeol 辦公大樓東側 $7.5$ 公尺處,然後返回並歸還第 $3$ 位客戶的手機。
  4. 歸還第 $2$ 位客戶的手機。
  5. 拿起第 $1$ 位客戶的手機。
  6. 移動到 Startup Hanbyeol 辦公大樓西側 $10.5$ 公尺處,然後返回並歸還第 $1$ 位客戶的手機。
  7. 返回 Startup Hanbyeol 辦公大樓。

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.