Codeforces Round 872 (Div. 2) 1825B - LuoTianyi and the Table Solution


 

Problem Link : https://codeforces.com/contest/1825/problem/B

Solution in C++:

  1. /// Author : AH_Tonmoy
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int32_t main() {
  5. ios_base::sync_with_stdio(0);
  6. cin.tie(0);
  7. int t;
  8. cin >> t;
  9. while (t--) {
  10. int n , m ;
  11. cin >> n >> m ;
  12. if ( n > m ) swap(n , m);
  13. vector<int>a(n*m) ;
  14. for ( int i = 0 ; i < n * m ; i++ ){
  15. cin >> a[i] ;
  16. }
  17. sort(a.begin(),a.end()) ;
  18. int mnf = a[0] ;
  19. int mns = a[1] ;
  20. int mxf = a[n*m - 1] ;
  21. int mxs = a[n*m - 2] ;
  22. int ans1 = ((n*m)- n) * (mxf-mnf) ;
  23. for ( int i = 1 ; i <= n - 1 ; i++) {
  24. ans1 +=(mxs-mnf) ;
  25. }
  26. int ans2 = ((n*m)- n) * (mxf-mnf) ;
  27. for ( int i = 1 ; i <= n - 1 ; i++) {
  28. ans2 +=(mxf-mns) ;
  29. }
  30. cout << max(ans1,ans2) << "\n";
  31. }
  32. }

No comments

Most View Post

Recent post

Codeforces Round 925 (Div. 3) 1931D. Divisible Pairs Solution

    Problem Link  :   https://codeforces.com/contest/1931/problem/D S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. ...

Powered by Blogger.