2020 牛客多校 第六场 C-Combination of Physics and Maths(思维)

题目链接: C-Combination of Physics and Maths

Description

题意:给出一个二维矩阵,求最大压力,压力为子矩阵之和除子矩阵底层元素之和

2020 牛客多校 第六场 C-Combination of Physics and Maths(思维)_第1张图片

Input

  • There are multiple test cases. The first line of input contains an integer 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) 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<= 5e4).

Output

For each test case, print the maximum pressure within an absolute or relative error of no more than 108 in one line.

Sample Input

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

Sample Output

4.50000000

Method

  • 利用性质 设列矩阵A, B,必有 压A <= 压A+B <= 压B 或 压B <= 压A+B <= 压A;
  • 所以其实就转换成求压力最大的列矩阵的压力值,O(m*n) 遍历矩阵即可;

Code

详见注释

#include 

using namespace std;
#pragma GCC optimize(2)
#define ios std::ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);
#define rtxt freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define ll long long
const int Max = 1e3+3;
const int mod = 1e9+7;

int a[Max][Max], n, m, tmp, T;
double ans=0;

int main()
{
	ios
	
	cin >> T;
	while(T--)
	{
		cin >> n >> m;
		for(int i=1; i<=n; i++)
			for(int j=1; j<=m; j++)
				cin >> a[i][j];
		for(int i=1; i<=m; i++)							//遍历矩阵
		{
			tmp = 0;
			for(int j=1; j<=n; j++)
			{
				tmp += a[j][i];
				ans = max(ans, (double)tmp/a[j][i]);	//求最大值
			}
		}
		printf("%.8lf\n", ans);
		ans = 0;
	}
	return 0;
}

蒟蒻一只,欢迎指正

你可能感兴趣的:(训练赛病历,引理,&,技巧,&,NT,c++,算法)