Codeforces Round #177 (Div. 2) 289B. Polo the Penguin and Matrix Solution



 Problem Link:  https://codeforces.com/problemset/problem/289/B

Solution in C++:

  1. /// La ilaha illellahu muhammadur rasulullah
  2. ///******Bismillahir-Rahmanir-Rahim******///
  3. /// Abul Hasnat Tonmoy
  4. /// Department of CSE,23rd batch
  5. /// Islamic University,Bangladesh
  6. ///**********ALLAH IS ALMIGHTY************///
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9. int main() {
  10. int r, c, d, i, j;
  11. vector<int> v, v1;
  12. cin >> r >> c >> d;
  13. int a[r + 1][c + 1];
  14. for (i = 1; i <= r; i++) {
  15. for (j = 1; j <= c; j++) {
  16. cin >> a[i][j];
  17. v.push_back(a[i][j]);
  18. }
  19. }
  20. int f = 0, ans = 0;
  21. sort(v.begin(), v.end());
  22. int s = v.size();
  23. int mid = s / 2;
  24. for (int i = 0; i < s; i++) {
  25. if (i != mid) v1.push_back(abs(v[mid] - v[i]));
  26. }
  27. for (auto it : v1) {
  28. if (it % d == 0)
  29. ans += (it / d);
  30. else
  31. f = 1;
  32. }
  33. if(f==0)
  34. cout<<ans<<endl;
  35. else
  36. cout<<"-1"<<endl;
  37. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.