CF1566D2.Seating Arrangements (hard version)
普及/提高-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
It is the hard version of the problem. The only difference is that in this version 1≤n≤300.
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 mk for all rows 1≤k≤n.
1
2
⋯
m−1
m
m+1
m+2
⋯
2m−1
2m
2m+1
2m+2
⋯
3m−1
3m
⋮
⋮
⋱
⋮
⋮
m(n−1)+1
m(n−1)+2
⋯
nm−1
nm
The table with seats indices
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.
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 ai. Let's define si 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 ai<aj it should be satisfied that si<sj.
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.
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.
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).
这是该问题的困难版本。唯一的区别在于本版本中 1≤n≤300。
电影院的座位可表示为一个 n 行 m 列的表格。行号用 1 到 n 的整数表示。每行内的座位从左到右用连续整数编号:对所有满足 1≤k≤n 的第 k 行,其座位编号为从 m(k−1)+1 到 mk。
1
2
⋯
m−1
m
m+1
m+2
⋯
2m−1
2m
2m+1
2m+2
⋯
3m−1
3m
⋮
⋮
⋱
⋮
⋮
m(n−1)+1
m(n−1)+2
⋯
nm−1
nm
座位编号表
共有 nm 位观众希望前往电影院观看一部新电影。他们被编号为 1 到 nm 的整数。你需要为每位观众恰好分配一个座位。
已知,在该电影院中,座位编号越小,观影视野越好。第 i 位观众的视力水平为 ai。记第 i 位观众被分配到的座位编号为 si。你希望为视力水平更低的观众分配更优(即编号更小)的座位,因此对任意两个观众 i,j,若 ai<aj,则必须满足 si<sj。
在完成全部座位分配后,观众将按编号顺序 1 到 nm 依次入场并就座。每位观众进入影厅后,先到达其座位所在的行,再从该行最左侧的第一个座位开始,自左向右走向自己的座位。在行进过程中,有些座位是空的,有些座位已被先前入座的观众占据。该观众的“不便值”等于其路径上经过的已被占据的座位数量。
举例如下:设 m=5,某观众被分配到第 1 行的第 4 号座位,此时第 1 行中编号为 1、3、5 的座位已被占据,而编号为 2 和 4 的座位仍为空。该观众的不便值为 2,因为他在走向自己座位途中会经过已被占据的座位 1 和 3。
请找出在满足全部约束条件的前提下,所有观众不便值之和(即总不便值)的最小可能值。
输入格式
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤100) — the number of test cases. Description of the test cases follows.
The first line of each test case contains two integers n and m (1≤n,m≤300) — the number of rows and places in each row respectively.
The second line of each test case contains n⋅m integers a1,a2,…,an⋅m (1≤ai≤109), where ai is the sight level of i-th person.
It's guaranteed that the sum of n⋅m over all test cases does not exceed 105.
输入包含多个测试用例。第一行包含一个整数 t(1≤t≤100),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 m(1≤n,m≤300),分别表示行数和每行的座位数。
每个测试用例的第二行包含 n⋅m 个整数 a1,a2,…,an⋅m(1≤ai≤109),其中 ai 表示第 i 个人的视线高度。
保证所有测试用例中 n⋅m 的总和不超过 105。
输出格式
For each test case print a single integer — the minimal total inconvenience that can be achieved.
对于每个测试用例,输出一个整数——所能达到的最小总不便度。
输入输出样例
输入#1
7 1 2 1 2 3 2 1 1 2 2 3 3 3 3 3 4 4 1 1 1 1 1 2 2 2 1 1 2 1 4 2 50 50 50 50 3 50 50 50 4 2 6 6 6 6 2 2 9 6 2 9 1 3 3 3 3 3 1 1 3 1 3 1 1 3 3 1 1 3
输出#1
1 0 4 0 0 0 1
说明/提示
In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is 1.
In the second test case the optimal seating looks like this:

In the third test case the optimal seating looks like this:

The number in a cell is the person's index that sits on this place.
在第一个测试用例中,只有一种安排座位的方式:第一个人坐在第一个位置,第二个人坐在第二个位置。总不便值为 1。
在第二个测试用例中,最优的座位安排如下所示:

在第三个测试用例中,最优的座位安排如下所示:

单元格中的数字表示坐在该位置上的人的编号。
输入解题思路,AI测评打分。不知道怎么写?