Advertisement
newb_ie

bs2

Aug 27th, 2021
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. long long binary_search (vector<int> &a,int n) {
  5. long long ans = 0;
  6. for (int i = 1; i <= n; ++i) {
  7. int l = 1,r = i - 1;
  8. int res = 0;
  9. while (l <= r) {
  10. int m = (l + r) / 2;
  11. if (a[m] < a[i]) {
  12. res = m;
  13. l = m + 1;
  14. } else {
  15. r = m - 1;
  16. }
  17. }
  18. ans += res * 2;
  19. }
  20. return ans;
  21. }
  22.  
  23. int main () {
  24. ios::sync_with_stdio(false);
  25. cin.tie(nullptr);
  26. cout.tie(nullptr);
  27. int T;
  28. cin >> T;
  29. for (int test_case = 1; test_case <= T; ++test_case) {
  30. int n;
  31. cin >> n;
  32. vector<int> a(n + 1);
  33. for (int i = 1; i <= n; ++i) cin >> a[i];
  34. sort (a.begin() + 1,a.end());
  35. cout << binary_search(a,n) << '\n';
  36. //~ int n;
  37. //~ cin >> n;
  38. //~ int a[n + 1];
  39. //~ map<int,int> m;
  40. //~ long long res = 0;
  41. //~ for (int i = 1; i <= n; ++i) cin >> a[i];
  42. //~ sort (a + 1,a + n + 1);
  43. //~ for (int i = 1; i <= n; ++i) {
  44. //~ //a[i] => i,j
  45. //~ int length = i - 1;
  46. //~ length -= m[a[i]];
  47. //~ res += length * 2;
  48. //~ m[a[i]]++;
  49. //~ }
  50. //~ cout << res << '\n';
  51. }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement