hdu4311 Meeting point-1

Meeting point-1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 447    Accepted Submission(s): 151


Problem Description
It has been ten years since TJU-ACM established. And in this year all the retired TJU-ACMers want to get together to celebrate the tenth anniversary. Because the retired TJU-ACMers may live in different places around the world, it may be hard to find out where to celebrate this meeting in order to minimize the sum travel time of all the retired TJU-ACMers. 
There is an infinite integer grid at which N retired TJU-ACMers have their houses on. They decide to unite at a common meeting place, which is someone's house. From any given cell, only 4 adjacent cells are reachable in 1 unit of time.
Eg: (x,y) can be reached from (x-1,y), (x+1,y), (x, y-1), (x, y+1).
Finding a common meeting place which minimizes the sum of the travel time of all the retired TJU-ACMers.
 

 

Input
The first line is an integer T represents there are T test cases. (0<T <=10)
For each test case, the first line is an integer n represents there are n retired TJU-ACMers. (0<n<=100000), the following n lines each contains two integers x, y coordinate of the i-th TJU-ACMer. (-10^9 <= x,y <= 10^9)
 

 

Output
For each test case, output the minimal sum of travel times.
 

 

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
In the first case, the meeting point is (-1,-2); the second is (0,0), the third is (3,1) and the last is (-2,2)
 
Source
 
题意:给定n个坐标,求其中一个坐标到其他坐标之和的最小值。
法一:第一步 按 x 排序
        第二步 sumx[i] 表示从第 1 个到第 i 个坐标的 x 坐标之和
        第三步 按 y 排序,计算sumy[i] 同时记录 i 的位置
        第四部 按 x 排序  依次扫描一遍
View Code
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #define MAXN 100010
 6 
 7 using namespace std;
 8 
 9 struct point{long long x,y,py;}a[MAXN];
10 long long sumx[MAXN],sumy[MAXN];
11 
12 bool cmp1(point A,point B)
13 {
14     if(A.x<B.x) return true;
15     if(A.x==B.x&&A.y<B.y) return true;
16     return false;
17 }
18 
19 bool cmp2(point A,point B)
20 {
21     if(A.y<B.y) return true;
22     if(A.y==B.y&&A.y<B.y) return true;
23     return false;
24 }
25 
26 int main()
27 {
28     long long test,i,j,n;
29     cin>>test;
30     while(test--)
31     {
32         memset(sumx,0,sizeof(sumx));
33         memset(sumy,0,sizeof(sumy));
34         cin>>n;
35         for(i=1;i<=n;i++)
36         {
37             scanf("%I64d%I64d",&a[i].x,&a[i].y);
38         }
39         sort(a+1,a+n+1,cmp1);
40         sumx[1]=a[1].x;
41         for(i=2;i<=n;i++) sumx[i]=sumx[i-1]+a[i].x;
42         sort(a+1,a+n+1,cmp2);
43         sumy[1]=a[1].y;
44         for(i=2;i<=n;i++) sumy[i]=sumy[i-1]+a[i].y;
45         for(i=1;i<=n;i++) a[i].py=i;
46         sort(a+1,a+n+1,cmp1);
47         long long ans=1,t;
48         ans <<= 60;
49         for(i=1;i<=n;i++)
50         {
51             t=i*a[i].x-sumx[i];
52             t+=(sumx[n]-sumx[i]-(n-i)*a[i].x);
53             j=a[i].py;
54             t+=(j*a[i].y-sumy[j]);
55             t+=(sumy[n]-sumy[j]-(n-j)*a[i].y);
56             if(ans>t) ans=t;
57         }
58         cout<<ans<<endl;
59     }
60     return 0;
61 }

 

法二:按 x 排序 枚举 [n/2-250  , n/2+250]  

心得:这个方法也是这篇博客的目的,虽然这方法属于没有正确理论为基础,可是在比赛中却是很妙!! 以后要懂得观察!!!

        于是只有稍微改下1003也就AC了……

View Code
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define MAXN 100010
 6 
 7 using namespace std;
 8 
 9 struct point{long long x,y;}a[MAXN];
10 
11 bool cmp(point A,point B)
12 {
13     if(A.x<B.x) return true;
14     if(A.x==B.x&&A.y<B.y) return true;
15     return false;
16 }
17 
18 long long ABS(long long A)
19 {
20     if(A<0) A=-A;
21     return A;
22 }
23 
24 long long work(int k,int n)
25 {
26     int i;
27     long long ans=0;
28     for(i=0;i<n;i++)
29     {
30         ans+=ABS(a[k].x-a[i].x);
31         ans+=ABS(a[k].y-a[i].y);
32     }
33     return ans;
34 }
35 
36 int main()
37 {
38     int test,n,s,e,i;
39     long long ans,t;
40     scanf("%d",&test);
41     while(test--)
42     {
43         scanf("%d",&n);
44         for(i=0;i<n;i++)
45         {
46             scanf("%I64d%I64d",&a[i].x,&a[i].y);
47         }
48         sort(a,a+n,cmp);
49         s=n/2-250;e=n/2+250;
50         if(s<0) s=0;
51         if(e>n-1) e=n-1;
52         ans=1;ans<<=60;
53         for(i=s;i<=e;i++)
54         {
55             t=work(i,n);
56             if(ans>t) ans=t;
57         }
58         cout<<ans<<endl;
59     }
60     return 0;
61 }

 

 
posted @ 2012-07-26 23:22  mtry  阅读(851)  评论(0编辑  收藏  举报