SHARE
TWEET

Untitled

a guest May 25th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. #include <functional>
  4. using namespace std;
  5.  
  6. int count=0;
  7.  
  8. void merge(int mas[], int left, int mid, int right) {
  9.     int i, j, k;
  10.     int n1 = mid - left + 1;
  11.     int n2 =  right - mid;
  12.  
  13.  
  14.     int L[n1], R[n2];
  15.  
  16.     for (int i = 0; i < n1; i++)
  17.         L[i] = mas[left + i];
  18.     for (int j = 0; j < n2; j++)
  19.         R[j] = mas[mid + 1+ j];
  20.  
  21.     i = j = 0;
  22.     k = left;
  23.     while (i < n1 && j < n2)
  24.     {
  25.         if (L[i] <= R[j])
  26.         {
  27.             mas[k] = L[i];
  28.             i++;
  29.         }
  30.         else
  31.         {
  32.             mas[k] = R[j];
  33.             j++;
  34.             count=count+(n1-i);
  35.         }
  36.  
  37.         k++;
  38.     }
  39.     while (i < n1)
  40.     {
  41.         mas[k] = L[i];
  42.         i++;
  43.         k++;
  44.     }
  45.  
  46.     while (j < n2)
  47.     {
  48.         mas[k] = R[j];
  49.         j++;
  50.         k++;
  51.     }
  52.  
  53. }
  54.  
  55. void merge_sort(int *mas,int left, int right) {
  56.     int n=0;
  57.     int mid;
  58.     if (right<=left)
  59.     {
  60.         exit;
  61.     }
  62.     else
  63.     {
  64.         mid = (left+right)/2;
  65.         merge_sort(mas,left,mid);
  66.         merge_sort(mas,mid+1,right);
  67.         merge(mas,left,mid,right);
  68.     }
  69.  
  70. }
  71.  
  72. int main() {
  73.     ifstream input("D:\\pascal\\input.txt");
  74.     ofstream output("output.txt");
  75.  
  76.     int n=0;
  77.     input >> n;
  78.     int a[n-1];
  79.     for (int i=0; i<n; i++)
  80.     {
  81.         input>>a[i];
  82.     }
  83.  
  84.     merge_sort(a,0,n-1);
  85.     cout<< count<< endl;
  86.     output << count;
  87.     return 0;
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top