2 3 1 1 2 1 1 2 5 2 9 5 20 11 9 1 1 1 20
2 44
我也是醉了,就是查找X坐标和Y坐标的中位数,然后进行计算距离即可,但是没想到,用户的居住地也可以设置成邮局。。。。。
#include<cstdio> #include<algorithm> #include<iostream> #include<cmath> using namespace std; typedef struct { int x; int y; }Node; int N; bool Cmx(const Node h1,const Node h2) { return h1.x < h2.x; } bool Cmy(const Node h1,const Node h2) { return h1.y < h2.y; } Node node[25]; int main() { //freopen("in.txt", "r", stdin); int n; scanf("%d", &N); while(N--) { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d", &node[i].x, &node[i].y); } sort(node, node+n, Cmx); int x = node[n/2].x; sort(node, node+n, Cmy); int y = node[n/2].y; int sum = 0; for (int i = 0; i < n; i++) { sum += fabs(node[i].x - x); sum += fabs(node[i].y - y); } printf("%d\n",sum); } return 0; }