题目链接:https://ac.nowcoder.com/acm/contest/5671/C
题目描述
Roundgod has an n × m n \times m n×m matrix A = [ a i , j ] A =[a_{i,j}] A=[ai,j]. One day while she’s doing her physics homework, she wonders is it possible to define the physical quantity for matrices.
As we all know, the pressure p p p satisfies a formula p = F S p=\frac{F}{S} p=SF, where F F F is the compressive force and S S S is the base area.
To describe it in maths, Roundgod puts forward that the compressive force of a matrix equals the sum of all its entries, while the base area of a matrix equals the sum of the entries in its last row. Then she can calculate the pressure for a matrix with the same formula.
Your goal is to find the submatrix of AA with maximum pressure.
A submatrix is obtained by taking nonempty subsets of its rows and columns. Formally, given a nonempty subsequence S S S of { 1 , 2 , … , n } \{1,2, \ldots, n\} {1,2,…,n} and a nonempty subsequence T T T of { 1 , 2 , … , m } \{1, 2, \ldots, m\} {1,2,…,m}, then
[ a S 1 , T 1 a S 1 , T 2 ⋯ a S 1 , T ∣ T ∣ a S 2 , T 1 a S 2 , T 2 ⋯ a S 2 , T ∣ T ∣ ⋮ ⋮ ⋱ ⋮ a S ∣ S ∣ , T 1 a S ∣ S ∣ , T 2 ⋯ a S ∣ S ∣ , T ∣ T ∣ ] \begin{bmatrix} a_{S_1, T_1} & a_{S_1, T_2} & \cdots & a_{S_1, T_{|T|}} \\ a_{S_2, T_1} & a_{S_2, T_2} & \cdots & a_{S_2, T_{|T|}} \\\vdots & \vdots & \ddots & \vdots \\ a_{S_{|S|}, T_1} & a_{S_{|S|}, T_2} &\cdots & a_{S_{|S|}, T_{|T|}} \end{bmatrix} ⎣⎢⎢⎢⎡aS1,T1aS2,T1⋮aS∣S∣,T1aS1,T2aS2,T2⋮aS∣S∣,T2⋯⋯⋱⋯aS1,T∣T∣aS2,T∣T∣⋮aS∣S∣,T∣T∣⎦⎥⎥⎥⎤
is a submatrix of A A A.
输入描述:
There are multiple test cases. The first line of input contains an integer T ( T ≤ 100 ) T ( T ≤ 100 ) T\ (T\le100)T (T≤100) T (T≤100)T(T≤100), indicating the number of test cases. For each test case:
The first line contains two integers n , m ( 1 ≤ n , m ≤ 200 ) n, m\ (1\le n,m\le 200) n,m (1≤n,m≤200), the number of rows and columns of the matrix, respectively.
Each of the next nn lines contains mm integers, specifying the matrix ( 1 ≤ a i , j ≤ 5 ⋅ 1 0 4 ) . (1\le a_{i,j}\le 5\cdot 10^4). (1≤ai,j≤5⋅104).
输出描述:
For each test case, print the maximum pressure within an absolute or relative error of no more than 1 0 − 8 10^{-8} 10−8 in one line.
示例1
输入
1
3 3
1 3 5
6 8 9
2 7 4
输出
4.50000000
说明
[ 1 5 6 9 2 4 ] \begin{bmatrix} 1 & 5 \\ 6 & 9 \\ 2 & 4 \end{bmatrix} ⎣⎡162594⎦⎤is one of submatrices of A A A with maximum pressure 1 + 5 + 6 + 9 + 2 + 4 2 + 4 = 27 6 = 4.5. \frac{1+5+6+9+2+4}{2+4}=\frac{27}{6}=4.5. 2+41+5+6+9+2+4=627=4.5.
假如我们对于每列都进行这样的计算:从第一行到终止行(任一行)的数之和除以终止行的数,那么我们可以得到以每一列的某些行作为矩阵的最大值,然后如果以多列作为选中的矩阵的话,合并后的值一定不会大于合并前中的一个矩阵的压强值,证明如下:
以两个矩阵来合并,设其中压强值较大的值为 a b a \over b ba,较小的为 c d c \over d dc,那么合并后的值为 a + c b + d a+c \over b+d b+da+c。
则 a b a\over b ba - a + c b + d a+c\over b+d b+da+c = a ⋅ d − c ⋅ b b ⋅ b + b ⋅ d a \cdot d - c\cdot b\over b\cdot b + b\cdot d b⋅b+b⋅da⋅d−c⋅b 又由 a b a\over b ba > c d c\over d dc => a ⋅ d > b ⋅ c a\cdot d>b\cdot c a⋅d>b⋅c
故 a b a\over b ba - a + c b + d a+c\over b+d b+da+c = = = a ⋅ d − c ⋅ b b ⋅ b + b ⋅ d a\cdot d-c\cdot b\over b\cdot b+b\cdot d b⋅b+b⋅da⋅d−c⋅b > 0 > 0 >0
即 a b a\over b ba > > > a + c b + d a+c\over b+d b+da+c (如果 a b = c d {a\over b} = {c\over d} ba=dc,就取等)
即所得到的结论:
c d ≤ a + c b + d ≤ a b {c\over d} \leq {a+c \over b+d} \leq{a\over b} dc≤b+da+c≤ba
故最大的结果就是只有一列的情况,找一列情况中最大的就可以了。
#include
const int N = 205;
int T, n, m, a[N][N];
int main() {
for (scanf("%d", &T); T; T--) {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
}
}
double ans = 1; //只有一个数时就为1,不可能比这还小
for (int j = 0; j < m; j++) {
double sum = 0;
for (int i = 0; i < n; i++) {
sum += a[i][j];
ans = std::max(ans, sum / a[i][j]);
}
}
printf("%.8f\n", ans);
}
return 0;
}