Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define sz(x) ((int) (x).size())
  6. #define all(x) (x).begin(), (x).end()
  7. #define fi first
  8. #define se second
  9. #define mp make_pair
  10. #define push_back
  11. #define re return
  12.  
  13. using ll = long long;
  14. using ull = unsigned long long;
  15. using ii = pair<int, int>;
  16. using vi = vector<int>;
  17. using vii = vector<ii>;
  18. using ld = long double;
  19.  
  20. template <class T> T abs (T x) { re x > 0 ? x : -x; }
  21. template <class T> T sqr (T x) { re x * x; }
  22.  
  23. const ld pi = acos(1.);
  24. const int inf = 1e9 + 7;
  25. const int N = 1e7;
  26.  
  27. int n, m;
  28. unsigned int a[N], x, y, cur;
  29.  
  30. unsigned int nextRand24() {
  31. cur = cur * x + y;
  32. return cur >> 8;
  33. }
  34. unsigned int nextRand32() {
  35. unsigned int a = nextRand24(), b = nextRand24();
  36. return (a << 8) ^ b;
  37. }
  38.  
  39. unsigned int get (int l, int r, int k) {
  40. if (r - l < 100) {
  41. for (int i = l; i < r; i++)
  42. for (int j = i + 1; j <= r; j++)
  43. if (a[i] > a[j]) {
  44. swap (a[i], a[j]);
  45. }
  46. re a[l + k];
  47. }
  48. auto c = (l + r) >> 1;
  49. int pos = l;
  50. while (pos < c) {
  51. if (a[pos] > a[c]) {
  52. swap(a[pos], a[c - 1]);
  53. swap(a[c - 1], a[c]);
  54. c--;
  55. } else {
  56. pos++;
  57. }
  58. }
  59. pos = r;
  60. while (pos > c) {
  61. if (a[pos] < a[c]) {
  62. swap(a[pos], a[c + 1]);
  63. swap(a[c + 1], a[c]);
  64. c++;
  65. } else {
  66. pos--;
  67. }
  68. }
  69. if (c == k + l) re a[c];
  70. if (c > k + l) re get (l, c - 1, k);
  71. else re get (c + 1, r, k - c + l - 1);
  72. }
  73.  
  74. int main() {
  75. cin >> n >> x >> y;
  76. for (int i = 0; i < n; i++) a[i] = nextRand32();
  77. ll mid = get (0, n - 1, n / 2);
  78. ll ans = 0;
  79. for (int i = 0; i < n; i++) ans += abs(a[i] - mid);
  80. cout << ans << endl;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement