codeforces-scrape

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

2127C.md (2911B)


      1 Source: https://codeforces.com/problemset/problem/2127/C
      2 
      3 C. Trip Shopping
      4 
      5 time limit per test
      6 
      7 2 seconds
      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 Ali and Bahamin decided to spend their summer vacation on the beautiful southern coasts of Iran. They also agreed to do some shopping during the trip — but instead of setting a fixed budget, they decided to determine how much they would spend by playing a game.
     22 
     23 The game is played on two arrays $a$ and $b$, each containing $n$ integers.
     24 
     25 The game will last for $k$ rounds. In one round:
     26 
     27 * First, Ali selects two indices $i$ and $j$ ($1 \leq i \lt j \leq n$);
     28 * Then, Bahamin rearranges the four integers $a_i$, $a_j$, $b_i$, and $b_j$ arbitrarily. Note that Bahamin can swap numbers between two arrays. He can also keep the two arrays unchanged.
     29 
     30 After all the $k$ rounds, the value of the game is defined as $v=\sum\limits_{i=1}^{n} |a_i-b_i|$. Ali and Bahamin will spend exactly $v$ coins during their trip.
     31 
     32 However, their goals are quite different:
     33 
     34 * Ali wants to spend as little as possible, that is, to minimize $v$;
     35 * Bahamin wants to spend as much as possible, that is, to maximize $v$.
     36 
     37 You have to find the final amount of coins they will spend if both Ali and Bahamin play optimally.
     38 
     39 Input
     40 
     41 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
     42 
     43 The first line of each test case contains two integers $n$ and $k$ ($2 \leq n \leq 2 \cdot 10^5$, $1 \leq k \leq n$) — the length of $a$ and $b$, and the number of rounds.
     44 
     45 The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \leq a_i \leq 10^9$) — the elements of $a$.
     46 
     47 The third line contains $n$ integers $b_1,b_2,\ldots,b_n$ ($1 \leq b_i \leq 10^9$) — the elements of $b$.
     48 
     49 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     50 
     51 Output
     52 
     53 For each test case, output a single integer — the final amount of coins they will spend if both Ali and Bahamin play optimally.
     54 
     55 Example
     56 
     57 Input
     58 
     59 ```
     60 5
     61 
     62 2 1
     63 
     64 1 7
     65 
     66 3 5
     67 
     68 3 2
     69 
     70 1 5 3
     71 
     72 6 2 4
     73 
     74 5 4
     75 
     76 1 16 10 10 16
     77 
     78 3 2 2 15 15
     79 
     80 4 1
     81 
     82 23 1 18 4
     83 
     84 19 2 10 3
     85 
     86 10 10
     87 
     88 4 3 2 100 4 1 2 4 5 5
     89 
     90 1 200 4 5 6 1 10 2 3 4
     91 ```
     92 
     93 Output
     94 
     95 ```
     96 8
     97 9
     98 30
     99 16
    100 312
    101 ```
    102 
    103 Note
    104 
    105 In the first test case, Ali can only choose $(i, j) = (1, 2)$, and Bahamin can rearrange all four numbers. Thus, he can assign $a = [5, 1]$ and $b = [3, 7]$. And the value of the game will be $v=|5 - 3| + |1 - 7| = 8$. It can be shown that this is the maximum possible value reachable for Bahamin — Other arrangements like $a = [5, 7],\space b = [1, 3]$ are also possible, but they don't have larger values.
    106 
    107 In the second test case, the best strategy for Bahamin is to keep the two arrays unchanged, regardless of what indices Ali selects. And the value of the game will be $v=|1 - 6| + |5 - 2| + |3 - 4| = 9$.