显示原始代码
#include <algorithm>
#include <cstdio>
#define N 100009
#define RI register int
#define Abs(x) (x > 0 ? x : -x)
using namespace std;
bool ind[N];
int n, st[4], f[2][N], sf, ans;
struct DT {
int x, y, i;
} a[4][N];
inline int read();
inline int get(DT x, DT y) { return Abs(x.x - y.x) + Abs(x.y - y.y); }
bool cmp0(DT x, DT y) { return x.x < y.x; }
bool cmp1(DT x, DT y) { return x.x > y.x; }
bool cmp2(DT x, DT y) { return x.y < y.y; }
bool cmp3(DT x, DT y) { return x.y > y.y; }
int main() {
n = read();
st[0] = st[1] = st[2] = st[3] = 1;
for (RI i = 1, xi, yi; i <= n; ++i) {
xi = read(), yi = read();
for (RI j = 0; j < 4; ++j) a[j][i].i = i, a[j][i].x = xi + yi, a[j][i].y = xi - yi;
}
sort(a[0] + 1, a[0] + 1 + n, cmp0);
sort(a[1] + 1, a[1] + 1 + n, cmp1);
sort(a[2] + 1, a[2] + 1 + n, cmp2);
sort(a[3] + 1, a[3] + 1 + n, cmp3);
for (RI i = 2; i <= n; i += 2) {
for (RI j = 0; j < 4; ++j)
while (st[j] && ind[a[j][st[j]].i]) ++st[j];
int now1 = get(a[0][st[0]], a[1][st[1]]);
int now2 = get(a[2][st[2]], a[3][st[3]]);
if (now1 > now2) {
ans += now1;
++sf;
ind[a[0][st[0]].i] = true;
ind[a[1][st[1]].i] = true;
f[0][sf] = a[0][st[0]].i;
f[1][sf] = a[1][st[1]].i;
} else {
ans += now2;
++sf;
ind[a[2][st[2]].i] = true;
ind[a[3][st[3]].i] = true;
f[0][sf] = a[2][st[2]].i;
f[1][sf] = a[3][st[3]].i;
}
}
printf("%d\n", sf);
for (RI i = 1; i <= sf; ++i) printf("%d %d\n", f[0][i], f[1][i]);
return 0;
}
inline int read() {
int x = 0;
char c = getchar();
while (c > '9' || c < '0') c = getchar();
while (c >= '0' && c <= '9') {
x *= 10;
x += c - '0';
c = getchar();
}
return x;
}