Advertisement
Guest User

AUE

a guest
Oct 15th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. void Merge(const int* a, int aLen, const int* b, int bLen, int* c, int64_t &count){
  5. int i = 0, j = 0;
  6. while (i < aLen && j < bLen){
  7. if (a[i] <= b[j]){
  8. c[i + j] = a[i];
  9. ++i;
  10. } else {
  11. c[i + j] = b[j];
  12. count += aLen - i;
  13. ++j;
  14. }
  15. }
  16. if (i == aLen){
  17. for ( ; j < bLen; ++j)
  18. c[i + j] = b[j];
  19. } else {
  20. for ( ; i < aLen; ++i)
  21. c[i + j] = a[i];
  22. }
  23. }
  24.  
  25. void MergeSort(int* a, int aLen, int64_t &count){
  26. if (aLen <= 1){
  27. return;
  28. }
  29. int firstLen = aLen / 2;
  30. int secondLen = aLen - firstLen;
  31. MergeSort( a, firstLen, count );
  32. MergeSort( a + firstLen, secondLen, count );
  33. int* c = new int[aLen];
  34. Merge( a, firstLen, a + firstLen, secondLen, c, count );
  35. memcpy( a, c, sizeof( int ) * aLen );
  36. delete[] c;
  37. }
  38.  
  39. int main()
  40. {
  41. int64_t k = 0;
  42. int64_t &count = k;
  43. int* a = new int [1000000];
  44. int c = 0;
  45. int aLen = 0;
  46. while (std::cin >> c ){
  47. a[aLen] = c;
  48. ++aLen;
  49. }
  50. MergeSort(a, aLen, count);
  51. std::cout << count;
  52. delete[] a;
  53. return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement