标签:
Description
Input
Output
Sample Input
4 6 -4 -1 -1 -2 2 -4 0 2 0 3 5 -2 6 0 0 2 0 -5 -2 2 -2 -1 2 4 0 5 -5 1 -1 3 3 1 3 -1 1 -1 10 -1 -1 -3 2 -4 4 5 2 5 -4 3 -1 4 3 -1 -2 3 4 -2 2
Sample Output
26 20 20 56
Hint
1 /****************************** 2 code by drizzle 3 blog: www.cnblogs.com/hsd-/ 4 ^ ^ ^ ^ 5 O O 6 ******************************/ 7 //#include<bits/stdc++.h> 8 #include<iostream> 9 #include<cstring> 10 #include<cstdio> 11 #include<map> 12 #include<algorithm> 13 #include<cmath> 14 #define ll long long 15 #define PI acos(-1.0) 16 #define mod 1000000007 17 int t; 18 using namespace std; 19 struct node 20 { 21 ll x,y,pos; 22 } N[100005]; 23 ll sumx[100005]; 24 ll sumy[100005]; 25 ll ans1[100005]; 26 ll ans2[100005]; 27 int exm; 28 bool cmp1(struct node aa,struct node bb) 29 { 30 return aa.x<bb.x; 31 } 32 bool cmp2(struct node aa,struct node bb) 33 { 34 return aa.y<bb.y; 35 } 36 int main() 37 { 38 while(~scanf("%d",&t)) 39 { 40 for(int i=1; i<=t; i++) 41 { 42 scanf("%d",&exm); 43 memset(N,0,sizeof(N)); 44 memset(ans1,0,sizeof(ans1)); 45 memset(ans2,0,sizeof(ans2)); 46 for(int j=1; j<=exm; j++) 47 { 48 scanf("%I64d %I64d",&N[j].x,&N[j].y); 49 N[j].pos=j; 50 } 51 sort(N+1,N+1+exm,cmp1); 52 sumx[1]=0; 53 for(int j=2; j<=exm; j++) 54 { 55 sumx[j]=sumx[j-1]+N[j].x-N[j-1].x; 56 ans1[N[1].pos]+=sumx[j]; 57 } 58 for(int j=2; j<=exm; j++) 59 { 60 61 ans1[N[j].pos]=ans1[N[j-1].pos]-(exm-j)*(sumx[j]-sumx[j-1])+(j-2)*(sumx[j]-sumx[j-1]); 62 } 63 sort(N+1,N+1+exm,cmp2); 64 sumy[1]=0; 65 for(int j=2; j<=exm; j++) 66 { 67 sumy[j]=sumy[j-1]+N[j].y-N[j-1].y; 68 ans2[N[1].pos]+=sumy[j]; 69 } 70 for(int j=2; j<=exm; j++) 71 { 72 ans2[N[j].pos]=ans2[N[j-1].pos]-(exm-j)*(sumy[j]-sumy[j-1])+(j-2)*(sumy[j]-sumy[j-1]); 73 } 74 ll maxn=ans1[1]+ans2[1]; 75 for(int j=1; j<=exm; j++) 76 { 77 if(ans1[j]+ans2[j]<maxn) 78 { 79 maxn=ans1[j]+ans2[j]; 80 } 81 } 82 printf("%I64d\n",maxn); 83 } 84 } 85 return 0; 86 }
标签:
原文地址:http://www.cnblogs.com/hsd-/p/5726546.html