Advertisement
Guest User

e.cpp

a guest
Dec 16th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cmath>
  5.  
  6. typedef int Int;
  7.  
  8. Int sqr(Int x) {
  9.     return x * x;
  10. }
  11.  
  12. Int low_sqr(Int x) {
  13.     return sqr(Int(std::floor(std::sqrt(x))));
  14. }
  15.  
  16. Int high_sqr(Int x) {
  17.     return sqr(Int(std::ceil(std::sqrt(x))));
  18. }
  19.  
  20. struct Pair {
  21.     Int value, dist;
  22.    
  23.     Pair(Int v)
  24.         : value(v)
  25.         , dist(
  26.             std::min(
  27.                 std::abs(v-high_sqr(v)),
  28.                 std::abs(v-low_sqr(v))
  29.             )
  30.         )
  31.     { }
  32.    
  33.     Pair(Int v, Int dist)
  34.         : value(v)
  35.         , dist(dist)
  36.     { }
  37. };
  38.  
  39. int main() {
  40. //  freopen("input.txt", "rt", stdin);
  41.    
  42.     std::ios_base::sync_with_stdio(false);
  43.     std::cin.tie(0);
  44.    
  45.     Int n;
  46.     std::cin >> n;
  47.    
  48.     std::vector<Pair> a;
  49.     a.reserve(n);
  50.    
  51.     for (Int i = 0; i < n; ++i) {
  52.         Int v;
  53.         std::cin >> v;
  54.         a.push_back(Pair(v));
  55.     }
  56.    
  57.     // Вначале лежат полные квадраты
  58.     std::sort(a.begin(), a.end(), [](const Pair& a, const Pair& b) {
  59.         return a.dist < b.dist || (a.dist == b.dist && a.value == 0);
  60.     });
  61.    
  62.     auto it = a.begin();
  63.     for (int i = 0; i < n / 2; ++i) {
  64.         it++;
  65.     }
  66.    
  67.     // Если после половины остались нули, убираем их в конец
  68.     std::sort(it, a.end(), [](const Pair& a, const Pair& b) {
  69.         return a.dist < b.dist || (a.dist == b.dist && b.value == 0);
  70.     });
  71.    
  72.     // Из первой половины делаем полные квадраты
  73.     Int answ = 0;
  74.     for (Int i = 0; i < n / 2; ++i) {
  75.         answ += a[i].dist;
  76.     }
  77.    
  78.     // Вторые изменяем на неполные квадраты
  79.     for (Int i = n / 2; i < n; ++i) {
  80.         if (a[i].value == 0) {
  81.             answ += 2;
  82.         } else if (a[i].dist == 0) {
  83.             answ++;
  84.         }
  85.     }
  86.     std::cout << answ;
  87.  
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement