codeforces-scrape

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

1899E.md (1887B)


      1 Source: https://codeforces.com/problemset/problem/1899/E
      2 
      3 E. Queue Sort
      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 Vlad found an array $a$ of $n$ integers and decided to sort it in non-decreasing order.
     22 
     23 To do this, Vlad can apply the following operation any number of times:
     24 
     25 * Extract the first element of the array and insert it at the end;
     26 * Swap that element with the previous one until it becomes the first or until it becomes strictly greater than the previous one.
     27 
     28 Note that both actions are part of the operation, and for one operation, you must apply both actions.
     29 
     30 For example, if you apply the operation to the array [$4, 3, 1, 2, 6, 4$], it will change as follows:
     31 
     32 * [$\color{red}{4}, 3, 1, 2, 6, 4$];
     33 * [$3, 1, 2, 6, 4, \color{red}{4}$];
     34 * [$3, 1, 2, 6, \color{red}{4}, 4$];
     35 * [$3, 1, 2, \color{red}{4}, 6, 4$].
     36 
     37 Vlad doesn't have time to perform all the operations, so he asks you to determine the minimum number of operations required to sort the array or report that it is impossible.
     38 
     39 Input
     40 
     41 The first line of the input contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Descriptions of the test cases follow.
     42 
     43 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.
     44 
     45 The second line of each test case contains $n$ integers $a_1, a_2, a_3, \dots, a_n$ ($1 \le a_i \le 10^9$) — the elements of the array.
     46 
     47 It is guaranteed that the sum of $n$ over all testcases does not exceed $2 \cdot 10^5$.
     48 
     49 Output
     50 
     51 For each test case, output a single integer — the minimum number of operations needed to sort the array. If it is impossible to do so, output $-1$ as the answer.
     52 
     53 Example
     54 
     55 Input
     56 
     57 ```
     58 5
     59 
     60 5
     61 
     62 6 4 1 2 5
     63 
     64 7
     65 
     66 4 5 3 7 8 6 2
     67 
     68 6
     69 
     70 4 3 1 2 6 4
     71 
     72 4
     73 
     74 5 2 4 2
     75 
     76 3
     77 
     78 2 2 3
     79 ```
     80 
     81 Output
     82 
     83 ```
     84 2
     85 6
     86 -1
     87 -1
     88 0
     89 ```