Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <cmath>
- #include <algorithm>
- #include <fstream>
- using namespace std;
- typedef long long nagai;
- int find_med(vector<int> a)
- {
- int BC = 1 << 15;
- int BS = 1 << 15;
- vector<int> cnt(BC);
- for (auto& x : a)
- ++cnt[x >> 15];
- int curc = a.size() / 2;
- int ans = -1;
- for (int i = 0; i < BC; ++i)
- if (curc >= cnt[i])
- curc -= cnt[i];
- else
- {
- ans = i;
- break;
- }
- cnt.assign(BS, 0);
- for (auto& x : a)
- if ((x >> 15) == ans)
- ++cnt[x & ((1 << 15) - 1)];
- int ans1 = -1;
- for (int i = 0; i < BS; ++i)
- if (curc >= cnt[i])
- curc -= cnt[i];
- else
- {
- ans1 = i;
- break;
- }
- return (ans << 15) + ans1;
- }
- int main()
- {
- ifstream cin("makesaw.in");
- ofstream cout("makesaw.out");
- int t; cin >> t;
- int n; cin >> n;
- vector<int> a(n);
- if (t == 1)
- for (auto& x : a)
- cin >> x;
- else
- {
- int a1, x, y, z;
- cin >> a1 >> x >> y >> z;
- a[0] = a1;
- for (int i = 1; i < n; ++i)
- a[i] = (1LL * a[i - 1] * x + y) % z;
- }
- vector<int> a1, b1;
- for (int i = 0; i < n; ++i)
- if (i & 1)
- b1.push_back(a[i]);
- else
- a1.push_back(a[i]);
- int amed = find_med(a1);
- int bmed = find_med(b1);
- nagai ans = 0;
- for (auto& x : a1)
- ans += abs(x - amed);
- for (auto& x : b1)
- ans += abs(x - bmed);
- cout << ans << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement