显示原始代码
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 100005;
const double pi = acos(-1);
int n;
struct node {
double x, y, the;
int id;
} p[N];
bool cmp(node a, node b) { return a.the < b.the; }
bool cmpx(node a, node b) { return a.x < b.x; }
bool cmpy(node a, node b) { return a.y < b.y; }
int main() {
scanf("%d", &n);
double sx = 0, sy = 0;
for (int i = 1, x, y; i <= n; i++) {
scanf("%d%d", &x, &y);
p[i].x = (x + y) * 0.5;
p[i].y = (x - y) * 0.5;
p[i].id = i;
}
sort(p + 1, p + n + 1, cmpx);
sx = p[n / 2 + 1].x;
sort(p + 1, p + n + 1, cmpy);
sy = p[n / 2 + 1].y;
for (int i = 1; i <= n; i++) {
if (p[i].y > sy) {
p[i].the =
acos((p[i].x - sx) / sqrt((p[i].x - sx) * (p[i].x - sx) + (p[i].y - sy) * (p[i].y - sy)));
} else {
p[i].the = pi + asin((p[i].x - sx) /
sqrt((p[i].x - sx) * (p[i].x - sx) + (p[i].y - sy) * (p[i].y - sy)));
}
}
sort(p + 1, p + n + 1, cmp);
double mi = 1e18, S = 0;
int id = 0;
for (int i = 1; i <= n; i++) {
double d = abs(p[i].x - sx) + abs(p[i].y - sy);
if (d < mi)
id = i, mi = d;
}
int i = 1, j = n / 2 + 1;
if (n & 1 and id <= i)
i++;
if (n & 1 and id <= j)
j++;
printf("%d\n", n / 2);
for (int k = 1; k <= n / 2; k++) {
printf("%d %d\n", p[i].id, p[j].id);
i++;
j++;
if (n & 1 and i == id)
i++;
if (n & 1 and j == id)
j++;
}
return 0;
}