codeforces-scrape

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

1847B.md (2280B)


      1 Source: https://codeforces.com/problemset/problem/1847/B
      2 
      3 B. Hamon Odyssey
      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 Jonathan is fighting against DIO's Vampire minions. There are $n$ of them with strengths $a_1, a_2, \dots, a_n$. $\def\and {{\,\texttt{&}\,}}$
     22 
     23 Denote $(l, r)$ as the group consisting of the vampires with indices from $l$ to $r$. Jonathan realizes that the strength of any such group is in its weakest link, that is, the bitwise AND. More formally, the strength level of the group $(l, r)$ is defined as $$f(l,r) = a_l \and a_{l+1} \and a_{l+2} \and \ldots \and a_r.$$ Here, $\and$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
     24 
     25 Because Jonathan would like to defeat the vampire minions fast, he will divide the vampires into contiguous groups, such that each vampire is in exactly one group, and the sum of strengths of the groups is minimized. Among all ways to divide the vampires, he would like to find the way with the maximum number of groups.
     26 
     27 Given the strengths of each of the $n$ vampires, find the maximum number of groups among all possible ways to divide the vampires with the smallest sum of strengths.
     28 
     29 Input
     30 
     31 The first line contains a single integer $t$ $(1 \leq t \leq 10^4)$ — the number of test cases. The description of test cases follows.
     32 
     33 The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the number of vampires.
     34 
     35 The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($0 \leq a_i \leq 10^9$) — the individual strength of each vampire.
     36 
     37 The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     38 
     39 Output
     40 
     41 For each test case, output a single integer — the maximum number of groups among all possible ways to divide the vampires with the smallest sum of strengths.
     42 
     43 Example
     44 
     45 Input
     46 
     47 ```
     48 3
     49 
     50 3
     51 
     52 1 2 3
     53 
     54 5
     55 
     56 2 3 1 5 2
     57 
     58 4
     59 
     60 5 7 12 6
     61 ```
     62 
     63 Output
     64 
     65 ```
     66 1
     67 2
     68 1
     69 ```
     70 
     71 Note
     72 
     73 In the first test case, the optimal way is to take all the $n$ vampires as a group. So, $f(1,3) = 1 \and 2 \and 3 = 0$.
     74 
     75 In the second test case, the optimal way is to make $2$ groups, $(2,3,1)$ and $(5,2)$. So, $f(1,3) + f(4,5) = (2 \and 3 \and 1) + (5 \and 2) = 0 + 0 = 0$.