Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- void Merge(const int* a, int aLen, const int* b, int bLen, int* c, int64_t &count){
- int i = 0, j = 0;
- while (i < aLen && j < bLen){
- if (a[i] <= b[j]){
- c[i + j] = a[i];
- ++i;
- } else {
- c[i + j] = b[j];
- count += aLen - i;
- ++j;
- }
- }
- if (i == aLen){
- for ( ; j < bLen; ++j)
- c[i + j] = b[j];
- } else {
- for ( ; i < aLen; ++i)
- c[i + j] = a[i];
- }
- }
- void MergeSort(int* a, int aLen, int64_t &count){
- if (aLen <= 1){
- return;
- }
- int firstLen = aLen / 2;
- int secondLen = aLen - firstLen;
- MergeSort( a, firstLen, count );
- MergeSort( a + firstLen, secondLen, count );
- int* c = new int[aLen];
- Merge( a, firstLen, a + firstLen, secondLen, c, count );
- memcpy( a, c, sizeof( int ) * aLen );
- delete[] c;
- }
- int main()
- {
- int64_t k = 0;
- int64_t &count = k;
- int* a = new int [1000000];
- int c = 0;
- int aLen = 0;
- while (std::cin >> c ){
- a[aLen] = c;
- ++aLen;
- }
- MergeSort(a, aLen, count);
- std::cout << count;
- delete[] a;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement