MUTC 2 C - Meeting point-2 切比雪夫距离orz

Meeting point-2

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


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)
 

Author
TJU
 

Source
2012 Multi-University Training Contest 2
 

Recommend
zhuyuanchen520
 

----------------------

摘自维基

----------------------

出租车几何曼哈顿距离方格线距离是由十九世纪赫尔曼·闵可夫斯基所创词汇,是种使用在欧几里得几何度量空间几何学用语,用以标明两个点上在标准座标系上的绝对轴距总和。

我们可以定义曼哈顿距离的正式意义为L1-距离城市区块距离,也就是在欧几里得空间的固定直角坐标系上两点所形成的线段对轴产生的投影的距离总和。

例如在平面上,座标(x1y1)的点P1与座标(x2y2)的点P2的曼哈顿距离为:

要注意的是,曼哈顿距离依赖座标系统的转度,而非系统在座标轴上的平移或映射。

曼哈顿距离的命名原因是从规划为方型建筑区块的城市(如曼哈顿)间,最短的行车路径而来(忽略曼哈顿的单向车道以及只存在于3、14大道的斜向车道)。任何往东三区块、往北六区块的的路径一定最少要走九区块,没有其他捷径。

出租车几何学满足除了SAS全等定理之外的希伯特定理,SAS全等指任两个三角型两个边与它们的夹角相等,则这两个三角型必全等。

在出租车几何学中,一个圆是由从圆心向各个固定曼哈顿距离标示出来的点围成的区域。因此这种圆其实就是旋转了45度的正方形。如果有一群圆,任两圆皆相交,则整群圆必在某点相交;因此曼哈顿距离会形成一个超凸度量空间(Injective metric space)。对一个半径为r 的来说,这个正方形的圆每边长√2r。此'"圆"的半径r对切比雪夫距离(L空间)的二维平面来说,也是一个对座标轴来说边长为2r的正方形,因此二维切比雪夫距离可视为等同于旋转且放大过的二维曼哈顿距离。然而这种介于L1与L的相等关系并不能延伸到更高的维度。

----------------------------

数学上,切比雪夫距离Chebyshev distance)或是L度量是向量空间中的一种度量,二个点之间的距离定义为其各座标数值差的最大值。以(x1,y1)和(x2,y2)二点为例,其切比雪夫距离为max(|x2-x1|,|y2-y1|)。切比雪夫距离得名自俄罗斯数学家切比雪夫。

若将国际象棋棋盘放在二维直角座标系中,格子的边长定义为1,座标的x轴及y轴和棋盘方格平行,原点恰落在某一格的中心点,则王从一个位置走到其他位置需要的步数恰为二个位置的切比雪夫距离,因此切比雪夫距离也称为棋盘距离。任何一个不在棋盘边缘的位置,和周围八个位置的切比雪夫距离都是1。

-----------------------------

特殊结论 

对于原坐标系中两点间的切比雪夫 距离,是将坐标轴顺时针旋转45度并将所有点的坐标值放大sqrt(2)倍所得到的新坐标系中的曼哈顿距离的二分之一。

坐标值顺时针旋转45度*sqrt(2)

a[i].x=x[i]-y[i];
a[i].y=x[i]+y[i];

----------------------

方法同B - Meeting point-1

----------------------

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

using namespace std;

const int maxn=111111;
const long long INF=1e18;

struct POINT{
    int x;
    int y;
}a[maxn];
int x[maxn];
int y[maxn];
long long sumx[maxn];
long long sumy[maxn];
int n;
long long ans;
long long tmp;

int fin(int q[],int key)
{
    int l=1,r=n,m;
    while (l<=r)
    {
        m=(l+r)/2;
        if (q[m]==key) return m;
        if (q[m]<key) l=m+1;
        else r=m-1;
    }
    return -1;
}

int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        sumx[0]=sumy[0]=0;
        scanf("%d",&n);
        for (int i=1;i<=n;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            //-----------------
            a[i].x=x[i]-y[i];
            a[i].y=x[i]+y[i];
            x[i]=a[i].x;
            y[i]=a[i].y;
            //-----------------
        }
        sort(x+1,x+n+1);
        sort(y+1,y+n+1);
        for (int i=1;i<=n;i++)
        {
            sumx[i]=sumx[i-1]+x[i];
            sumy[i]=sumy[i-1]+y[i];
        }
        ans=INF;
        for (int i=1;i<=n;i++)
        {
            int idx=fin(x,a[i].x);
            int idy=fin(y,a[i].y);
            tmp=0;
            tmp+=(1LL*x[idx]*(idx-1)-sumx[idx-1]) + (sumx[n]-sumx[idx]-1LL*x[idx]*(n-idx));
            tmp+=(1LL*y[idy]*(idy-1)-sumy[idy-1]) + (sumy[n]-sumy[idy]-1LL*y[idy]*(n-idy));
            if (tmp<ans) ans=tmp;
        }
        printf("%I64d\n",ans/2);
    }
    return 0;
}








你可能感兴趣的:(题解)