Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace __gnu_pbds;
- #define ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
- int main() {
- int t;
- cin >> t;
- while(t--){
- int n;
- cin >> n;
- long long a[n], b[n] = {0ll};
- for(int i=0; i<n; i++) cin >> a[i];
- ordered_set s;
- s.insert(-a[0]);
- for(int i=1; i<n; i++){
- b[i] = s.order_of_key(-a[i]);
- s.insert(-a[i]);
- }
- map<int, int> mp;
- long long ans = 0;
- for(int i=0; i<n; i++){
- ans += min(b[i], i-b[i]-mp[a[i]]);
- mp[a[i]]++;
- }
- cout << ans << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement