QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 1024 MB Total points: 100

#6343. Bitaro 的旅行

Statistics

JOI 市有一條非常長的道路,可以將它視為實數線。道路上的位置以實數座標表示。JOI 市有 $N$ 個沿著道路分布的景點,依照座標由小到大編號為 $1$ 到 $N$。第 $i$ 個景點($1 \le i \le N$)的座標為 $X_i$。

Bitaro 將造訪 JOI 市的所有景點。由於「貪心」是他的人生座右銘,他會不斷重複以下步驟,直到造訪所有景點。

  • 設 Bitaro 目前的座標為 $x$。在他尚未造訪的景點中,選出與目前位置的距離 $|x-X_i|$ 最小的景點 $i$。接著 Bitaro 移動到景點 $i$ 的位置並造訪它。如果有多個這樣的景點,他會前往其中座標最小的景點。這裡,$|t|$ 表示 $t$ 的絕對值。

然而,多年的經驗讓 Bitaro 知道,如果重複以上步驟移動,總移動距離可能會比預期更長。由於總移動距離會隨起始座標而改變,他想知道分別從 $Q$ 個候選起始座標 $S_1,S_2,\ldots,S_Q$ 出發,直到造訪所有景點為止的總移動距離。

為了幫助 Bitaro,請根據 JOI 市的資訊與候選起始座標,撰寫程式,計算他分別從每個候選起始座標出發時的總移動距離。

輸入格式

從標準輸入讀取以下資料。

$$ \begin{array}{llll} N \\ X_1 & X_2 & \cdots & X_N \\ Q \\ S_1 \\ S_2 \\ \vdots \\ S_Q \end{array} $$

輸出格式

向標準輸出寫出 $Q$ 行。輸出第 $j$ 行($1 \le j \le Q$)應包含 Bitaro 從座標 $S_j$ 出發時的總移動距離。

資料範圍

  • $1 \le N \le 200\,000$。
  • $1 \le Q \le 200\,000$。
  • $0 \le X_i \le 10^9$($1 \le i \le N$)。
  • $X_i<X_{i+1}$($1 \le i \le N-1$)。
  • $0 \le S_j \le 10^9$($1 \le j \le Q$)。
  • 所有給定的值皆為整數。

子任務

  1. (5 分)$Q=1$,$N \le 2\,000$。
  2. (10 分)$Q=1$。
  3. (30 分)$X_{i+1}-X_i \le 100$($1 \le i \le N-1$)。
  4. (55 分)無額外限制。

範例

輸入格式 1

5
0 5 6 7 9
1
7

輸出格式 1

15

說明

如果 Bitaro 從座標 $7$ 出發,他會依照以下方式造訪所有景點。

  1. 他尚未造訪景點 $1,2,3,4,5$,這些景點與他目前位置的距離依序為 $7,2,1,0,2$。由於景點 $4$ 距離 Bitaro 最近,他停留在座標 $7$,並造訪景點 $4$。
  2. 他尚未造訪景點 $1,2,3,5$,這些景點與他目前位置的距離依序為 $7,2,1,2$。由於景點 $3$ 距離 Bitaro 最近,他從座標 $7$ 移動到座標 $6$,並造訪景點 $3$。
  3. 他尚未造訪景點 $1,2,5$,這些景點與他目前位置的距離依序為 $6,1,3$。由於景點 $2$ 距離 Bitaro 最近,他從座標 $6$ 移動到座標 $5$,並造訪景點 $2$。
  4. 他尚未造訪景點 $1,5$,這些景點與他目前位置的距離依序為 $5,4$。由於景點 $5$ 距離 Bitaro 最近,他從座標 $5$ 移動到座標 $9$,並造訪景點 $5$。
  5. 他尚未造訪景點 $1$。由於景點 $1$ 距離 Bitaro 最近,他從座標 $9$ 移動到座標 $0$,並造訪景點 $1$。

由於 Bitaro 的總移動距離為 $15$,因此輸出 $15$。

此範例輸入滿足所有子任務的限制。

輸入格式 2

10
1 2 3 4 5 6 7 8 9 10
10
1
2
3
4
5
6
7
8
9
10

輸出格式 2

9
10
11
12
13
14
15
16
17
9

說明

此範例輸入滿足子任務 $3,4$ 的限制。

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.