Codeforces Round 920 (Div. 3) 1921D. Very Different Array Solution


 

Problem Link : https://codeforces.com/problemset/problem/1921/D

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 ; cin >> n >> m ;
  11. vector<int> a(n),b(m);
  12. for (int i = 0 ; i < n ; i++){
  13. cin >> a[i] ;
  14. }
  15. for ( int i = 0 ; i < m ; i++){
  16. cin >> b[i] ;
  17. }
  18. sort(a.begin(),a.end()) ;
  19. sort(b.rbegin(),b.rend()) ;
  20. int i = 0 , l = 0 , j = n - 1 , r = m - 1 ;
  21. long long ans = 0 ;
  22. int mx ;
  23. while(i <= j){
  24. int left = abs(a[i] - b[l]) ;
  25. int right = abs (a[j] - b[r]);
  26. int mx = max(left,right);
  27. ans += mx ;
  28. if ( left == mx ){
  29. i++ ;
  30. l++ ;
  31. }
  32. else{
  33. j--;
  34. r-- ;
  35. }
  36. }
  37. cout << ans <<'\n';
  38. }
  39. return 0 ;
  40. }

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.