http://acm.hdu.edu.cn/showproblem.php?pid=4312
和 4311 的区别在于一个转换 自己还真想不出来
max(|x|,|y|) 等于 max(|x-y|,|x+y|)/2
原来的两点 (x1,y1) (x2,y2) 转换为 (x1-y1,x1+y1) (x2-y2,x2+y2)
最后记得除以2
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> #include<map> #include<queue> #include<cmath> #define LL long long using namespace std; const int N=100005; struct node { int I; LL x,y; }mem[N]; LL l[N]; LL sumx[N]; LL sumy[N]; bool cmp1(node a,node b) { return a.x<b.x; } bool cmp2(node a,node b) { return a.y<b.y; } int main() { int T; scanf("%d",&T); while(T--) { int n; scanf("%d",&n); for(int i=1;i<=n;++i) { LL x,y; cin>>x>>y; mem[i].x=x-y; mem[i].y=x+y; mem[i].I=i; } sort(mem+1,mem+n+1,cmp1); l[0]=0; for(int i=1;i<=n;++i) { l[i]=l[i-1]+mem[i].x; } for(int i=1;i<=n;++i) { sumx[mem[i].I]=((i-1)*mem[i].x-l[i-1])+((l[n]-l[i])-(n-i)*mem[i].x); } sort(mem+1,mem+n+1,cmp2); l[0]=0; for(int i=1;i<=n;++i) { l[i]=l[i-1]+mem[i].y; } for(int i=1;i<=n;++i) { sumy[mem[i].I]=((i-1)*mem[i].y-l[i-1])+((l[n]-l[i])-(n-i)*mem[i].y); } LL ans=sumx[1]+sumy[1]; for(int i=2;i<=n;++i) { if(sumx[i]+sumy[i]<ans) ans=sumx[i]+sumy[i]; } cout<<(ans/2)<<endl; } return 0; }