【HDU5721 BestCoder 2nd AnniversaryD】【平面最近点对 分治写法+KD-tree写法】Palace 平面最近点对

Palace

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 260    Accepted Submission(s): 72

Problem Description

The last trial Venus imposes on Psyche is a quest to the underworld. She is to take a box and obtain in it a dose of the beauty of Prosperina, queen of the underworld.

There are n
palaces in the underworld, which can be located on a 2-Dimension plane with
(x,y)
coordinates (where x,y
are integers). Psyche would like to find the distance of the closest pair of two palaces. It is the password to enter the main palace.

However, the underworld is mysterious and changes all the time. At different times, exactly one of then
palaces disappears.

Psyche wonders what the distance of the closest pair of two palaces is after some palace has disappeared.

Print the sum of the distance after every single palace has disappeared.

To avoid floating point error, define the distance
d
between palace (x1,y1)
and (x2,y2)
as d=(x1?x2)2+(y1?y2)2.

Input

The first line of the input contains an integer
T(1≤T≤5),
which denotes the number of testcases.

For each testcase, the first line contains an integers
n(3≤n≤105),
which denotes the number of temples in this testcase.

The following n
lines contains n
pairs of integers, the i-th
pair (x,y)(?105≤x,y≤105)
denotes the position of the i-th
palace.

Output

For each testcase, print an integer which denotes the sum of the distance after every single palace has disappeared.

Sample Input

1
3
0 0
1 1
2 2

Sample Output

12

Hint

If palace $ (0,0) $ disappears,$ d = (1-2) ^ 2 + (1 - 2) ^ 2 = 2 $;

If palace $ (1,1) $ disappears,$ d = (0-2) ^ 2 + (0 - 2) ^ 2 = 8 $;

If palace $ (2,2) $ disappears,$ d = (0-1) ^ 2 + (0-1) ^ 2 = 2 $;

Thus the answer is $ 2 + 8 + 2 = 12 $。


 

Source

BestCoder 2nd Anniversary

Recommend

wange2014

分治写法:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5 + 10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int casenum, casei;
struct Point
{
	LL x, y; int o;
}p[N], tmpp[N];
int n;
LL K(LL x)
{
	return x*x;
}
struct Ans
{
	int x, y; LL dis;
	Ans(int x = -1, int y = -1, LL dis = 1e18) :x(x), y(y), dis(dis) {};
	void update(Ans b)
	{
		if (b.dis < dis)
		{
			x = b.x;
			y = b.y;
			dis = b.dis;
		}
	}
};
Ans getDistance(Point &a, Point &b)
{
	LL dis = K(a.x - b.x) + K(a.y - b.y);
	return Ans(a.o, b.o, dis);
}
bool cmpxy(const Point& a, const Point& b)
{
	if (a.x != b.x)return a.x < b.x;
	return a.y < b.y;
}
bool cmpy(const Point& a, const Point& b)
{
	return a.y < b.y;
}
Ans res;
void Closest_Pair(int l, int r)
{
	if (l + 1 == r)
	{
		res.update(getDistance(p[l], p[r]));
	}
	else if (l + 2 == r)
	{
		res.update(getDistance(p[l], p[l + 1]));
		res.update(getDistance(p[l + 1], p[r]));
		res.update(getDistance(p[l], p[r]));
	}
	else
	{
		int mid = (l + r) >> 1;
		//先分治求左右子区间内部的最近公共点对
		Closest_Pair(l, mid);
		Closest_Pair(mid + 1, r);
		//再求左右子区间之间的最近公共点对
		int g = 0;
		for (int i = l; i <= r; ++i)
		{
			if (K(p[i].x - p[mid].x) < res.dis)tmpp[g++] = p[i];
		}
		sort(tmpp, tmpp + g, cmpy);
		for (int i = 0; i < g; ++i)
		{
			for (int j = i + 1; j < g && K(tmpp[j].y - tmpp[i].y) < res.dis; ++j)
			{
				res.update(getDistance(tmpp[j], tmpp[i]));
			}
		}
	}
}
int main()
{
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)
	{
		scanf("%d", &n);
		for (int i = 0; i < n; i++)scanf("%lld%lld", &p[i].x, &p[i].y);
		sort(p, p + n, cmpxy);
		for (int i = 0; i < n; i++)p[i].o = i;
		res = Ans(); Closest_Pair(0, n - 1); LL ans = res.dis * (n - 2);
		int x = res.x; int y = res.y;
		int tmpx = p[x].x; int tmpy = p[x].y;
		p[x].x = p[x].y = 1e9;
		res = Ans(); Closest_Pair(0, n - 1); ans += res.dis;
		p[x].x = tmpx; p[x].y = tmpy;
		p[y].x = p[y].y = 1e9;
		res = Ans(); Closest_Pair(0, n - 1); ans += res.dis;
		printf("%lld\n", ans);
	}
	return 0;
}
/*
【trick&&吐槽】
这题一眼标算,可惜不会模板TwT
最后学习了别人的模板改动过了初测然后FST,血崩!

【题意】
二维平面上有n个点
让你求,在每个点消失一次的情况下,剩余点的最近公共点对距离,并求和。

【类型】
最近公共点对
分治法 or KD-Tree

【分析】
显然,我们只要先求出不删点条件下的最近公共点对距离。
这个距离对答案的贡献系数是(n-2)
然后再分别删掉这2个端点,并再次求最近公共点对求和,答案就出来了。

【时间复杂度&&优化】
O(T * 3nlognlogn)

【数据】
1
5
3 1 1 1 1
3 5 7 5 5

*/

