codeforces-scrape

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

2227E.md (2771B)


      1 Source: https://codeforces.com/problemset/problem/2227/E
      2 
      3 E. It All Went Sideways
      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 Yousef has $n$ columns of cubes standing side by side. The $i$-th column contains $a_i$ identical unit cubes stacked vertically. Initially gravity pulls cubes downwards, so every column $i$ contains exactly $a_i$ cubes at heights $1, 2, \dots, a_i$.
     22 
     23 Suddenly, gravity shifts to the right. Each cube slides horizontally as far to the right as possible. A cube cannot pass through or overlap other cubes, and it must remain at its original height. The final configuration is uniquely determined by the initial heights.
     24 
     25  ![](https://espresso.codeforces.com/111233edd63fbf78194f96fc81812e9b86368c8b.png) Before and after applying rightward gravity. 
     26 
     27 Before the gravity shift, Yousef may perform at most one operation: choose an index $i$ and decrease $a_i$ by $1$ (i.e. remove one cube from that column). He may also choose to do nothing.
     28 
     29 A cube is said to move if its column index after the gravity shift is different from its original column index.
     30 
     31 Find the maximum possible number of cubes that will move after the gravity shift, assuming Yousef applies the single decrease optimally (or chooses not to apply it).
     32 
     33 Input
     34 
     35 The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.
     36 
     37 The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array.
     38 
     39 The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the elements of the array.
     40 
     41 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     42 
     43 Output
     44 
     45 For each test case, output a single integer — the maximum possible number of cubes that will move after the gravity shift, assuming Yousef applies the single decrease optimally (or chooses not to apply it).
     46 
     47 Example
     48 
     49 Input
     50 
     51 ```
     52 5
     53 
     54 5
     55 
     56 1 2 3 2 1
     57 
     58 7
     59 
     60 5 4 1 1 1 1 3
     61 
     62 6
     63 
     64 1 2 3 4 5 6
     65 
     66 6
     67 
     68 4 1 6 3 2 6
     69 
     70 7
     71 
     72 1 3 2 7 2 3 1
     73 ```
     74 
     75 Output
     76 
     77 ```
     78 8
     79 
     80 12
     81 
     82 0
     83 
     84 10
     85 
     86 18
     87 ```
     88 
     89 Note
     90 
     91 In the first test case, it is optimal to perform the operation on index $5$, making the array $a = [1, 2, 3, 2, 0]$. After the gravity shift, every remaining cube will move. The answer is $1+2+3+2 = 8$. It can be proven that a greater answer does not exist.
     92 
     93 In the second test case, we can perform the operation on index $6$, making the array $a = [5, 4, 1, 1, 1, 0, 3]$. After the gravity shift, $5+4+1+1+1 = 12$ cubes will move. Note that the cubes at index $7$ do not move, since they are already at the rightmost end of the array.
     94 
     95 In the third test case, there is no way to make any cube move.