Meeting point-1

来源:互联网 发布:linux mount 重新挂载 编辑:程序博客网 时间:2024/04/25 15:44

B - Meeting point-1
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 4311

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

46-4 -1-1 -22 -40 20 35 -260 02 0-5 -22 -2-1 24 05-5 1-1 33 13 -11 -110-1 -1-3 2-4 45 25 -43 -14 3-1 -23 4-2 2
 

Sample Output

26202056

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) 


很久没写题解了,昨天做了一次小练习,感觉颓废了呀。“2012 Multi-University Training Contest 2”其中B题昨天始终没有搞定,就差一点点呀。

B题的思路被我的一个学弟一下想出来了,开始对此算法报有怀疑的态度,最后自己想了几种方法没能实现。还是采取学弟的方法。最后总结发现我们错在__int64上面。

题意:

就是给你n个点,要找出一个点求出其他点到这点的哈夫曼距离最短,求出最短距离。

具体代码:


#include <stdio.h>#include <string.h>#include <math.h>#include <iostream>#include<algorithm>#define M 100005using namespace std;#define LL __int64struct point {    LL x;    LL y;    int id;} p[M];bool cmpx(point e1,point e2) {    return e1.x < e2.x;}bool cmpy(point e1,point e2) {    return e1.y < e2.y;}LL temp[M],sumx[M],sumy[M];int main() {    int cse;    cin>>cse;    while(cse--) {        int n;        scanf("%d",&n);        for(int i=1; i<=n; i++) {            scanf("%I64d%I64d",&p[i].x,&p[i].y);            p[i].id = i;        }        memset(sumx,0,sizeof(sumx));        memset(sumy,0,sizeof(sumy));        sort(p+1, p+n+1, cmpx);        temp[0] = 0;        for(int i=1; i<=n; i++)            temp[i] = temp[i-1] + p[i].x;        for(int i=1; i<=n; i++)            sumx[ p[i].id ] = temp[n]  - 2*temp[i-1] + (2*i-n-2)*p[i].x;        sort(p+1, p+n+1, cmpy);        temp[0] = 0;        for(int i=1; i<=n; i++)            temp[i] = temp[i-1] + p[i].y;        for(int i=1; i<=n; i++)            sumy[ p[i].id ] = temp[n] -  2*temp[i-1] + (2*i-n-2)*p[i].y;        LL ans = sumx[1] + sumy[1];        for(int i=2; i<=n; i++) {            if(ans > sumx[i]+sumy[i])                ans = (sumx[i]+sumy[i]);        }        printf("%I64d\n",ans);    }    return 0;}



0 0
原创粉丝点击