KD-tree写法

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<string>
#include<math.h>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
#define MS(x, y) memset(x, y, sizeof(x))
#define MC(x, y) memcpy(x, y, sizeof(x))
#define lson l,mid-1,(key+1)%Dim
#define rson mid+1,r,(key+1)%Dim
typedef long long LL;
const int Dim = 2;
const int N = 100010;
int Spe;
struct Point
{
	LL p[2]; int o;
}p[N], P;
int n;
struct Ans
{
	int x, y; LL dis;
	Ans(int x = -1, int y = -1, LL dis = 1e18) :x(x), y(y), dis(dis) {};
	void update(Ans b)
	{
		if (b.dis < dis)
		{
			x = b.x;
			y = b.y;
			dis = b.dis;
		}
	}
}ans;
LL K(LL x) { return x*x; }
Ans getDistance(Point &a, Point &b)
{
	if (b.o == Spe || a.o == b.o)return Ans();
	LL dis = K(a.p[0] - b.p[0]) + K(a.p[1] - b.p[1]);
	return Ans(a.o, b.o, dis);
}
int cmpkey;
bool cmp(Point x, Point y)
{
	return x.p[cmpkey] < y.p[cmpkey];
}
void build(int l, int r, int key)
{
	if (l >= r)return;
	int mid = (l + r) >> 1;
	cmpkey = key;
	nth_element(p + l, p + mid, p + r + 1, cmp);
	build(lson);
	build(rson);
}
void find(int l, int r, int key)
{
	if (l > r)return;
	int mid = (l + r) >> 1;
	ans.update(getDistance(P, p[mid]));
	if (P.p[key] < p[mid].p[key])
	{
		find(lson);
		if (K(P.p[key] - p[mid].p[key]) < ans.dis)find(rson);
	}
	if (P.p[key] > p[mid].p[key])
	{
		find(rson);
		if (K(P.p[key] - p[mid].p[key]) < ans.dis)find(lson);
	}
}
void solve()
{
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 0; j < Dim; ++j)scanf("%lld", &p[i].p[j]);
	}
	build(1, n, 0);
	for (int i = 1; i <= n; ++i)p[i].o = i;
	ans = Ans(); Spe = 0; LL sum = 0;
	for (int i = 1; i <= n; ++i)
	{
		P = p[i]; find(1, n, 0);
	}
	sum += ans.dis*(n - 2);
	int x = ans.x; int y = ans.y;
	ans = Ans(); Spe = x;
	for (int i = 1; i <= n; ++i)if (i != x)
	{
		P = p[i]; find(1, n, 0);
	}
	sum += ans.dis;
	ans = Ans(); Spe = y;
	for (int i = 1; i <= n; ++i)if (i != y)
	{
		P = p[i]; find(1, n, 0);
	}
	sum += ans.dis;
	printf("%lld\n", sum);

}
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
int casenum, casei;
int main()
{
	//fre();
	scanf("%d", &casenum);
	for (casei = 1; casei <= casenum; ++casei)solve();
	return 0;
}
/*
【trick&&吐槽】
这题一眼标算,可惜不会模板TwT
最后学习了别人的模板改动过了初测,然后FST,血崩!

这题还有另外一种KD-tree写法

【题意】
二维平面上有n个点
让你求,在每个点消失一次的情况下,剩余点的最近公共点对距离,并求和。

【类型】
最近公共点对
分治法 or KD-Tree

【分析】
显然,我们只要先求出不删点条件下的最近公共点对距离。
这个距离对答案的贡献系数是(n-2)
然后再分别删掉这2个端点,并再次求最近公共点对求和,答案就出来了。

【时间复杂度&&优化】
O(T * 3nsqrt(n))

【数据】
1
5
100000 100000
200000 200000
300000 300000
400000 400000
500000 500000

*/
时间: 2024-08-28 14:27:36

