51nod1108(曼哈顿距离)

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1108

 

题意:中文题诶~

 

思路:曼哈顿距离,题目没要求所求点要在给出的点中,所以可以分别取x, y, z的中位数为所求点的坐标,显然该点到其他点的曼哈顿距离和是最小的;

 

代码:

 1 #include <iostream>
 2 #include <algorithm>
 3 #define ll long long
 4 using namespace std;
 5 
 6 const int MAXN=1e4+10;
 7 ll x[MAXN], y[MAXN], z[MAXN];
 8 
 9 int main(void){
10     int n;
11     cin >> n;
12     for(int i=0; i<n; i++){
13         cin >> x[i] >> y[i] >> z[i];
14     }
15     sort(x, x+n);
16     sort(y, y+n);
17     sort(z, z+n);
18     ll ans=0;
19     int pos=n>>1;
20     for(int i=0; i<n; i++){
21         ans+=abs(x[pos]-x[i])+abs(y[pos]-y[i])+abs(z[pos]-z[i]);
22     }
23     cout << ans << endl;
24     return 0;
25 }
View Code

 

posted @ 2017-05-10 21:20  geloutingyu  阅读(171)  评论(0编辑  收藏  举报