HDU 4311 - Meeting point-1(技巧)

Meeting point-1

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 3928    Accepted Submission(s): 1322

ProblemDescription

It has been ten years since TJU-ACMestablished. And in this year all the retired TJU-ACMers want to get togetherto celebrate the tenth anniversary. Because the retired TJU-ACMers may live indifferent places around the world, it may be hard to find out where tocelebrate this meeting in order to minimize the sum travel time of all theretired TJU-ACMers.
There is an infinite integer grid at which N retired TJU-ACMers have theirhouses on. They decide to unite at a common meeting place, which is someone'shouse. From any given cell, only 4 adjacent cells are reachable in 1 unit oftime.
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 ofall the retired TJU-ACMers.

 

 

Input

The first line is an integer T representsthere are T test cases. (0<T <=10)
For each test case, the first line is an integer n represents there are nretired TJU-ACMers. (0<n<=100000), the following n lines each containstwo integers x, y coordinate of the i-th TJU-ACMer. (-10^9 <= x,y <=10^9)

 

 

Output

For each test case, output the minimal sumof travel times.

 

 

SampleInput

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

 

 

SampleOutput

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 thelast is (-2,2)

 

【题意】

给定n个点,找出一点使得该点到其余各点的曼哈顿距离最小,输出最小值。

 

【思路】

   分别对横纵坐标排序,比如说横坐标x,排好序后如果点i为所求,则前i-1个点的x坐标小于x[i],i+1到n这n-i个点的x坐标大于x[i],则对应的结果为(i-1)*x[i]-s[i-1]+s[n]-s[i]-(n-i)*x[i],s[i]为前i个点的坐标值和。对y轴也做同样处理,枚举取出最大值即可。

 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;

const ll inf = 1e18 + 500;
const int maxn = 100050;

int n;
ll s[maxn];

struct node {
	ll x, y;
	ll ansx, ansy;
}a[maxn];

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

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

int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for (int i = 1; i <= n; ++i) {
			scanf("%lld%lld", &a[i].x, &a[i].y);
		}
		memset(s, 0, sizeof(s));
		sort(a + 1, a + 1 + n, cmp1);
		for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + a[i].x;
		for (int i = 1; i <= n; ++i) {
			a[i].ansx = (i - 1)*a[i].x - s[i - 1] + s[n] - s[i] - (n - i)*a[i].x;
		}
		
		memset(s, 0, sizeof(s));
		sort(a + 1, a + 1 + n, cmp2);
		for (int i = 1; i <= n; ++i) s[i] = s[i - 1] + a[i].y;
		for (int i = 1; i <= n; ++i) {
			a[i].ansy = (i - 1)*a[i].y - s[i - 1] + s[n] - s[i] - (n - i)*a[i].y;
		}
		
		ll ans = inf;
		for (int i = 1; i <= n; ++i) {
			ans = min(ans, a[i].ansx + a[i].ansy);
		}
		printf("%lld\n", ans);
	}
	return 0;
}


posted @ 2018-01-06 23:40  不想吃WA的咸鱼  阅读(216)  评论(0编辑  收藏  举报