codeforces-scrape

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

1993B.md (2039B)


      1 Source: https://codeforces.com/problemset/problem/1993/B
      2 
      3 B. Parity and Sum
      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 Given an array $a$ of $n$ positive integers.
     22 
     23 In one operation, you can pick any pair of indexes $(i, j)$ such that $a_i$ and $a_j$ have distinct parity, then replace the smaller one with the sum of them. More formally:
     24 
     25 * If $a_i \lt a_j$, replace $a_i$ with $a_i + a_j$;
     26 * Otherwise, replace $a_j$ with $a_i + a_j$.
     27 
     28 Find the minimum number of operations needed to make all elements of the array have the same parity.
     29 
     30 Input
     31 
     32 The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
     33 
     34 The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$).
     35 
     36 The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of array $a$.
     37 
     38 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     39 
     40 Output
     41 
     42 For each test case, output a single integer — the minimum number of operations required.
     43 
     44 Example
     45 
     46 Input
     47 
     48 ```
     49 7
     50 
     51 5
     52 
     53 1 3 5 7 9
     54 
     55 4
     56 
     57 4 4 4 4
     58 
     59 3
     60 
     61 2 3 4
     62 
     63 4
     64 
     65 3 2 2 8
     66 
     67 6
     68 
     69 4 3 6 1 2 1
     70 
     71 6
     72 
     73 3 6 1 2 1 2
     74 
     75 5
     76 
     77 999999996 999999997 999999998 999999999 1000000000
     78 ```
     79 
     80 Output
     81 
     82 ```
     83 0
     84 0
     85 2
     86 4
     87 3
     88 3
     89 3
     90 ```
     91 
     92 Note
     93 
     94 In the first test case, all integers already have the same parity. Therefore, no operation is needed.
     95 
     96 In the third test case, we can perform two operations $(1, 2)$ and $(1, 3)$. The array $a$ transforms as follows: $a = [\color{red}2, \color{red}3, 4] \longrightarrow [\color{red}5, 3, \color{red}4] \longrightarrow [5, 3, 9]$.
     97 
     98 In the fourth test case, an example of an optimal sequence of operations is $(1, 2)$, $(1, 3)$, $(1, 4)$, and $(1, 4)$. The array $a$ transforms as follows: $a = [\color{red}3, \color{red}2, 2, 8] \longrightarrow [\color{red}3, 5, \color{red}2, 8] \longrightarrow [\color{red}3, 5, 5, \color{red}8] \longrightarrow [\color{red}{11}, 5, 5, \color{red}8] \longrightarrow [11, 5, 5, 19]$.