codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1585C.md (2446B)


      1 Source: https://codeforces.com/problemset/problem/1585/C
      2 
      3 C. Minimize Distance
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 A total of $n$ depots are located on a number line. Depot $i$ lies at the point $x_i$ for $1 \le i \le n$.
     22 
     23 You are a salesman with $n$ bags of goods, attempting to deliver one bag to each of the $n$ depots. You and the $n$ bags are initially at the origin $0$. You can carry up to $k$ bags at a time. You must collect the required number of goods from the origin, deliver them to the respective depots, and then return to the origin to collect your next batch of goods.
     24 
     25 Calculate the minimum distance you need to cover to deliver all the bags of goods to the depots. You do not have to return to the origin after you have delivered all the bags.
     26 
     27 Input
     28 
     29 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10\,500$). Description of the test cases follows.
     30 
     31 The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$).
     32 
     33 The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($-10^9 \le x_i \le 10^9$). It is possible that some depots share the same position.
     34 
     35 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     36 
     37 Output
     38 
     39 For each test case, output a single integer denoting the minimum distance you need to cover to deliver all the bags of goods to the depots.
     40 
     41 Example
     42 
     43 Input
     44 
     45 ```
     46 4
     47 5 1
     48 1 2 3 4 5
     49 9 3
     50 -5 -10 -15 6 5 8 3 7 4
     51 5 3
     52 2 2 3 3 3
     53 4 2
     54 1000000000 1000000000 1000000000 1000000000
     55 ```
     56 
     57 Output
     58 
     59 ```
     60 25
     61 41
     62 7
     63 3000000000
     64 ```
     65 
     66 Note
     67 
     68 In the first test case, you can carry only one bag at a time. Thus, the following is a solution sequence that gives a minimum travel distance: $0 \to 2 \to 0 \to 4 \to 0 \to 3 \to 0 \to 1 \to 0 \to 5$, where each $0$ means you go the origin and grab one bag, and each positive integer means you deliver the bag to a depot at this coordinate, giving a total distance of $25$ units. It must be noted that there are other sequences that give the same distance.
     69 
     70 In the second test case, you can follow the following sequence, among multiple such sequences, to travel minimum distance: $0 \to 6 \to 8 \to 7 \to 0 \to 5 \to 4 \to 3 \to 0 \to (-5) \to (-10) \to (-15)$, with distance $41$. It can be shown that $41$ is the optimal distance for this test case.