1. 程式人生 > 實用技巧 >牛客多校(2020第六場)C Combination of Physics and Maths(貪心)

牛客多校(2020第六場)C Combination of Physics and Maths(貪心)

題目連結:傳送門

題解:

  此題就是一個矩陣的最後一行的數代表底面積,所有數的和為重量,求壓強P

  • a/b<=(a+c)/(b+d)<=c/d
  • 所以如果選擇子矩陣有很多列,則拆成倆個行數不變得更小子矩陣
  • (也就是豎著切),其中一個肯定是不最壞的情況
  • 所以答案就是尋找單列最大值
 1 /*
 2 a/b <= (a+c)/(b+d) <= c/d
 3 所以如果選擇子矩陣有很多列,則拆成倆個行數不變得更小子矩陣
 4 (也就是豎著切),其中一個肯定是不最壞的情況
 5 所以答案就是尋找單列最大值
 6 */
 7 #include<iostream>
 8 #include<cstring>
 9
10 using namespace std; 11 12 const int MAX_N = 505; 13 int matrix[MAX_N]; //用來儲存當前遍歷到的列的值 14 double max_res; 15 int n, m; 16 17 int main() { 18 ios::sync_with_stdio(false); cin.tie(0); 19 int t; 20 cin >> t; 21 while (t--) { 22 memset(matrix, 0, sizeof(matrix)); 23 max_res = 0
; 24 cin >> n >> m; 25 26 for (int i = 1; i <= n; i++) 27 for (int j = 1; j <= m; j++) { 28 int a; 29 cin >> a; 30 matrix[j] += a; 31 max_res = max(max_res, 1.0*matrix[j] / a); 32 }
33 34 printf ("%.8lf\n", max_res); 35 } 36 return 0; 37 } 38 39 /* 40 in 41 42 1 43 3 3 44 1 3 5 45 6 8 9 46 2 7 4 47 48 out 49 4.50000000 50 */