-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday22.cpp
More file actions
36 lines (34 loc) · 990 Bytes
/
day22.cpp
File metadata and controls
36 lines (34 loc) · 990 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int minOperationsToUniValueGrid(vector<vector<int>>& grid, int m, int n, int x) {
vector<int> values;
int base = grid[0][0];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int diff = grid[i][j] - base;
if (abs(diff) % x != 0) return -1;
values.push_back(grid[i][j]);
}
}
sort(values.begin(), values.end());
int median = values[values.size() / 2], operations = 0;
for (int v : values) operations += abs(v - median) / x;
return operations;
}
int main() {
int T;
cin >> T;
while (T--) {
int m, n, x;
cin >> m >> n >> x;
vector<vector<int>> grid(m, vector<int>(n));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) cin >> grid[i][j];
}
cout << minOperationsToUniValueGrid(grid, m, n, x) << endl;
}
return 0;
}