Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <fstream>
- using namespace std;
- ifstream fin("meh.txt");
- int mergeIt(vector<int> mV, int start, int middle, int end)
- {
- int count = 0;
- int b[end - start +1];
- int i = start, j = middle + 1, poz=0;
- while ((i <= middle)&&( j <= end))
- {
- if (i < j && mV[i] > 2*mV[j])
- {
- count = count + j - middle;
- b[poz] = mV[i];
- i++;
- }
- else
- {
- b[poz] = mV[j];
- j++;
- }
- poz++;
- }
- while(i <= middle)
- {
- if (j <= end && (i < j && mV[i] > 2*mV[j]))
- count = count + (j - middle);
- b[poz] = mV[i];
- poz++;
- i++;
- }
- while(j <= end)
- {
- b[poz] = mV[j];
- poz++;
- j++;
- }
- for (i = start; i <= end; i++)
- mV[i] = b[i - start];
- return count;
- }
- int mergeSort(vector<int> mV, int start, int end)
- {
- int result1, result2, result3;
- if (start == end)
- {
- return 0;
- }
- else
- {
- int middle = (start + end) / 2;
- result1 = mergeSort(mV, start, middle);
- result2 = mergeSort(mV, middle + 1, end);
- result3 = mergeIt(mV, start, middle, end);
- }
- return result1+result2+result3;
- }
- int main()
- {
- vector<int> myVector;
- int n, x;
- fin >> n;
- while (fin >> x)
- myVector.push_back(x);
- cout << "Nr. de inversiuni semnificative este " << mergeSort(myVector, 0, n - 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement