Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- long long binary_search (vector<int> &a,int n) {
- long long ans = 0;
- for (int i = 1; i <= n; ++i) {
- int l = 1,r = i - 1;
- int res = 0;
- while (l <= r) {
- int m = (l + r) / 2;
- if (a[m] < a[i]) {
- res = m;
- l = m + 1;
- } else {
- r = m - 1;
- }
- }
- ans += res * 2;
- }
- return ans;
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int T;
- cin >> T;
- for (int test_case = 1; test_case <= T; ++test_case) {
- int n;
- cin >> n;
- vector<int> a(n + 1);
- for (int i = 1; i <= n; ++i) cin >> a[i];
- sort (a.begin() + 1,a.end());
- cout << binary_search(a,n) << '\n';
- //~ int n;
- //~ cin >> n;
- //~ int a[n + 1];
- //~ map<int,int> m;
- //~ long long res = 0;
- //~ for (int i = 1; i <= n; ++i) cin >> a[i];
- //~ sort (a + 1,a + n + 1);
- //~ for (int i = 1; i <= n; ++i) {
- //~ //a[i] => i,j
- //~ int length = i - 1;
- //~ length -= m[a[i]];
- //~ res += length * 2;
- //~ m[a[i]]++;
- //~ }
- //~ cout << res << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement