Advertisement
leminhkt

noname48

Apr 28th, 2020
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define sqr(x) (x)*(x)
  3. #define FOR(i, a, b) for (int i = a; i <= b; i++)
  4. using namespace std;
  5. int n, t, d1, d2, res = 0, te1, te2, MAX;
  6. vector<int> v, v1;
  7. int main()
  8. {
  9.     // freopen("CHINHPHUONG.cpp", "r", stdin);
  10.     // freopen("CHINHPHUONG.out", "w", stdout);
  11.  
  12.     cin >> t;
  13.     FOR(i, 1, t)
  14.     {
  15.         cin >> n;
  16.         te1 = (int)sqrt(n);
  17.         te2 = te1 + 1;
  18.         if (sqr(te1) != n)
  19.         {
  20.             v.push_back(min((n - sqr(te1)), (sqr(te2) - n)));
  21.             d1++;
  22.         }
  23.         else
  24.         {
  25.             d2++;
  26.             if (n == 0) v1.push_back(2);
  27.             else v1.push_back(1);
  28.         }
  29.     }
  30.     if (d1 == d2){
  31.         cout << 0; return 0;
  32.     }
  33.     if (d1 > d2){
  34.         sort(v.begin(), v.end(), greater<int>());
  35.         FOR(i, t/2, d1 - 1)
  36.         {
  37.             res += v.back();
  38.             // cout << res << "\n";
  39.             v.pop_back();
  40.         }
  41.     }
  42.     else{
  43.         sort(v1.begin(), v1.end(), greater<int>());
  44.         FOR(i, t/2, d2 - 1)
  45.         {
  46.             res += v1.back();
  47.             // cout << res << "\n";
  48.             v1.pop_back();
  49.         }
  50.     }
  51.     // FOR(i, 0, 5) cout << v[i] << " ";
  52.     // FOR(i, 0, 5) cout << v1[i] < " ";
  53.     // cout << d1 << " " << d2 << "\n";
  54.     cout << res;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement