codeforces-scrape

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

1706C.md (3401B)


      1 Source: https://codeforces.com/problemset/problem/1706/C
      2 
      3 C. Qpwoeirut And The City
      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 Qpwoeirut has taken up architecture and ambitiously decided to remodel his city.
     22 
     23 Qpwoeirut's city can be described as a row of $n$ buildings, the $i$-th ($1 \le i \le n$) of which is $h_i$ floors high. You can assume that the height of every floor in this problem is equal. Therefore, building $i$ is taller than the building $j$ if and only if the number of floors $h_i$ in building $i$ is larger than the number of floors $h_j$ in building $j$.
     24 
     25 Building $i$ is cool if it is taller than both building $i-1$ and building $i+1$ (and both of them exist). Note that neither the $1$-st nor the $n$-th building can be cool.
     26 
     27 To remodel the city, Qpwoeirut needs to maximize the number of cool buildings. To do this, Qpwoeirut can build additional floors on top of any of the buildings to make them taller. Note that he cannot remove already existing floors.
     28 
     29 Since building new floors is expensive, Qpwoeirut wants to minimize the number of floors he builds. Find the minimum number of floors Qpwoeirut needs to build in order to maximize the number of cool buildings.
     30 
     31 Input
     32 
     33 The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
     34 
     35 The first line of each test case contains the single integer $n$ ($3 \le n \le 10^5$) — the number of buildings in Qpwoeirut's city.
     36 
     37 The second line of each test case contains $n$ integers $h_1, h_2, \ldots, h_n$ ($1 \le h_i \le 10^9$) — the number of floors in each of the buildings of the city.
     38 
     39 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 — the minimum number of additional floors Qpwoeirut needs to build in order to maximize the number of cool buildings.
     44 
     45 Example
     46 
     47 Input
     48 
     49 ```
     50 6
     51 
     52 3
     53 
     54 2 1 2
     55 
     56 5
     57 
     58 1 2 1 4 3
     59 
     60 6
     61 
     62 3 1 4 5 5 2
     63 
     64 8
     65 
     66 4 2 1 3 5 3 6 1
     67 
     68 6
     69 
     70 1 10 1 1 10 1
     71 
     72 8
     73 
     74 1 10 11 1 10 11 10 1
     75 ```
     76 
     77 Output
     78 
     79 ```
     80 2
     81 0
     82 3
     83 3
     84 0
     85 4
     86 ```
     87 
     88 Note
     89 
     90 In the first test case, it is optimal for Qpwoeirut to make the second building cool by building $2$ additional floors on top of it, making it taller than both of its adjacent buildings. The final heights of buildings will be $[2, \underline{3}, 2]$.
     91 
     92 |  |  |  |
     93 | --- | --- | --- |
     94 |  |  |  |
     95 
     96 In the second test case, the number of cool buildings is already maximized, so Qpwoeirut does not need to do anything.
     97 
     98 In the third test case, it is optimal for Qpwoeirut to make the third and fifth buildings cool by building $2$ additional floors onto the third building and $1$ additional floor onto the fifth building. The final heights of buildings will be $[3, 1, \underline{6}, 5, \underline{6}, 2]$.
     99 
    100 |  |  |  |
    101 | --- | --- | --- |
    102 |  |  |  |
    103 
    104 It can be shown that it is impossible to make more than $2$ of the buildings cool, or to make $2$ buildings cool using fewer than $3$ additional floors.
    105 
    106 In the fourth test case, Qpwoeirut can either make the second building cool, or he can make the third building cool. Either way, he will be building $3$ additional floors and maximizing the number of cool buildings. The final heights of buildings will be $[4, 2, \underline{4}, 3, 5, 3, 6, 1]$ or $[4, \underline{5}, 1, 3, 5, 3, 6, 1]$.
    107 
    108 |  |  |  |  |
    109 | --- | --- | --- | --- |
    110 |  |  |  |  |