Advertisement
akashtadwai

gnupbds ordered set

Oct 2nd, 2021
1,470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #include <ext/pb_ds/assoc_container.hpp>
  4. #include <ext/pb_ds/tree_policy.hpp>
  5. using namespace __gnu_pbds;
  6. #define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  7.  
  8. int main() {
  9.     int t;
  10.     cin >> t;
  11.     while(t--){
  12.         int n;
  13.         cin >> n;
  14.         long long a[n], b[n] = {0ll};
  15.         for(int i=0; i<n; i++) cin >> a[i];
  16.         ordered_set s;
  17.         s.insert(-a[0]);
  18.         for(int i=1; i<n; i++){
  19.             b[i] = s.order_of_key(-a[i]);
  20.             s.insert(-a[i]);
  21.         }
  22.         map<int, int> mp;
  23.         long long ans = 0;
  24.         for(int i=0; i<n; i++){
  25.             ans += min(b[i], i-b[i]-mp[a[i]]);
  26.             mp[a[i]]++;
  27.         }
  28.         cout << ans << endl;
  29.     }
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement