Advertisement
exmkg

Untitled

Dec 16th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner in = new Scanner(System.in);
  6.         int n = in.nextInt();
  7.         int[] nums = new int[n];
  8.         for (int i = 0; i < n; i++) {
  9.             nums[i] = in.nextInt();
  10.         }
  11.         System.out.println(countInversions(nums));
  12.     }
  13.  
  14.     public static long countInversions(int[] nums) {
  15.         return mergeSort(nums, 0, nums.length - 1);
  16.     }
  17.  
  18.     public static long mergeSort(int[] arr, int left, int right) {
  19.         // If the subarray has only one element,
  20.         // it doesn't have any inversions within it.
  21.         if (left == right) return 0;
  22.        
  23.         int mid = (left + right) / 2;
  24.  
  25.         long result = 0;
  26.  
  27.         // Count all inversions such that the first element
  28.         // is in the left half [left; mid] and the second is, too.
  29.         result += mergeSort(arr, left, mid);
  30.  
  31.         // Count all inversions such that the first element
  32.         // is in the right half [mid + 1; right] and the second is, too.
  33.         result += mergeSort(arr, mid + 1, right);
  34.  
  35.         // Count all inversions such that the first element
  36.         // is in the left half [left; mid] and the second
  37.         // is in the right half [mid + 1; right].
  38.         result += mergeSortedArrays(arr, left, right);
  39.  
  40.         return result;
  41.     }
  42.  
  43.     public static long mergeSortedArrays(int[] arr, int left, int right) {
  44.         int len = right - left + 1;
  45.         int[] tmp = new int[len];
  46.         int mid = (left + right) / 2;
  47.         long inversions = 0;
  48.         for (int k = 0, i = left, j = mid + 1; k < len; k++) {
  49.             if (i > mid) tmp[k] = arr[j++];
  50.             else if (j > right) tmp[k] = arr[i++];
  51.             else if (arr[i] < arr[j]) tmp[k] = arr[i++];
  52.             else {
  53.                 tmp[k] = arr[j++];
  54.                 // Elements at indices [i; mid] from the left half
  55.                 // are forming inversions with the element at index j
  56.                 // in the right half, e.g. (arr[i], arr[j]) is an inversion,
  57.                 // (arr[i + 1], arr[j]) is an inversion, ..., (arr[mid], arr[j]) is an inversion.
  58.                 // Hence, (mid - i + 1) inversions
  59.                 // are added to the total count of inversions such that
  60.                 // the first element is in the left half [left; mid] and
  61.                 // the second is in the right half [mid + 1; right].
  62.                 inversions += mid - i + 1;
  63.             }
  64.         }
  65.         for (int k = 0, i = left; k < len; k++, i++) {
  66.             arr[i] = tmp[k];
  67.         }
  68.         return inversions;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement