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
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Point { ll x, y; int id; };
inline void processTestCase() { int n; cin >> n;
vector<Point> points(n); for (int i = 0; i < n; ++i) { cin >> points[i].x >> points[i].y; points[i].id = i + 1; }
auto compareByX = [](const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.id < b.id); };
auto compareByY = [](const Point& a, const Point& b) { return a.y < b.y || (a.y == b.y && a.id < b.id); };
auto compareByYDesc = [](const Point& a, const Point& b) { return a.y > b.y || (a.y == b.y && a.id < b.id); };
auto compareByXDesc = [](const Point& a, const Point& b) { return a.x > b.x || (a.x == b.x && a.id < b.id); };
vector<pair<int, int>> pairsOption1, pairsOption2; ll sumOption1 = 0, sumOption2 = 0;
vector<Point> pointsSortedByX = points; sort(pointsSortedByX.begin(), pointsSortedByX.end(), compareByX);
vector<Point> leftGroup(pointsSortedByX.begin(), pointsSortedByX.begin() + n / 2); vector<Point> rightGroup(pointsSortedByX.begin() + n / 2, pointsSortedByX.end());
sort(leftGroup.begin(), leftGroup.end(), compareByY); sort(rightGroup.begin(), rightGroup.end(), compareByYDesc);
for (int i = 0; i < n / 2; ++i) { ll dx = abs(leftGroup[i].x - rightGroup[i].x); ll dy = abs(leftGroup[i].y - rightGroup[i].y); sumOption1 += dx + dy; int a = leftGroup[i].id, b = rightGroup[i].id; if (a > b) swap(a, b); pairsOption1.push_back({a, b}); }
vector<Point> pointsSortedByY = points; sort(pointsSortedByY.begin(), pointsSortedByY.end(), compareByY);
vector<Point> bottomGroup(pointsSortedByY.begin(), pointsSortedByY.begin() + n / 2); vector<Point> topGroup(pointsSortedByY.begin() + n / 2, pointsSortedByY.end());
sort(bottomGroup.begin(), bottomGroup.end(), compareByX); sort(topGroup.begin(), topGroup.end(), compareByXDesc);
for (int i = 0; i < n / 2; ++i) { ll dx = abs(bottomGroup[i].x - topGroup[i].x); ll dy = abs(bottomGroup[i].y - topGroup[i].y); sumOption2 += dx + dy; int a = bottomGroup[i].id, b = topGroup[i].id; if (a > b) swap(a, b); pairsOption2.push_back({a, b}); }
if (sumOption1 >= sumOption2) { for (auto& pr : pairsOption1) { cout << pr.first << ' ' << pr.second << '\n'; } } else { for (auto& pr : pairsOption2) { cout << pr.first << ' ' << pr.second << '\n'; } } }
int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int testCases; cin >> testCases; while (testCases--) { processTestCase(); } return 0; }
|