显示原始代码
#include <iostream>
#include <algorithm>
#include <queue>
typedef long long ll;
using namespace std;
const int MAX = 1e5 + 7;
struct num {
ll j, x, y, dis;
} number[MAX];
bool cmpx(num x, num y) { return x.x < y.x; }
bool cmpy(num x, num y) { return x.y < y.y; }
bool cmpd(num x, num y) { return x.dis > y.dis; }
ll x, y, xx[MAX], yy[MAX], n;
queue<num> xp, xn, a, b, c, d;
int main() {
cin >> n;
for (ll i = 0; i < n; i++) {
cin >> x >> y;
number[i].x = x + y;
number[i].y = x - y;
number[i].j = i + 1;
xx[i] = x + y;
yy[i] = x - y;
}
if (n == 1)
cout << 0;
else {
cout << n / 2 << endl;
sort(xx, xx + n);
sort(yy, yy + n);
if (n % 2) {
ll x_0 = xx[n / 2], y_0 = yy[n / 2];
for (ll i = 0; i < n; i++) number[i].dis = abs(x_0 - number[i].x) + abs(y_0 - number[i].y);
sort(number, number + n, cmpd);
n--;
}
sort(number, number + n, cmpy);
ll y_0 = number[n / 2 - 1].y + number[n / 2].y;
sort(number, number + n, cmpx);
for (ll i = 0; i < n; i++) {
if (i >= n / 2) {
if (2 * number[i].y > y_0)
a.push(number[i]);
else if (2 * number[i].y < y_0)
b.push(number[i]);
else
xp.push(number[i]);
} else {
if (2 * number[i].y > y_0)
c.push(number[i]);
else if (2 * number[i].y < y_0)
d.push(number[i]);
else
xn.push(number[i]);
}
}
unsigned long long si;
si = min(a.size(), d.size());
for (ll i = 0; i < si; i++) {
cout << a.front().j << " " << d.front().j << endl;
a.pop();
d.pop();
}
si = min(b.size(), c.size());
for (ll i = 0; i < si; i++) {
cout << b.front().j << " " << c.front().j << endl;
b.pop();
c.pop();
}
while (a.size()) {
cout << a.front().j << " " << xn.front().j << endl;
a.pop();
xn.pop();
}
while (b.size()) {
cout << b.front().j << " " << xn.front().j << endl;
b.pop();
xn.pop();
}
while (c.size()) {
cout << c.front().j << " " << xp.front().j << endl;
c.pop();
xp.pop();
}
while (d.size()) {
cout << d.front().j << " " << xp.front().j << endl;
d.pop();
xp.pop();
}
while (xn.size()) {
cout << xp.front().j << " " << xn.front().j << endl;
xp.pop();
xn.pop();
}
}
return 0;
}