【HDU5721 BestCoder 2nd AnniversaryD】【平面最近点对 分治写法+KD-tree写法】Palace 平面最近点对的相关文章

HDU 5721 Palace BestCoder 2nd Anniversary (平面最近点对)

Palace Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 465    Accepted Submission(s): 118 Problem Description The last trial Venus imposes on Psyche is a quest to the underworld. She is to ta

BestCoder 2nd Anniversary的前两题

Oracle Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 79    Accepted Submission(s): 41 Problem Description There is once a king and queen, rulers of an unnamed city, who have three daughters

BestCoder 2nd Anniversary

http://acm.hdu.edu.cn/search.php?field=problem&key=BestCoder+2nd+Anniversary&source=1&searchmode=source A 取最小的非零数,再相加 1 // #pragma comment(linker, "/STACK:102c000000,102c000000") 2 #include <iostream> 3 #include <cstdio>

HDU 5720 Wool BestCoder 2nd Anniversary (区间覆盖)

Wool Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 686    Accepted Submission(s): 192 Problem Description At dawn, Venus sets a second task for Psyche. She is to cross a river and fetch gol

HDU 5719 BestCoder 2nd Anniversary Arrange (DP)

Arrange Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 558    Accepted Submission(s): 198 Problem Description Accidentally, Cupid, god of desire has hurt himself with his own dart and fallen

uva10245-The Closest Pair Problem(平面上的点分治)

解析:平面上的点分治,先递归得到左右子区间的最小值d,再处理改区间,肯定不会考虑哪些距离已经大于d的点对,对y坐标归并排序,然后从小到大开始枚举更新d,对于某个点,x轴方向只用考虑[x-d,x+d](x是分的中轴线),y轴方向只用考虑[y-d,y](y是这个点的y值),因为d值一直在变小,所以这个矩形包含的点数很少. 代码 #include<cstdio> #include<cstring> #include<string> #include<vector>

平面最近点对(分治nlogn)

平面最近点对,是指给出平面上的n个点,寻找点对间的最小距离 首先可以对按照x为第一关键字排序,然后每次按照x进行分治,左边求出一个最短距离d1,右边也求出一个最短距离d2,那么取d=min(d1, d2) 然后只需考虑横跨左右两侧的点,不妨枚举左侧的点pi 那么很显然的是如果pi距离中间的点超过了d,便可以直接舍去,只需考虑距离中间点小于d的点 这样一来就可以对每个pi画一个边长为2d的正方形,易证,矩形内最多存在8个点. 那么关键问题就是要快速找这8个点 朴素做法是对分治后的点进行快排,这样复

hdu 5719 BestCoder 2nd Anniversary B Arrange 简单计数问题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5719 题意:一个数列为1~N的排列,给定mn[1...n]和mx[1...n],问有符合的排列数为多少?如果不存在,输出0: 思路: 有解的几种条件: 1. mn , mx 变化单调: 2. mn,mx 不能同时变化: 3. 一个位置可选的个数>0; 当解存在时,递推出每次可选择的个数,num += mx[i] - mx[i-1] + mn[i-1] - mn[i] - 1; 即可: 坑:开始想成了

BestCoder 2nd Anniversary 1005&amp;Hdu 5722 -Jewelry

题意:问有多少个合法区间. 分析:对于[l,r],枚举右区间r,获取合法的l的区间.当增加一个元素Ai,原来合法的区间就会变不合法,要删掉,同时又会新增一个合法的区间,要插入. 例如,当x=2,对于元素 Ai其出现的位置为:1 2 3, 当新增位置4又出现Ai时,那么原来[1+1,2]的区间不合法,删掉.然后区间[2+1,3],插入. /************************************************ Author :DarkTong Created Time :