codeforces-scrape

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

1881E.md (2188B)


      1 Source: https://codeforces.com/problemset/problem/1881/E
      2 
      3 E. Block Sequence
      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 Given a sequence of integers $a$ of length $n$.
     22 
     23 A sequence is called beautiful if it has the form of a series of blocks, each starting with its length, i.e., first comes the length of the block, and then its elements. For example, the sequences [$\color{red}{3},\ \color{red}{3},\ \color{red}{4},\ \color{red}{5},\ \color{green}{2},\ \color{green}{6},\ \color{green}{1}$] and [$\color{red}{1},\ \color{red}{8},\ \color{green}{4},\ \color{green}{5},\ \color{green}{2},\ \color{green}{6},\ \color{green}{1}$] are beautiful (different blocks are colored differently), while [$1$], [$1,\ 4,\ 3$], [$3,\ 2,\ 1$] are not.
     24 
     25 In one operation, you can remove any element from the sequence. What is the minimum number of operations required to make the given sequence beautiful?
     26 
     27 Input
     28 
     29 The first line of the input contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then follows the descriptions of the test cases.
     30 
     31 The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the sequence $a$.
     32 
     33 The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$) — the elements of the sequence $a$.
     34 
     35 It is guaranteed that the sum of the values of $n$ over all test cases does not exceed $2 \cdot 10^5$.
     36 
     37 Output
     38 
     39 For each test case, output a single number — the minimum number of deletions required to make the sequence $a$ beautiful.
     40 
     41 Example
     42 
     43 Input
     44 
     45 ```
     46 7
     47 
     48 7
     49 
     50 3 3 4 5 2 6 1
     51 
     52 4
     53 
     54 5 6 3 2
     55 
     56 6
     57 
     58 3 4 1 6 7 7
     59 
     60 3
     61 
     62 1 4 3
     63 
     64 5
     65 
     66 1 2 3 4 5
     67 
     68 5
     69 
     70 1 2 3 1 2
     71 
     72 5
     73 
     74 4 5 5 1 5
     75 ```
     76 
     77 Output
     78 
     79 ```
     80 0
     81 4
     82 1
     83 1
     84 2
     85 1
     86 0
     87 ```
     88 
     89 Note
     90 
     91 In the first test case of the example, the given sequence is already beautiful, as shown in the statement.
     92 
     93 In the second test case of the example, the sequence can only be made beautiful by removing all elements from it.
     94 
     95 In the fifth test case of the example, the sequence can be made beautiful by removing the first and last elements. Then the sequence will become [$2,\ 3,\ 4$].