codeforces-scrape

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

2146D1.md (2218B)


      1 Source: https://codeforces.com/problemset/problem/2146/D1
      2 
      3 D1. Max Sum OR (Easy Version)
      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 This is the easy version of the problem. The difference between the versions is that in this version, $l=0$, and $r \lt 2\cdot 10^5$. You can hack only if you solved all versions of this problem.
     22 
     23 You are given two integers $l$ and $r$ ($l\le r$).
     24 
     25 Let $n = r - l + 1$. We will create two arrays $a$ and $b$, both consisting of $n$ integers. Initially, both $a$ and $b$ are equal to $[l, l+1, \ldots, r]$. You have to reorder the array $a$ arbitrarily to maximize the following value:
     26 
     27 $$\sum_{i=1}^n \left (a_i\;|\;b_i \right ).$$
     28 
     29 Here, $|$ denotes the [bitwise OR operation](https://en.wikipedia.org/wiki/Bitwise_operation#OR).
     30 
     31 You also need to construct a possible way to reorder the array $a$.
     32 
     33 Input
     34 
     35 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.
     36 
     37 The only line of each test case contains two integers $l$ and $r$ ($0=l\leq r \lt 2\cdot 10^5$) — the minimum and maximum elements in $a$.
     38 
     39 Let $n = r - l + 1$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
     40 
     41 Output
     42 
     43 For each test case, print a single integer in the first line of output — the maximum value of $\sum\limits_{i=1}^n \left (a_i\;|\;b_i \right )$.
     44 
     45 Then, print $n$ distinct integers $a_1, a_2, \ldots,a_n$ in the second line — the array $a$ after reordering.
     46 
     47 If there are multiple answers, you may print any of them.
     48 
     49 Example
     50 
     51 Input
     52 
     53 ```
     54 3
     55 
     56 0 3
     57 
     58 0 9
     59 
     60 0 15
     61 ```
     62 
     63 Output
     64 
     65 ```
     66 12
     67 
     68 3 2 1 0
     69 
     70 90
     71 
     72 7 8 5 4 3 2 9 0 1 6
     73 
     74 240
     75 
     76 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
     77 ```
     78 
     79 Note
     80 
     81 In the first test case, the reordered array $a$ is $[3,2,1,0]$. The value of the expression is $(3\;|\;0)+(2\;|\;1)+(1\;|\;2)+(0\;|\;3)=3+3+3+3=12$. It can be proved that this is the maximum possible value of the expression.
     82 
     83 In the second test case, the reordered array $a$ is $[7,8,5,4,3,2,9,0,1,6]$. The value of the expression is $90$. It can be proved that this is the maximum possible value of the expression.