[TJOI2013]松鼠聚会

坐标变换

看图

B - G 是原来的一个单位

B - J 也是。

我们发现 切比雪夫距离 可以在坐标旋转后转换为 曼哈顿距离

而曼哈顿距离可以在O(n)内计算出(求出每个x到xi的距离,同理求y,相加求和即可);

具体可以看我的代码:

/**

 * Problem:Meeting

 * Author:Shun Yao

 * Time:2013.5.21

 * Result:Accepted

 */



#include <cstring>

#include <cstdlib>

#include <cstdio>

#include <algorithm>



long n;

long long sumx, sumy, ans[100000], ANS;



class anode {

public:

	long long x, y;

	long id;

	anode() {}

	~anode() {}

} a[100000];



char cmpx(anode a, anode b) {

	return a.x < b.x;

}



char cmpy(anode a, anode b) {

	return a.y < b.y;

}



int main() {

	static long i;

	static long long xx, yy, sum;

	freopen("meeting.in", "r", stdin);

	freopen("meeting.out", "w", stdout);

	scanf("%ld", &n);

	sumx = sumy = 0;

	for (i = 0; i < n; ++i) {

		scanf("%lld%lld", &a[i].x, &a[i].y);

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

		sumy += a[i].y = a[i].x - (a[i].y << 1);

		a[i].id = i;

	}

	ANS = 1LL << 60;

	std::sort(a, a + n, cmpx);

	sum = 0;

	for (i = 0; i < n; ++i) {

		sum += a[i].x;

		ans[a[i].id] = ((i + 1 << 1) - n) * a[i].x + sumx - (sum << 1);

	}

	std::sort(a, a + n, cmpy);

	sum = 0;

	for (i = 0; i < n; ++i) {

		sum += a[i].y;

		ans[a[i].id] += ((i + 1 << 1) - n) * a[i].y + sumy - (sum << 1);

		if (ANS > ans[a[i].id])

			ANS = ans[a[i].id];

	}

	printf("%lld", ANS >> 1);

	fclose(stdin);

	fclose(stdout);

	return 0;

}

 

 

你可能感兴趣的:(T)