# C Combination of Physics and Maths

Roundgod has an n × m n \times m matrix A = [ a i , j ] A =[a_{i,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 satisfies a formula p = F S p=\frac{F}{S} , where F F is the compressive force and 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 of { 1 , 2 , … , n } \{1,2, \ldots, n\} and a nonempty subsequence T T of { 1 , 2 , … , m } \{1, 2, \ldots, 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}

is a submatrix of 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) , 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) , 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).

For each test case, print the maximum pressure within an absolute or relative error of no more than 1 0 − 8 10^{-8} in one line.

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} is one of submatrices of 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.

## 思路

a b a\over b - a + c b + d a+c\over b+d = a ⋅ d − c ⋅ b b ⋅ b + b ⋅ d a \cdot d - c\cdot b\over b\cdot b + b\cdot d   又由 a b a\over b > c d c\over d => a ⋅ d > b ⋅ c a\cdot d>b\cdot c

a b a\over b - a + c b + d a+c\over b+d = = a ⋅ d − c ⋅ b b ⋅ b + b ⋅ d a\cdot d-c\cdot b\over b\cdot b+b\cdot d > 0 > 0

a b a\over b > > a + c b + d a+c\over b+d （如果 a b = c d {a\over b} = {c\over d} ,就取等）

c d ≤ a + c b + d ≤ a b {c\over d} \leq {a+c \over b+d} \leq{a\over b}

## 代码

#include <bits/stdc++.h>

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;
}


07-23 77
07-24 32
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客