200733: [AtCoder]ARC073 F - Many Moves

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $900$ points

Problem Statement

There are $N$ squares in a row. The squares are numbered $1, 2, ..., N$ from left to right.

You have two pieces, initially placed on square $A$ and $B$, respectively. You will be asked to process $Q$ queries of the following kind, in the order received:

  • Given an integer $x_i$, move one of the two pieces of your choice to square $x_i$.

Here, it takes you one second to move a piece one square. That is, the time it takes to move a piece from square $X$ to $Y$ is $|X-Y|$ seconds.

Your objective is to process all the queries in the shortest possible time.

You may only move the pieces in response to queries, and you may not move both pieces at the same time. Also, it is not allowed to rearrange the order in which queries are given. It is, however, allowed to have both pieces in the same square at the same time.

Constraints

  • $1 ≤ N, Q ≤ 200,000$
  • $1 ≤ A, B ≤ N$
  • $1 ≤ x_i ≤ N$

Input

Input is given from Standard Input in the following format:

$N$ $Q$ $A$ $B$
$x_1$ $x_2$ ... $x_Q$

Output

Let the shortest possible time to process all the queries be $X$ seconds. Print $X$.


Sample Input 1

8 3 1 8
3 5 1

Sample Output 1

7

All the queries can be processed in seven seconds, by:

  • moving the piece at square $1$ to $3$
  • moving the piece at square $8$ to $5$
  • moving the piece at square $3$ to $1$

Sample Input 2

9 2 1 9
5 1

Sample Output 2

4

The piece at square $9$ should be moved first.


Sample Input 3

9 2 1 9
5 9

Sample Output 3

4

The piece at square $1$ should be moved first.


Sample Input 4

11 16 8 1
1 1 5 1 11 4 5 2 5 3 3 3 5 5 6 7

Sample Output 4

21

Input

题意翻译

## 题意 在一行中有$n$个格子,从左往右编号为$1$到$n$。 有$2$颗棋子,一开始分别位于位置$A$和$B$。按顺序给出$Q$个要求,每个要求是如下形式: - 给出一个位置$x_i$,要求将两个棋子中任意一个移动到位置$x_i$。 将一颗棋子移动一格需要花费$1$秒,就是说将棋子从$X$位置移动到$Y$位置需要花费$|X-Y|$秒。 为了回答要求,你只能移动棋子,并且同一时刻只能移动一颗棋子。要求的顺序是不可更改的。在同一时间允许两颗棋子在同一个格子内。 ## 输入格式 第一行$4$个整数,分别为$n,Q,A,B$。 第二行$Q$个整数,第$i$个整数为$x_i$。 - $1\leq n,Q\leq 2\times 10^5$ - $1\leq A,B\leq n$ - $1\leq x_i\leq n$ ## 输出格式 最小需要多少秒回答全部要求。

加入题单

算法标签: