1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #define M 100008 5 using namespace std; 6 struct data 7 { 8 int x,y,num; 9 }a[M]; 10 long long n,suma[M],suma1[M],ans,sumb[M],sumb1[M]; 11 bool cmpx(data a1,data a2) 12 { 13 return a1.x<a2.x; 14 } 15 bool cmpy(data a1,data a2) 16 { 17 return a1.y<a2.y; 18 } 19 int main() 20 { 21 scanf("%d",&n); 22 for(int i=1;i<=n;i++) 23 { 24 int a1,a2; 25 scanf("%d%d",&a1,&a2); 26 a[i].x=a1+a2; 27 a[i].y=a1-a2; 28 } 29 sort(a+1,a+n+1,cmpx); 30 ans=100000000000000000LL; 31 for(int i=1;i<=n;i++) 32 { 33 suma[i]=suma[i-1]+a[i].x; 34 a[i].num=i; 35 } 36 for(int i=n;i;i--) 37 suma1[i]=suma1[i+1]+a[i].x; 38 sort(a+1,a+n+1,cmpy); 39 for(int i=1;i<=n;i++) 40 sumb[i]=sumb[i-1]+a[i].y; 41 for(int i=n;i;i--) 42 sumb1[i]=sumb1[i+1]+a[i].y; 43 for(int i=1;i<=n;i++) 44 { 45 long long ss=0,x=suma[a[i].num]-suma[a[i].num-1],y=sumb[i]-sumb[i-1]; 46 ss=x*(a[i].num-1)-suma[a[i].num-1]+suma1[a[i].num+1]-x*(n-a[i].num)+y*(i-1)-sumb[i-1]+sumb1[i+1]-y*(n-i); 47 ans=min(ss,ans); 48 } 49 printf("%lld\n",ans/2); 50 return 0; 51 }
这个题让我学会了切比雪夫距离。