codeforces-scrape

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

1566D1.md (3894B)


      1 Source: https://codeforces.com/problemset/problem/1566/D1
      2 
      3 D1. Seating Arrangements (easy version)
      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 It is the easy version of the problem. The only difference is that in this version $n = 1$.
     22 
     23 In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: in the $k$-th row from $m (k - 1) + 1$ to $m k$ for all rows $1 \le k \le n$.
     24 
     25 |  |  |  |  |  |
     26 | --- | --- | --- | --- | --- |
     27 | $1$ | $2$ | $\cdots$ | $m - 1$ | $m$ |
     28 | $m + 1$ | $m + 2$ | $\cdots$ | $2 m - 1$ | $2 m$ |
     29 | $2m + 1$ | $2m + 2$ | $\cdots$ | $3 m - 1$ | $3 m$ |
     30 | $\vdots$ | $\vdots$ | $\ddots$ | $\vdots$ | $\vdots$ |
     31 | $m (n - 1) + 1$ | $m (n - 1) + 2$ | $\cdots$ | $n m - 1$ | $n m$ |
     32 
     33 The table with seats indices 
     34 
     35 There are $nm$ people who want to go to the cinema to watch a new film. They are numbered with integers from $1$ to $nm$. You should give exactly one seat to each person.
     36 
     37 It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. $i$-th person has the level of sight $a_i$. Let's define $s_i$ as the seat index, that will be given to $i$-th person. You want to give better places for people with lower sight levels, so for any two people $i$, $j$ such that $a_i \lt a_j$ it should be satisfied that $s_i \lt s_j$.
     38 
     39 After you will give seats to all people they will start coming to their seats. In the order from $1$ to $nm$, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat's row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through.
     40 
     41 Let's consider an example: $m = 5$, the person has the seat $4$ in the first row, the seats $1$, $3$, $5$ in the first row are already occupied, the seats $2$ and $4$ are free. The inconvenience of this person will be $2$, because he will go through occupied seats $1$ and $3$.
     42 
     43 Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied).
     44 
     45 Input
     46 
     47 The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows.
     48 
     49 The first line of each test case contains two integers $n$ and $m$ ($n = 1$, $1 \le m \le 300$) — the number of rows and places in each row respectively.
     50 
     51 The second line of each test case contains $n \cdot m$ integers $a_1, a_2, \ldots, a_{n \cdot m}$ ($1 \le a_i \le 10^9$), where $a_i$ is the sight level of $i$-th person.
     52 
     53 It's guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$.
     54 
     55 Output
     56 
     57 For each test case print a single integer — the minimal total inconvenience that can be achieved.
     58 
     59 Example
     60 
     61 Input
     62 
     63 ```
     64 4
     65 1 3
     66 1 2 3
     67 1 5
     68 2 1 5 3 3
     69 1 2
     70 2 1
     71 1 6
     72 2 3 2 1 1 1
     73 ```
     74 
     75 Output
     76 
     77 ```
     78 3
     79 6
     80 0
     81 1
     82 ```
     83 
     84 Note
     85 
     86 In the first test case, there is a single way to arrange people, because all sight levels are distinct. The first person will sit on the first seat, the second person will sit on the second place, the third person will sit on the third place. So inconvenience of the first person will be $0$, inconvenience of the second person will be $1$ and inconvenience of the third person will be $2$. The total inconvenience is $0 + 1 + 2 = 3$.
     87 
     88 In the second test case, people should sit as follows: $s_1 = 2$, $s_2 = 1$, $s_3 = 5$, $s_4 = 4$, $s_5 = 3$. The total inconvenience will be $6$.