Advertisement
ilnazEPTA

Untitled

Dec 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. void qs_helper(int* A, int l, int r)
  4. {
  5. if (r > l)
  6. {
  7. int i = 0, j, k, pe;
  8. int m = (l + r) / 2;
  9. if (A[l] < A[m])
  10. if (A[m] < A[r])
  11. swap(A[l], A[m]);
  12. else
  13. if (A[l] < A[r])
  14. swap(A[l], A[r]);
  15. else
  16. if (A[r] < A[m])
  17. swap(A[l], A[m]);
  18. else
  19. if (A[r] < A[l])
  20. swap(A[l], A[r]);
  21. pe = A[i]; i = l; j = l + 1; k = r;
  22. while (j <= k)
  23. if (A[j] == pe)
  24. j++;
  25. else
  26. if (A[j] < pe)
  27. {
  28. swap(A[i], A[j]);
  29. i++; j++;
  30. }
  31. else
  32. {
  33. swap(A[j], A[k]);
  34. k--;
  35. }
  36.  
  37.  
  38.  
  39. }
  40.  
  41.  
  42. }
  43. void quick_sort(int* A, int n)
  44. {
  45. qs_helper(A, 0, n - 1);
  46. }
  47. int main()
  48. {
  49. int n;
  50. cin >> n;
  51. int t;
  52. cin >> t;
  53.  
  54. int* a = new int[n];
  55. for (int i = 0; i < n; i++)
  56. cin >> a[i];
  57. quick_sort(a, n);
  58. int i = 0;
  59. int j = n - 1;
  60. int res = 0;
  61. while (i < j)
  62. {
  63. if (a[i] + a[j] > t)
  64. j--;
  65. if (a[i] + a[j] < t)
  66. i++;
  67. else
  68. {
  69. res++;
  70. i++;
  71. j--;
  72. }
  73.  
  74. }
  75. cout << res;
  76.  
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement