天天看點

水題:meeting point-1Meeting point-1

Meeting point-1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 987    Accepted Submission(s): 306

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
   
  
       

平面上兩點間的 Manhattan 距離為 |x1-x2| + |y1-y2|

X 方向的距離與 Y 方向上的距離可以分開來處理。假設我們以 (xi,yi) 作為開會的地點,那麼其餘的點到該開會地點所需的時間為 X 方向上到 xi 所需要的時間加上 Y 方向上到 yi 所需要的時間。

對資料預處理後可以快速地求出x坐标小于xi的點的個數rankx, 并且這些 x 坐标值之和 sumx,那麼這些點 X 方向上對結果的貢獻為 rankx * xi - sumx;同理可以處理出 x 坐标大于 xi 的點在 X 方向上對結果的貢獻值。同理可求得其餘點在 Y 方向上到達 yi 所需要的總時間。

注意INF的值要取足夠大,不然不停地wa,最後發現取1LL<<63-1就行了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;

const long long INF =  1LL<<63-1;

struct acmers{
    long long x;
    long long y;
    long long sum;
}a[100010];

bool cmp1(acmers a, acmers b){
    return a.x < b.x;
}

bool cmp2(acmers a, acmers b){
    return a.y < b.y;
}

int main(){
    long long t;
    long long i;
    long long n;
    long long rankx, ranky, rankxr, rankyd;
    long long sumxl, sumxr;
    long long sumyu, sumyd;
    scanf("%I64d", &t);
    while(t --){
        scanf("%I64d", &n);
        sumxr = 0;
        sumyd = 0;
        for(i = 0; i < n; i ++){
            scanf("%I64d %I64d", &a[i].x, &a[i].y);
            a[i].sum = 0;
            sumxr += a[i].x;
            sumyd += a[i].y;
        }
        //cout << "sumxr" << sumxr << endl;
        //cout << "sumyd" << sumyd << endl;
        sort(a, a+n, cmp1);
        sumxl = 0;
        rankx = 0;
        rankxr = n;
        for(rankx = 0; rankx < n; rankx ++){
            sumxr -= a[rankx].x;
            rankxr--;
            a[rankx].sum += rankx * a[rankx].x - sumxl + sumxr - rankxr * a[rankx].x;
            sumxl += a[rankx].x;
        }
        sort(a, a + n, cmp2);
        sumyu = 0;
        ranky = 0;
        rankyd = n;
        for(ranky = 0; ranky < n; ranky ++){
            sumyd -= a[ranky].y;
            rankyd--;
            a[ranky].sum += ranky * a[ranky].y - sumyu + sumyd - rankyd * a[ranky].y;
            sumyu += a[ranky].y;
        }
        long long mm = INF;
        for(i = 0; i < n; i ++){
            if (a[i].sum < mm)
                mm = a[i].sum;
        }
        cout << mm << endl;
    }
    return 0;
}
           

繼續閱讀