아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Two Pointers (easy version)

면접 대비

시간 제한1초메모리 제한1024 MB

요약
직선 위 A와 B에서 각각 출발하는 두 사람이 모든 도시를 하나 이상 방문할 때, 두 사람이 이동한 거리의 합의 최솟값을 구한다.
난이도

보통10점 중 7점

유형
그리디, 정렬, 수학, 투 포인터
정답자
아직 제출이 없습니다

문제

Alice and Bob are visiting cities on a very long road that stretches from points −109-10^9 to 10910^9. Alice starts at point AA while Bob starts at point BB.

There are nn cities to visit, where the ii-th city is at point t_it\_i. Each city must be visited by Alice or Bob at least once, but they can be visited in any order.

What is the minimum total distance Alice and Bob travel?

입력

Each test consists of multiple test cases. The first line contains a single integer TT (1≤T≤1001 \le T \le 100), the number of test cases. Each test case is formatted as follows:

The first line contains three space-separated integers nn, AA, and BB (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5, −109≤A,B≤109-10^9 \le A, B \le 10^9) -- the number of cities, Alice's position, and Bob's position, respectively.

The second line contains nn space-separated integers t_1,t_2,…,t_nt\_1, t\_2, \ldots, t\_n (−109≤t_i≤109-10^9 \le t\_i \le 10^9) -- the positions of the cities.

It is guaranteed that the sum of nn over all test cases is at most 2⋅1052 \cdot 10^5.

출력

For each test case, print the answer on a separate line.

Output the minimum total distance that Alice and Bob must travel to visit all cities.

힌트

In the first test case: There are 77 cities. Alice starts at coordinate −6-6 and Bob starts at point 1010.

One possible optimal way to visit all cities is as follows (i→xji \xrightarrow{x} j means to go from ii to jj, driving xx distance):

  • Alice visits the cities (given in order): A→0city 6→9city 1A \xrightarrow{0} \text{city }6 \xrightarrow{9} \text{city }1.
  • Bob visits the cities (given in order): B→1city 5→1city 3→4city 4→8city 7→1city 2B \xrightarrow{1} \text{city }5 \xrightarrow{1} \text{city }3 \xrightarrow{4} \text{city }4 \xrightarrow{8} \text{city }7 \xrightarrow{1} \text{city }2.

Alice drives for a total of 0+9=90 + 9 = 9 distance and Bob drives for a total of 1+1+4+8+1=151 + 1 + 4 + 8 + 1 = 15 distance. The total distance driven by both Alice and Bob is 9+15=249 + 15 = 24. It can be proven that there is no way to drive less than 2424 distance, thus the answer is 2424.

In the second test case, Alice and Bob are both already at city 22. Bob can visit the city 22 then city 11, driving 2,000,000,0002,000,000,000 total distance. Note that Alice can choose to do nothing.

In the third test case, Alice can visit the only city, driving from point 44 to point 11 for 33 distance. Bob does nothing.

예제1

  1. 예제 1

    입력
    4
    7 -6 10
    -15 -1 12 8 11 -6 0
    2 -1000000000 -1000000000
    1000000000 -1000000000
    1 4 6
    1
    4 727 137
    39 852 201 696
    
    예상 출력
    24
    2000000000
    3
    413