Facebook
From Putrid Horse, 2 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 292
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. typedef pair<int, int> pii;
  7. typedef pair<ll, ll> pll;
  8. typedef vector<long long> vll;
  9. typedef vector<int> vi;
  10.  
  11. #define io ios_base::sync_with_stdio(false)
  12. #define pb push_back
  13. #define isSet(n, i) bool(n & (1LL << i))
  14. #define Set(n, i) (n | (1LL << i))
  15. #define unSet(n, i) (n & !(1LL << i))
  16. #define PI 2 * acos(0.0)
  17. #define all(r) (r).begin(), (r).end()
  18. #define rev(r) (r).rbegin(), (r).rend()
  19. #define dbg(a) cout << #a << " ->->->-> " << a << "\n"
  20. #define inf 1000000000000000000
  21. #define mod 1000000007
  22. #define N 1000000
  23.  
  24. int dirx[] = {1, -1, 0, 0, 1, 1, -1, -1}, diry[] = {0, 0, 1, -1, 1, -1, 1, -1};
  25.  
  26. map<pll, pll> ans;
  27. map<pll, int> pres;
  28.  
  29. ll md(pll a, pll b)
  30. {
  31.     return abs(a.first - b.first) + abs(a.second - b.second);
  32. }
  33.  
  34. void rec(int x, int y)
  35. {
  36.     for (int i = 0; i < 4; i++)
  37.     {
  38.         int tx = x + dirx[i], ty = y + diry[i];
  39.         if (tx <= N && tx >= -N && ty <= N && ty >= -N)
  40.         {
  41.             if (pres[{tx, ty}] == 0)
  42.             {
  43.                 ans[{x, y}] = {tx, ty};
  44.                 return;
  45.             }
  46.         }
  47.     }
  48.     for (int i = 0; i < 4; i++)
  49.     {
  50.         int tx = x + dirx[i], ty = y + diry[i];
  51.         if (tx <= N && tx >= -N && ty <= N && ty >= -N)
  52.         {
  53.             if (ans[{tx, ty}].first == INT_MAX)
  54.             {
  55.                 rec(tx, ty);
  56.             }
  57.             if (i == 0)
  58.             {
  59.                 ans[{x, y}] = ans[{tx, ty}];
  60.             }
  61.             else if (md(ans[{tx, ty}], {x, y}) < md(ans[{x, y}], {x, y}))
  62.             {
  63.                 ans[{x, y}] = ans[{tx, ty}];
  64.             }
  65.         }
  66.     }
  67. }
  68.  
  69. int main()
  70. {
  71.     io;
  72.     ifstream in("input.txt");
  73.     ofstream out("output.txt");
  74.     int n;
  75.     cin >> n;
  76.     vector<pll> inp(n);
  77.     for (int i = 0; i < n; i++)
  78.     {
  79.         ll x, y;
  80.         cin >> x >> y;
  81.         inp[i] = {x, y};
  82.         pres[{x, y}] = 1;
  83.         ans[{x, y}] = {INT_MAX, INT_MAX};
  84.     }
  85.     for (int i = 0; i < n; i++)
  86.     {
  87.         if (ans[inp[i]].first == INT_MAX)
  88.         {
  89.             rec(inp[i].first, inp[i].second);
  90.         }
  91.     }
  92.     for (int i = 0; i < n; i++)
  93.     {
  94.         cout << ans[inp[i]].first << " " << ans[inp[i]].second << "\n";
  95.     }
  96.     return 0;
  97. }