Meeting point-1
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)
【思路】:首先我们要先知道,什么是曼哈顿距离
曼哈顿距离为 abs(xi - xj) + abs(yi - yj)
那么我们可以得知就是考虑x的距离,跟y的距离
根据数据范围我们可以得知,复杂度是O(n ^ 2)的是不可能通过该题的
如果转换成一维的,我们容易想到前缀和,其实可以使用两个一维的前缀和来维护
我们可以假设以第一个点作为中心点,可以处理出对应的前缀和,
那么我们要把当前的点的x值对应在x有序数组中的位置,y值对应在y有序数组中的位置
这样的话,选一个点,会把所有的点分成两个部分,x < 当前选定的点的x, x > 当前选定的点的x
考虑小的情况,就是对应的(x - x的最小值) * (x在有序数组中的序号的值 + 1) - 前缀和x
考虑大的情况,就是对应的 前缀和的最大值 - 当前点的前缀和 - (点的个数减去(当前的点的序号 + 1)) * (x - x的最小值)
然后把两者加起来,y的考虑情况是一样的
这样的话就可以在O(n)的时间里得到最大值,预处理的复制度为O(n * log n + n)
附上代码
#include <bits/stdc++.h>
#define mem(x) memset(x, 0, sizeof(x))
using namespace std;
typedef long long LL;
const int MAXN = 100005;
struct node
{
LL num, x, y, sumx_num, sumy_num;
} arr[MAXN];
LL sumx[MAXN], sumy[MAXN];
int n;
bool cmp(const node &a, const node &b)
{
return a.x < b.x;
}
bool cmp1(const node &a, const node &b)
{
return a.y < b.y;
}
void Init()
{
mem(sumx);
mem(sumy);
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
Init();
LL xmins, ymins;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%lld%lld", &arr[i].x, &arr[i].y);
arr[i].num = i;
}
sort(arr, arr + n, cmp);
arr[0].sumx_num = 0;
xmins = arr[0].x;
for (int i = 1; i < n; i++)
{
sumx[i] = sumx[i - 1] + arr[i].x - arr[0].x;
arr[i].sumx_num = i;
}
sort(arr, arr + n, cmp1);
arr[0].sumy_num = 0;
ymins = arr[0].y;
for (int i = 1; i < n; i++)
{
sumy[i] = sumy[i - 1] + arr[i].y - arr[0].y;
arr[i].sumy_num = i;
}
LL sum = __LONG_LONG_MAX__;
for (int i = 0; i < n; i++)
{
LL a = sumx[n - 1] - sumx[arr[i].sumx_num] - (n - 1 - arr[i].sumx_num) * (arr[i].x - xmins) + ((arr[i].sumx_num + 1) * (arr[i].x - xmins) - sumx[arr[i].sumx_num]);
LL b = sumy[n - 1] - sumy[arr[i].sumy_num] - (n - 1 - arr[i].sumy_num) * (arr[i].y - ymins) + ((arr[i].sumy_num + 1) * (arr[i].y - ymins) - sumy[arr[i].sumy_num]);
sum = min(sum, a + b);
}
printf("%lld
", sum);
}
return 0;
}