首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

hdu 4312 Meeting point-二

2013-11-08 
hdu 4312Meeting point-2旋转#includeiostream#includevector#includealgorithm#includecstdio#in

hdu 4312 Meeting point-2

旋转

#include<iostream>#include<vector>#include<algorithm>#include<cstdio>#include<queue>#include<stack>#include<string>#include<map>#include<set>#include<cassert>#include<cstring>#include<iomanip>using namespace std;#ifdef _WIN32typedef __int64 i64;#define out64 "%I64d\n"#define in64 "%I64d"#elsetypedef long long i64;#define out64 "%lld\n"#define in64 "%lld"#endif/************ for topcoder by zz1215 *******************/#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)#define FFF(i,a)        for( int i = 0 ; i < (a) ; i ++)#define FFD(i,a,b)      for( int i = (a) ; i >= (b) ; i --)#define S64(a)          scanf(in64,&a)#define SS(a)           scanf("%d",&a)#define LL(a)           ((a)<<1)#define RR(a)           (((a)<<1)+1)#define pb              push_back#define CL(Q)           while(!Q.empty())Q.pop()#define MM(name,what)   memset(name,what,sizeof(name))#define read            freopen("in.txt","r",stdin)#define write           freopen("out.txt","w",stdout)const int inf = 0x3f3f3f3f;const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;const double oo = 10e9;const double eps = 10e-9;const int maxn = 100011;struct zz{i64 x;i64 y;int id;}zx;i64 n;i64 fx[maxn];i64 fy[maxn];i64 sum[maxn];inline bool cmpx(const zz & a,const zz & b) {return a.x<b.x;} inline bool cmpy(const zz & a,const zz & b) {return a.y<b.y;}vector<zz>v;void findx(){sort(v.begin(),v.end(),cmpx);i64 temp;for(int i=1;i<v.size();i++){fx[i] += fx[i-1];temp = v[i].x-v[i-1].x;temp *= i;fx[i] += temp;sum[v[i].id]+=fx[i];}for(int i=1;i<=n;i++){fx[i]=0;}int cnt = v.size()-1;for(int i=cnt-1;i>=0;i--){fx[i] += fx[i+1];temp = v[i+1].x-v[i].x;temp *= (cnt - i);fx[i] += temp;sum[v[i].id]+=fx[i];}return ;}void findy(){sort(v.begin(),v.end(),cmpy);i64 temp;for(int i=1;i<v.size();i++){fy[i] += fy[i-1];temp = v[i].y-v[i-1].y;temp *= i;fy[i] += temp;sum[v[i].id]+=fy[i];}for(int i=1;i<=n;i++){fy[i]=0;}int cnt = v.size()-1;for(int i=cnt-1;i>=0;i--){fy[i] += fy[i+1];temp = v[i+1].y-v[i].y;temp *= (cnt - i);fy[i] += temp;sum[v[i].id]+=fy[i];}return ;}i64 start(){findx();findy();i64 ans = inf64;for(int i=1;i<=n;i++){if(sum[i]<ans){ans=sum[i];}}return ans;}int main(){int T;cin>>T;while(T--){cin>>n;v.clear();for(int i=0;i<=n;i++){fx[i]=0;fy[i]=0;sum[i]=0;}int x,y;for(int i=1;i<=n;i++){cin>>x>>y;zx.x = x+y;zx.y = y-x;zx.id = i;v.pb(zx);}cout<<start()/2<<endl;}return 0;}


热点排行