Guest User

Untitled

a guest
Jan 18th, 2021
52
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4.   int t;
  5.   cin >> t;
  6.   while(t--) {
  7.     int n, m;
  8.     cin >> n >> m;
  9.     int a[n][m];
  10.  
  11.     for(int i = 0; i < n; i++)
  12.       for(int j = 0; j < m; j++)
  13.         cin >> a[i][j];
  14.  
  15.     int ans = n * m * 10;
  16.  
  17.     for(int d = 0; d < 10; d++) {
  18.      
  19.       int temp = 0;
  20.       for(int i = 0; i < n; i++)
  21.         for(int j = 0; j < m; j++)
  22.           temp += abs(a[i][j] - d);
  23.  
  24.       ans = min(ans, temp);
  25.     }
  26.     cout << ans << "\n";
  27.   }
  28.   return 0;
  29. }
RAW Paste Data