Codeforces Round #177 (Div. 2) 289B. Polo the Penguin and Matrix Solution
Problem Link: https://codeforces.com/problemset/problem/289/B
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- int r, c, d, i, j;
- vector<int> v, v1;
- cin >> r >> c >> d;
- int a[r + 1][c + 1];
- for (i = 1; i <= r; i++) {
- for (j = 1; j <= c; j++) {
- cin >> a[i][j];
- v.push_back(a[i][j]);
- }
- }
- int f = 0, ans = 0;
- sort(v.begin(), v.end());
- int s = v.size();
- int mid = s / 2;
- for (int i = 0; i < s; i++) {
- if (i != mid) v1.push_back(abs(v[mid] - v[i]));
- }
- for (auto it : v1) {
- if (it % d == 0)
- ans += (it / d);
- else
- f = 1;
- }
- if(f==0)
- cout<<ans<<endl;
- else
- cout<<"-1"<<endl;
- }
No comments