CF1566D1.Seating Arrangements (easy version)

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

It is the easy version of the problem. The only difference is that in this version n=1n = 1.

In the cinema seats can be represented as the table with nn rows and mm columns. The rows are numbered with integers from 11 to nn. The seats in each row are numbered with consecutive integers from left to right: in the kk-th row from m(k−1)+1m (k - 1) + 1 to mkm k for all rows 1≤k≤n1 \le k \le n.

11

22

⋯\cdots

m−1m - 1

mm

m+1m + 1

m+2m + 2

⋯\cdots

2m−12 m - 1

2m2 m

2m+12m + 1

2m+22m + 2

⋯\cdots

3m−13 m - 1

3m3 m

⋮\vdots

⋮\vdots

⋱\ddots

⋮\vdots

⋮\vdots

m(n−1)+1m (n - 1) + 1

m(n−1)+2m (n - 1) + 2

⋯\cdots

nm−1n m - 1

nmn m

The table with seats indices

There are nmnm people who want to go to the cinema to watch a new film. They are numbered with integers from 11 to nmnm. You should give exactly one seat to each person.

It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. ii-th person has the level of sight aia_i. Let's define sis_i as the seat index, that will be given to ii-th person. You want to give better places for people with lower sight levels, so for any two people ii, jj such that ai<aja_i \lt a_j it should be satisfied that si<sjs_i \lt s_j.

After you will give seats to all people they will start coming to their seats. In the order from 11 to nmnm, 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.

Let's consider an example: m=5m = 5, the person has the seat 44 in the first row, the seats 11, 33, 55 in the first row are already occupied, the seats 22 and 44 are free. The inconvenience of this person will be 22, because he will go through occupied seats 11 and 33.

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).

这是该问题的简易版本,唯一的区别在于本版本中 n=1n = 1。

电影院的座位可表示为一个 nn 行 mm 列的表格。行编号为从 11 到 nn 的整数。每行内的座位从左到右依次编号:在第 kk 行(对所有满足 1≤k≤n1 \le k \le n 的 kk),座位编号从 m(k−1)+1m (k - 1) + 1 到 mkm k。

11

22

⋯\cdots

m−1m - 1

mm

m+1m + 1

m+2m + 2

⋯\cdots

2m−12 m - 1

2m2 m

2m+12m + 1

2m+22m + 2

⋯\cdots

3m−13 m - 1

3m3 m

⋮\vdots

⋮\vdots

⋱\ddots

⋮\vdots

⋮\vdots

m(n−1)+1m (n - 1) + 1

m(n−1)+2m (n - 1) + 2

⋯\cdots

nm−1n m - 1

nmn m

座位编号表

共有 nmnm 位观众希望前往电影院观看一部新电影。他们被编号为从 11 到 nmnm 的整数。你需要为每位观众恰好分配一个座位。

已知,在该电影院中,座位编号越小,观影视野越好。第 ii 位观众的视力水平为 aia_i。记第 ii 位观众被分配到的座位编号为 sis_i。你希望为视力水平更低的观众分配更优的座位,因此对于任意两位观众 ii、jj,若 ai<aja_i \lt a_j,则必须满足 si<sjs_i \lt s_j。

分配完所有座位后,观众将按编号从 11 到 nmnm 的顺序依次入场并就座。每位观众进入影厅后,先走到自己座位所在的行,再从该行第一个座位开始,自左向右走向自己的座位。在行走过程中,部分座位为空,部分座位已被先前入座的观众占据。该观众的“不便值”等于其路径上经过的已被占据的座位数量。

我们考虑一个例子:设 m=5m = 5,某位观众被分配到第一行的第 44 号座位;此时第一行中编号为 11、33、55 的座位已被占据,而编号为 22 和 44 的座位仍为空。该观众的不便值为 22,因为他/她需经过已被占据的座位 11 和 33。

请找出在满足全部约束条件下,所有观众的不便值之和(即总不便值)的最小可能值。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1001 \le t \le 100) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers nn and mm (n=1n = 1, 1≤m≤3001 \le m \le 300) — the number of rows and places in each row respectively.

The second line of each test case contains n⋅mn \cdot m integers a1,a2,…,an⋅ma_1, a_2, \ldots, a_{n \cdot m} (1≤ai≤1091 \le a_i \le 10^9), where aia_i is the sight level of ii-th person.

It's guaranteed that the sum of n⋅mn \cdot m over all test cases does not exceed 10510^5.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤1001 \le t \le 100),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(n=1n = 1,1≤m≤3001 \le m \le 300),分别表示行数和每行的座位数。

每个测试用例的第二行包含 n⋅mn \cdot m 个整数 a1,a2,…,an⋅ma_1, a_2, \ldots, a_{n \cdot m}(1≤ai≤1091 \le a_i \le 10^9),其中 aia_i 表示第 ii 个人的视线高度。

保证所有测试用例中 n⋅mn \cdot m 的总和不超过 10510^5。

输出格式

For each test case print a single integer — the minimal total inconvenience that can be achieved.

对于每个测试用例,输出一个整数——所能达到的最小总不便度。

输入输出样例

  • 输入#1

    4
    1 3
    1 2 3
    1 5
    2 1 5 3 3
    1 2
    2 1
    1 6
    2 3 2 1 1 1

    输出#1

    3
    6
    0
    1

说明/提示

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 00, inconvenience of the second person will be 11 and inconvenience of the third person will be 22. The total inconvenience is 0+1+2=30 + 1 + 2 = 3.

In the second test case, people should sit as follows: s1=2s_1 = 2, s2=1s_2 = 1, s3=5s_3 = 5, s4=4s_4 = 4, s5=3s_5 = 3. The total inconvenience will be 66.

在第一个测试用例中,只有一种安排人员的方式,因为所有视线高度均互不相同。第一个人将坐在第一个座位上,第二个人将坐在第二个座位上,第三个人将坐在第三个座位上。因此,第一个人的不便值为 00,第二个人的不便值为 11,第三个人的不便值为 22。总不便值为 0+1+2=30 + 1 + 2 = 3。

在第二个测试用例中,人员应按如下方式就座:s1=2s_1 = 2,s2=1s_2 = 1,s3=5s_3 = 5,s4=4s_4 = 4,s5=3s_5 = 3。总不便值为 66。

输入解题思路,AI测评打分。不知道怎么写?

首页