CF1825B.LuoTianyi and the Table
入门
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
LuoTianyi gave an array b of n⋅m integers. She asks you to construct a table a of size n×m, filled with these n⋅m numbers, and each element of the array must be used exactly once. Also she asked you to maximize the following value:
i=1∑nj=1∑m(1≤x≤i,1≤y≤jmaxax,y−1≤x≤i,1≤y≤jminax,y)
This means that we consider n⋅m subtables with the upper left corner in (1,1) and the bottom right corner in (i,j) (1≤i≤n, 1≤j≤m), for each such subtable calculate the difference of the maximum and minimum elements in it, then sum up all these differences. You should maximize the resulting sum.
Help her find the maximal possible value, you don't need to reconstruct the table itself.
洛天依给出一个包含 n⋅m 个整数的数组 b。她要求你用这 n⋅m 个数构造一个大小为 n×m 的表格 a,其中每个数恰好使用一次。此外,她还要求最大化以下值:
i=1∑nj=1∑m(1≤x≤i,1≤y≤jmaxax,y−1≤x≤i,1≤y≤jminax,y)
这意味着:我们考虑所有以 (1,1) 为左上角、(i,j) 为右下角的子表格(其中 1≤i≤n,1≤j≤m),共 n⋅m 个;对每个这样的子表格,计算其内部最大值与最小值之差;最后将所有这些差值求和。你需要使该总和尽可能大。
请帮她找出可能的最大值,你无需实际构造出对应的表格。
输入格式
Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤200) — the number of test cases. The description of test cases follows.
The first line of each test case contains two integers n and m (2≤n,m≤100) — the number of rows and columns of the table.
The second line of each test case contains n⋅m integers b1,b2,…,bn⋅m (−105≤bi≤105) — the numbers you can put in the table.
Note, that integers in the array b can be negative.
It is guaranteed that the sum of n⋅m over all test cases doesn't exceed 2⋅105.
每个测试包含多个测试用例。第一行包含一个整数 t(1≤t≤200),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 m(2≤n,m≤100),分别表示表格的行数和列数。
每个测试用例的第二行包含 n⋅m 个整数 b1,b2,…,bn⋅m(−105≤bi≤105),表示可以填入表格中的数字。
注意,数组 b 中的整数可以为负数。
保证所有测试用例中 n⋅m 的总和不超过 2⋅105。
输出格式
For each test case, output a single integer — the maximal value, that can be obtained.
对于每个测试用例,输出一个整数——所能获得的最大值。
输入输出样例
输入#1
5 2 2 1 3 1 4 2 2 -1 -1 -1 -1 2 3 7 8 9 -3 10 8 3 2 4 8 -3 0 -7 1 4 3 -32030 59554 16854 -85927 68060 -64460 -79547 90932 85063 82703 -12001 38762
输出#1
9 0 64 71 1933711
说明/提示
In the first test case, the table is follows:
4
1
1
3
In the subtable with the bottom right corner in (1,1), the difference of the maximal and minimal elements is 4−4=0.
In the subtable with the bottom right corner in (1,2), the difference of the maximal and minimal elements is 4−1=3.
In the subtable with the bottom right corner in (2,1), the difference of the maximal and minimal elements is 4−1=3.
In the subtable with the bottom right corner in (2,2), the difference of the maximal and minimal elements is 4−1=3.
Then the maximum possible value is 0+3+3+3=9.
In the second test case, all elements are equal, so all differences are 0, and the answer is 0.
在第一个测试用例中,表格如下:
4
1
1
3
在右下角位于 (1,1) 的子表格中,最大元素与最小元素的差为 4−4=0。
在右下角位于 (1,2) 的子表格中,最大元素与最小元素的差为 4−1=3。
在右下角位于 (2,1) 的子表格中,最大元素与最小元素的差为 4−1=3。
在右下角位于 (2,2) 的子表格中,最大元素与最小元素的差为 4−1=3。
因此,可能的最大值为 0+3+3+3=9。
在第二个测试用例中,所有元素均相等,因此所有差值均为 0,答案为 0。
输入解题思路,AI测评打分。不知道怎么写?