Codeforces Round 861 (Div. 2) 1808B - Playing in a Casino Solution


 

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

Solution in C++:

  1. /// Author : AH_Tonmoy
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define int long long
  5. int32_t main() {
  6. ios_base::sync_with_stdio(0);
  7. cin.tie(0);
  8. int t;
  9. cin >> t;
  10. while (t--) {
  11. int n , m ; cin >> n >> m ;
  12. std::vector<int> v[m];
  13. for ( int i = 0 ; i < n ; i++){
  14. for ( int j = 0 ; j < m ; j++){
  15. int x ;
  16. cin >> x ;
  17. v[j].push_back(x) ;
  18. }
  19. }
  20. for( int i = 0 ; i < m ; i++){
  21. sort(v[i].begin(),v[i].end()) ;
  22. }
  23. int ans = 0 ;
  24. for ( int i = 0 ; i < m ; i++) {
  25. vector<int>r(n,0) ;
  26. r[n-1] = v[i][n-1] ;
  27. for ( int j = n - 2 ; j >= 0 ; j--) {
  28. r[j] = r[j+1] + v[i][j] ;
  29. }
  30. for ( int j = 0 ; j < n - 1 ; j++) {
  31. int cnt = r[j+1] - (n -1 -j)*v[i][j] ;
  32. ans +=cnt ;
  33. }
  34. }
  35. cout << ans << endl ;
  36. }
  37. return 0 ;
  38. }

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.