您的位置:首页 > 其它

2020牛客多校第六场 C Combination of Physics and Maths

2020-07-29 23:30 225 查看

C Combination of Physics and Maths

题目链接:https://ac.nowcoder.com/acm/contest/5671/C

 
题目描述

Roundgod has an n×mn \times mn×m matrix A=[ai,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 ppp satisfies a formula p=FSp=\frac{F}{S}p=SF​, where FFF is the compressive force and SSS 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 SSS of {1,2,…,n}\{1,2, \ldots, n\}{1,2,…,n} and a nonempty subsequence TTT of {1,2,…,m}\{1, 2, \ldots, m\}{1,2,…,m}, then

[aS1,T1aS1,T2⋯aS1,T∣T∣aS2,T1aS2,T2⋯aS2,T∣T∣​⋮⋮⋱⋮aS∣S∣,T1​aS∣S∣,T2​⋯aS∣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​,T1​​aS2​,T1​​​⋮aS∣S∣​,T1​​​​aS1​,T2​​aS2​,T2​​⋮aS∣S∣​,T2​​​​⋯⋯⋱⋯​aS1​,T∣T∣​​aS2​,T∣T∣​​⋮aS∣S∣​,T∣T∣​​​⎦⎥⎥⎥⎤​

is a submatrix of AAA.

输入描述:
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≤ai,j≤5⋅104).(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 10−810^{-8}10−8 in one line.

示例1
输入

1
3 3
1 3 5
6 8 9
2 7 4

输出

4.50000000

说明

[156924]\begin{bmatrix} 1 & 5 \\ 6 & 9 \\ 2 & 4 \end{bmatrix}⎣⎡​162​594​⎦⎤​is one of submatrices of AAA with maximum pressure 1+5+6+9+2+42+4=276=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.

 

题面

思路

题解:


题意就不再解释

假如我们对于每列都进行这样的计算:从第一行到终止行(任一行)的数之和除以终止行的数,那么我们可以得到以每一列的某些行作为矩阵的最大值,然后如果以多列作为选中的矩阵的话,合并后的值一定不会大于合并前中的一个矩阵的压强值,证明如下:

以两个矩阵来合并,设其中压强值较大的值为aba \over bba​,较小的为cdc \over ddc​,那么合并后的值为a+cb+da+c \over b+db+da+c​。

则 aba\over bba​ - a+cb+da+c\over b+db+da+c​ = a⋅d−c⋅bb⋅b+b⋅da \cdot d - c\cdot b\over b\cdot b + b\cdot db⋅b+b⋅da⋅d−c⋅b​   又由aba\over bba​ > cdc\over ddc​ => a⋅d>b⋅ca\cdot d>b\cdot ca⋅d>b⋅c

故aba\over bba​ - a+cb+da+c\over b+db+da+c​ ===a⋅d−c⋅bb⋅b+b⋅da\cdot d-c\cdot b\over b\cdot b+b\cdot db⋅b+b⋅da⋅d−c⋅b​ >0> 0>0

即aba\over bba​>>>a+cb+da+c\over b+db+da+c​ (如果ab=cd{a\over b} = {c\over d}ba​=dc​,就取等)

即所得到的结论:
cd≤a+cb+d≤ab{c\over d} \leq {a+c \over b+d} \leq{a\over b}dc​≤b+da+c​≤ba​

故最大的结果就是只有一列的情况,找一列情况中最大的就可以了。

代码

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: