Advertisement
uzimane_

Сортировка по нескольким критериям

Jul 10th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. long long int n;
  7. long long int* A;
  8. long long int* B;
  9. long long int* Z;
  10. long long int counter = 1;
  11. bool flag = false;
  12.  
  13. void merge_series(long long int* A, long long int*B, long long int*Z, long long int b, long long int c, long long int e, long long int* D, long long int* C, long long int*Y)
  14. {
  15. long long int i = b, j = c + 1, k;
  16. for (k = b; k <= e; k++)
  17. if (j > e)
  18. {
  19. D[k] = A[i];
  20. Y[k] = Z[i];
  21. C[k] = B[i++];
  22. }
  23. else
  24. if (i > c)
  25. {
  26. D[k] = A[j];
  27. C[k] = B[j];
  28. Y[k] = Z[j++];
  29. }
  30. else
  31. if (A[i] >= A[j])
  32. {
  33. if (A[i] > A[j])
  34. {
  35. D[k] = A[i];
  36. C[k] = B[i];
  37. Y[k] = Z[i];
  38. }
  39. if (A[i] == A[j])
  40. {
  41. if (B[i] <= B[j])
  42. {
  43. if (B[i] < B[j])
  44. {
  45. D[k] = A[i];
  46. C[k] = B[i];
  47. Y[k] = Z[i];
  48. }
  49. if (B[i] == B[j])
  50. {
  51. if (Z[i] < Z[j])
  52. {
  53. D[k] = A[i];
  54. C[k] = B[i];
  55. Y[k] = Z[i];
  56. }
  57. else
  58. {
  59. D[k] = A[j];
  60. C[k] = B[j];
  61. Y[k] = Z[j];
  62. }
  63. }
  64. }
  65. else
  66. {
  67. D[k] = A[j];
  68. C[k] = B[j];
  69. Y[k] = Z[j];
  70. j++;
  71. flag = true;
  72. }
  73. }
  74. if (flag == false)
  75. {
  76. i++;
  77. }
  78. flag = false;
  79. }
  80. else
  81. {
  82. D[k] = A[j];
  83. Y[k] = Z[j];
  84. C[k] = B[j++];
  85. }
  86. }
  87.  
  88. void merge_rec(long long int* A, long long int*B, long long int*Z, long long int b, long long int e, long long int* D, long long int* C, long long int*Y)
  89. {
  90. long long int c = (b + e) / 2;
  91. if (b < c)
  92. merge_rec(A,B,Z, b, c, D, C,Y);
  93. if (c + 1 < e)
  94. merge_rec(A, B,Z, c + 1, e, D, C, Y);
  95. merge_series(A, B,Z, b, c, e, D, C,Y);
  96. for (int i = b; i <= e; i++)
  97. {
  98. A[i] = D[i];
  99. B[i] = C[i];
  100. Z[i] = Y[i];
  101. }
  102. }
  103.  
  104. void merge_sort(long long int* A, int n)
  105. {
  106. long long int* D = new long long int[n];
  107. long long int* C = new long long int[n];
  108. long long int* Y = new long long int[n];
  109. merge_rec(A, B, Z, 0, n - 1, D, C, Y);
  110. delete[] D;
  111. }
  112.  
  113. int main()
  114. {
  115. freopen("input.txt", "r", stdin);
  116. freopen("output.txt", "w", stdout);
  117.  
  118. cin >> n;
  119. A = new long long int[n];
  120. B = new long long int[n];
  121. Z = new long long int[n];
  122. for (int i = 0; i < n; i++)
  123. {
  124. cin >> A[i];
  125. cin >> B[i];
  126. Z[i] = counter; counter++;
  127. }
  128. merge_sort(A, n);
  129. for (int i = 0; i < n; i++)
  130. {
  131. //cout << A[i] << " " << B[i] << " " << Z[i] << endl;
  132. cout << Z[i] << " ";
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement