#7613. [MCO 2023] Two Pointers (hard version)

[MCO 2023] Two Pointers (hard version)

[MCO 2023] Two Pointers (hard version)

题目描述

Alice and Bob are driving on a very long road that stretches from points $-10^9$ to $10^9$. Alice starts at point $A$ while Bob starts at point $B$. There are $n$ events to visit, where event $i$ is at position $t_i$. Either Alice or Bob must visit each event, but they must be visited $\textbf{in order}$ (they must visit event $1$, then event $2$, then event $3$, $\dots$ then event $n$).

Find the minimum total\textbf{total} distance Alice and Bob can drive to visit all events.

输入格式

The first line contains a single integer $n$ ($1\le n\le3\cdot10^5$) --- the number of events.

The second line contains two integers AA and BB (−109≤A,B≤109-10^9\le A,B\le10^9) --- Alice and Bob's starting points.

The third line contains nn integers t1,t2,…,tnt_1,t_2,\dots,t_n (−109≤ti≤109-10^9\le t_i\le10^9) --- the locations of events either Alice or Bob must get to.

输出格式

Output an integer --- the minimum total distance Alice and Bob drive.
5
2 3
5 1 4 4 7
7
6
540 152
450 600 532 496 325 336
526
8
35 315
-406 -543 114 205 -840 161 540 -731
1699

提示

### Note

In the first example:

  • Bob moves from position 33 to position 55 to attend event 11, driving 22 units.
  • Alice moves from position 22 to position 11 to attend event 22, driving 11 unit.
  • Bob moves from position 55 to position 44 for event 33, driving 11 unit.
  • Bob stays at position 44, attending event 44, driving 00 units.
  • Bob moves from position 44 to position 77 for event 55, driving 33 units.

The total distance travelled is 2+1+1+0+3=72+1+1+0+3=7.

In the second example, Alice visits all events.

Scoring

Subtask 1 (55 points) ∣ti∣,∣A∣≤1000,B=109|t_i|,|A|\le1000,B=10^9

Subtask 2 (88 points) n≤20n\le20

Subtask 3 (1919 points) n≤3000n\le3000

Subtask 4 (1212 points) n≤105,∣ti∣,∣A∣,∣B∣≤100n\le10^5,|t_i|,|A|,|B|\le100

Subtask 5 (4343 points) ∣ti∣,∣A∣,∣B∣≤2⋅105|t_i|,|A|,|B|\le2\cdot10^5

Subtask 6 (1313 points) No additional constraints 标签: P15292|2023|MCC/MCO(马来西亚)

来源

P15292|[MCO 2023] Two Pointers (hard version)