题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4311
思路:只会最简单的暴力枚举,枚举n/2的附近点即可。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 #define MAXN 100010 8 typedef long long ll; 9 #define inf (1ll)<<60 10 int n; 11 struct Node{ 12 int x,y; 13 }node[MAXN]; 14 15 int cmp(const Node &p,const Node &q){ 16 if(p.x!=q.x)return p.x<q.x; 17 return p.y<q.y; 18 } 19 20 ll Solve(int k){ 21 ll ans=0; 22 for(int i=1;i<=n;i++){ 23 ans+=abs(node[k].x-node[i].x); 24 ans+=abs(node[k].y-node[i].y); 25 } 26 return ans; 27 } 28 29 30 int main(){ 31 int _case; 32 // freopen("1.txt","r",stdin); 33 scanf("%d",&_case); 34 while(_case--){ 35 scanf("%d",&n); 36 for(int i=1;i<=n;i++){ 37 scanf("%d%d",&node[i].x,&node[i].y); 38 } 39 sort(node+1,node+1+n,cmp); 40 ll sum=inf; 41 int st=(n/2-333<1?1:n/2-333); 42 int ed=(n/2+333>n?n:n/2+333); 43 for(int i=st;i<=ed;i++){ 44 ll ans=Solve(i); 45 sum=min(ans,sum); 46 } 47 printf("%I64d\n",sum); 48 } 49 return 0; 50 }