Advertisement
ke_timofeeva7

пираты баренцева моря

May 16th, 2021
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <memory.h>
  8. #include <stdio.h>
  9. #include <vector>
  10. #include <stack>
  11. #include <deque>
  12. #include <queue>
  13. #include <vector>
  14. #include <set>
  15. #include <iterator>
  16. #include <map>
  17. #include <iomanip>
  18. #define int long long
  19. #define sp system("pause")
  20. #define pb push_back
  21. #define double long double
  22. #define endl "\n"
  23. #define un unsigned
  24. #define INF 1000000000
  25. #define EPS 0.0000001
  26. using namespace std;
  27.  
  28. int n;
  29.  
  30. struct point
  31. {
  32.     int x, y;
  33. };
  34.  
  35. istream& operator >> (istream& in, vector<point>& a)
  36. {
  37.     for (int i = 0; i < n; i++)
  38.     {
  39.         in >> a[i].x >> a[i].y;
  40.     }
  41.  
  42.     return in;
  43. }
  44.  
  45. vector <int> vn;
  46.  
  47. int Binary_search(int l, int r, int key)
  48. {
  49.     int m;
  50.  
  51.     while (r > l)
  52.     {
  53.         m = (r + l) / 2;
  54.  
  55.         if (vn[m] >= key)
  56.         {
  57.             r = m;
  58.         }
  59.         else
  60.         {
  61.             l = m + 1;
  62.         }
  63.     }
  64.  
  65.     if (vn[r] == key || r == 0)
  66.     {
  67.         return r;
  68.     }
  69.     else
  70.     {
  71.         int q = abs(key - vn[r]);
  72.         int w = abs(key - vn[r - 1]);
  73.  
  74.         if (q < w)
  75.         {
  76.             return r;
  77.         }
  78.         else
  79.         {
  80.             return r - 1;
  81.         }
  82.     }
  83.  
  84. }
  85.  
  86.  
  87. signed main()
  88. {
  89.     ios_base::sync_with_stdio(0);
  90.     cin.tie();
  91.     cout.tie();
  92.     cout.precision(15);
  93.  
  94.     cin >> n;
  95.  
  96.     vector<point> vc(n);
  97.  
  98.     cin >> vc;
  99.  
  100.     int ANS = INF;
  101.  
  102.     for (int j = 1; j <= n; j++)
  103.     {
  104.         for (int i = 0; i < n; i++)
  105.         {
  106.             vn.pb(i + 1);
  107.         }
  108.  
  109.         int ans = 0;
  110.  
  111.         for (int i = 0; i < n; i++)
  112.         {
  113.             int kek1 = Binary_search(0, n - i - 1, vc[i].x);
  114.  
  115.             ans += abs(vn[kek1] - vc[i].x) + abs(j - vc[i].y);
  116.  
  117.             vn.erase(vn.begin() + kek1, vn.begin() + kek1 + 1);
  118.         }
  119.  
  120.         ANS = min(ans, ANS);
  121.     }
  122.  
  123.     cout << ANS;
  124.  
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement