CF2122C Manhattan Pairs
转载自:题解:CF2122C Manhattan Pairs - 洛谷专栏
能打到这种场这辈子有了。
不妨想想曼哈顿距离几何意义,\(|x_i−x_j|+|y_i−y_j|\) 即两点所围矩阵周长一半,故在其中任选选一点 \(z(x,y)\),\(x,z\) 与 \(y,z\) 曼哈顿距离和等于 \(x,y\) 曼哈顿距离。
不妨想想一三象限,二四象限点数相等怎么做。显然此时一一连接即最大化,这样所有的相对坐标的贡献均为自己的绝对值,也就取得答案上界。
不保证的情况下分别按 \(x,y\) 离散化出一个原点使得恰有一半点在原点左侧与上侧,做完了。
#include<bits/stdc++.h>
//#include<cassert>
#define F(i,l,r) for(int i(l); i <= (r); ++ i)
#define G(i,r,l) for(int i(r); i >= (l); -- i)
using namespace std;
using ll = long long;
const int N = 3e5;
struct node{
int x, y, id;
}c[N];
bool cmp1(const node &p, const node &q){
return p.x < q.x;
}
bool cmp2(const node &p, const node &q){
return p.y < q.y;
}
int n;
void Main(){
cin >> n;
F(i, 1, n){
cin >> c[i].x >> c[i].y;
c[i].id = i;
}
sort(c + 1, c + n + 1, cmp1);
sort(c + 1, c + n / 2 + 1, cmp2);
sort(c + n / 2 + 1, c + n + 1, cmp2);
F(i, 1, n / 2){
cout << c[i].id << ' ' << c[n - i + 1].id << '\n';
}
return ;
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T;
cin >> T;
while(T --) Main();
return fflush(0), 0;
}

浙公网安备 33010602011771号