hdu4312 Meeting point-2

Meeting point-2

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


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. 

It's an opportunity to show yourself in front of your predecessors!

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, all 8 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), (x-1,y+1), (x-1,y-1), (x+1,y+1), (x+1,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
20 15 14 38
Hint
In the first case, the meeting point is (0,2); the second is (0,0), the third is (1,-1) and the last is (-1,-1)
 

 

Source
 
分析:有1002的经验解决这道题,那就小case了;
心得:这篇博客的目的在于比较 调用函数 返回 long long 和 int 在时间上有很大的不同!!!
 
返回 long long 
Accepted 4312 890MS 1056K 1108 B
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{int 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     return A>0?A:-A;
21 }
22 
23 long long MAX(long long A,long long B)
24 {
25     return A>B?A:B;
26 }
27 
28 long long work(int k,int n)
29 {
30     int i;
31     long long ans=0;
32     for(i=0;i<n;i++)
33     {
34         ans+=MAX(ABS(a[k].x-a[i].x),ABS(a[k].y-a[i].y));
35     }
36     return ans;
37 }
38 
39 int main()
40 {
41     int test,n,s,e,i;
42     long long ans,t;
43     scanf("%d",&test);
44     while(test--)
45     {
46         scanf("%d",&n);
47         for(i=0;i<n;i++)
48         {
49             scanf("%d%d",&a[i].x,&a[i].y);
50         }
51         sort(a,a+n,cmp);
52         s=n/2-215;e=n/2+215;
53         if(s<0) s=0;
54         if(e>n-1) e=n-1;
55         ans=1;ans<<=60;
56         for(i=s;i<=e;i++)
57         {
58             t=work(i,n);
59             if(ans>t) ans=t;
60         }
61         printf("%I64d\n",ans);
62     }
63     return 0;
64 }

 

返回 int

Accepted 4312 546MS 1056K 1078 B
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{int 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 int ABS(int A)
19 {
20     return A>0?A:-A;
21 }
22 
23 int MAX(int A,int B)
24 {
25     return A>B?A:B;
26 }
27 
28 long long work(int k,int n)
29 {
30     int i;
31     long long ans=0;
32     for(i=0;i<n;i++)
33     {
34         ans+=MAX(ABS(a[k].x-a[i].x),ABS(a[k].y-a[i].y));
35     }
36     return ans;
37 }
38 
39 int main()
40 {
41     int test,n,s,e,i;
42     long long ans,t;
43     scanf("%d",&test);
44     while(test--)
45     {
46         scanf("%d",&n);
47         for(i=0;i<n;i++)
48         {
49             scanf("%d%d",&a[i].x,&a[i].y);
50         }
51         sort(a,a+n,cmp);
52         s=n/2-215;e=n/2+215;
53         if(s<0) s=0;
54         if(e>n-1) e=n-1;
55         ans=1;ans<<=60;
56         for(i=s;i<=e;i++)
57         {
58             t=work(i,n);
59             if(ans>t) ans=t;
60         }
61         printf("%I64d\n",ans);
62     }
63     return 0;
64 }

 

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