1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <algorithm> #include <utility> #include <vector> #include <istream> #include <map> #include <cmath> #include <cstring> #include <string> #define ll long long #define maxn 200005 #define mdl 998244353 #define clr(a,n) for(int i=0;i<n;i++)a[i]=0 using namespace std; struct node { ll x, y; friend bool operator<(node n1, node n2) { if (n1.y == n2.y) { return n1.x < n2.x; } else return n1.y > n2.y; } friend bool operator>(node n1, node n2) { if (n1.y == n2.y) { return n1.x > n2.x; } else return n1.y < n2.y; } }pts[maxn],opts[maxn]; bool cmp1(node n1, node n2) { return n1 < n2; } bool cmp2(node n1, node n2) { return n1 > n2; } map<node, node> dp; map<node, ll> exi,mi; ll cnt(node n1, node n2) { return abs(n1.x - n2.x) + abs(n1.y - n2.y); }
int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n; for (int i = 0; i < n; i++) { scanf("%lld%lld", &pts[i].x, &pts[i].y); opts[i].x = pts[i].x; opts[i].y = pts[i].y; node d = { pts[i].x,pts[i].y }; exi[d] = 1; } sort(pts, pts + n,cmp1); for (int i = 0; i < n; i++) { node d0 = { pts[i].x,pts[i].y }; if (mi[d0] == 0)mi[d0] = 1e9; node d1 = { pts[i].x - 1,pts[i].y }; node d2 = { pts[i].x,pts[i].y + 1 }; if (exi[d1] == 0) { if (cnt(d0, d1) <= mi[d0]) { mi[d0] = cnt(d0, d1); dp[d0] = d1; } } else { if (cnt(d0, dp[d1]) <= mi[d0]) { mi[d0] = cnt(d0, dp[d1]); dp[d0] = dp[d1]; } } if (exi[d2] == 0) { if (cnt(d0, d2) <= mi[d0]) { mi[d0] = cnt(d0, d2); dp[d0] = d2; } } else { if (cnt(d0, dp[d2]) <= mi[d0]) { mi[d0] = cnt(d0, dp[d2]); dp[d0] = dp[d2]; } } } sort(pts, pts + n, cmp2); for (int i = 0; i < n; i++) { node d0 = { pts[i].x,pts[i].y }; if (mi[d0] == 0)mi[d0] = 1e9; node d1 = { pts[i].x + 1,pts[i].y }; node d2 = { pts[i].x,pts[i].y - 1 }; if (exi[d1] == 0) { if (cnt(d0, d1) <= mi[d0]) { mi[d0] = cnt(d0, d1); dp[d0] = d1; } } else { if (cnt(d0, dp[d1]) <= mi[d0]) { mi[d0] = cnt(d0, dp[d1]); dp[d0] = dp[d1]; } } if (exi[d2] == 0) { if (cnt(d0, d2) <= mi[d0]) { mi[d0] = cnt(d0, d2); dp[d0] = d2; } } else { if (cnt(d0, dp[d2]) <= mi[d0]) { mi[d0] = cnt(d0, dp[d2]); dp[d0] = dp[d2]; } } } for (int i = 0; i < n; i++) { printf("%lld %lld\n", dp[opts[i]].x, dp[opts[i]].y); }
}
|