Advertisement
Alex239

Untitled

Dec 3rd, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. #include <vector>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7. typedef long long nagai;
  8.  
  9. int find_med(vector<int> a)
  10. {
  11.     int BC = 1 << 15;
  12.     int BS = 1 << 15;
  13.     vector<int> cnt(BC);
  14.     for (auto& x : a)
  15.         ++cnt[x >> 15];
  16.     int curc = a.size() / 2;
  17.     int ans = -1;
  18.     for (int i = 0; i < BC; ++i)
  19.         if (curc >= cnt[i])
  20.             curc -= cnt[i];
  21.         else
  22.         {
  23.             ans = i;
  24.             break;
  25.         }
  26.     cnt.assign(BS, 0);
  27.     for (auto& x : a)
  28.         if ((x >> 15) == ans)
  29.             ++cnt[x & ((1 << 15) - 1)];
  30.     int ans1 = -1;
  31.     for (int i = 0; i < BS; ++i)
  32.         if (curc >= cnt[i])
  33.             curc -= cnt[i];
  34.         else
  35.         {
  36.             ans1 = i;
  37.             break;
  38.         }
  39.     return (ans << 15) + ans1;
  40.  
  41. }
  42.  
  43. int main()
  44. {
  45.     ifstream cin("makesaw.in");
  46.     ofstream cout("makesaw.out");
  47.     int t; cin >> t;
  48.     int n; cin >> n;
  49.     vector<int> a(n);
  50.     if (t == 1)
  51.         for (auto& x : a)
  52.             cin >> x;
  53.     else
  54.     {
  55.         int a1, x, y, z;
  56.         cin >> a1 >> x >> y >> z;
  57.         a[0] = a1;
  58.         for (int i = 1; i < n; ++i)
  59.             a[i] = (1LL * a[i - 1] * x + y) % z;
  60.     }
  61.     vector<int> a1, b1;
  62.     for (int i = 0; i < n; ++i)
  63.         if (i & 1)
  64.             b1.push_back(a[i]);
  65.         else
  66.             a1.push_back(a[i]);
  67.     int amed = find_med(a1);
  68.     int bmed = find_med(b1);
  69.     nagai ans = 0;
  70.     for (auto& x : a1)
  71.         ans += abs(x - amed);
  72.     for (auto& x : b1)
  73.         ans += abs(x - bmed);
  74.     cout << ans << endl;
  75.     return 0;
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement