POJ 3714 Raid(计算几何の最近点对)
Description
After successive failures in the battles against the Union, the Empire retreated to its last stronghold. Depending on its powerful defense system, the Empire repelled the six waves of Union's attack. After several sleepless nights of thinking, Arthur, General of the Union, noticed that the only weakness of the defense system was its energy supply. The system was charged by N nuclear power stations and breaking down any of them would disable the system.
The general soon started a raid to the stations by N special agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur soon realized that he needed to rearrange the plan. The first thing he wants to know now is that which agent is the nearest to any power station. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?
Input
The first line is a integer T representing the number of test cases.
Each test case begins with an integer N (1 ≤ N ≤ 100000).
The next N lines describe the positions of the stations. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the station.
The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the agent.
题目大意:给一个点集A,一个点集B,求min(distance(x, y))(x∈A,y∈B)
思路1:分治法。把点集按x坐标从小到大排序,mid = (left + right)/2,递归分治计算出左边部分和右边部分的最小距离mind,那么,若左半部分和右半部分存在一对点距离小于mind,那么这两个点一定在范围(x[mid] -mind ,x[mid] -mind)之间(因为在这之外的点与对面的点的距离必然大于mind)
思路2:暴力枚举+剪枝。暴力枚举每两个点之间的距离,若y[j] - y[i] >= mind则break(这个不用解释了吧……)
PS:要保留3位小数,题目没说,可以看样例
分治算法:1141MS
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; const double MAX_DIST = 1e100;
const int MAXN = ; struct Point {
double x, y;
bool flag;
}; inline double dist(const Point &a, const Point &b) {
if(a.flag != b.flag)
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
else
return MAX_DIST;
} Point pt[MAXN];
int y_sort[MAXN]; inline bool x_cmp(const Point &a, const Point &b) {
return a.x < b.x;
} inline bool y_cmp(const int &a, const int &b) {
return pt[a].y < pt[b].y;
} double shortest_distance(int left, int right) {
if(right - left == )
return dist(pt[left], pt[right]);
else if(right - left == )
return min(min(dist(pt[left], pt[left+]), dist(pt[left], pt[left+])),
dist(pt[left+], pt[left+]));
int mid = (left + right) >> ;
double mind = min(shortest_distance(left, mid), shortest_distance(mid+, right));
if(mind == ) return ;
int yn = ;
for(int i = mid; pt[mid].x - pt[i].x < mind && i >= left; --i)
y_sort[yn++] = i;
int y_mid = yn;
for(int i = mid+; pt[i].x - pt[mid].x < mind && i <= right; ++i)
y_sort[yn++] = i;
for(int i = ; i < y_mid; ++i) for(int j = y_mid; j < yn; ++j)
mind = min(mind, dist(pt[y_sort[i]], pt[y_sort[j]]));
return mind;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
for(int i = ; i < n; ++i) {
scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[i].flag = false;
}
for(int i = n; i < *n; ++i) {
scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[i].flag = true;
}
sort(pt, pt + *n, x_cmp);
printf("%.3f\n", shortest_distance(, * n - ));
}
}
分治算法:1485MS
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; const double MAX_DIST = 1e100;
const int MAXN = ; struct Point {
double x, y;
bool flag;
}; inline double dist(const Point &a, const Point &b) {
if(a.flag != b.flag)
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
else
return MAX_DIST;
} Point pt[MAXN];
int y_sort[MAXN]; inline bool x_cmp(const Point &a, const Point &b) {
return a.x < b.x;
} inline bool y_cmp(const int &a, const int &b) {
return pt[a].y < pt[b].y;
} void _min(double &a, const double &b) {
if(a > b) a = b;
} double shortest_distance(int left, int right) {
if(right - left == )
return dist(pt[left], pt[right]);
else if(right - left == )
return min(min(dist(pt[left], pt[left+]), dist(pt[left], pt[left+])),
dist(pt[left+], pt[left+]));
int mid = (left + right) >> ;
double mind = min(shortest_distance(left, mid), shortest_distance(mid+, right));
if(mind == ) return ;
int yn = ;
for(int i = mid; pt[mid].x - pt[i].x < mind && i >= left; --i)
y_sort[yn++] = i;
for(int i = mid+; pt[i].x - pt[mid].x < mind && i <= right; ++i)
y_sort[yn++] = i;
sort(y_sort, y_sort + yn);
for(int i = ; i < yn; ++i) {
for(int j = i + ; j < yn; ++j) {
if(pt[y_sort[j]].y - pt[y_sort[i]].y >= mind) break;
_min(mind, dist(pt[y_sort[i]], pt[y_sort[j]]));
}
}
return mind;
} int main() {
freopen("f:/data.in", "r", stdin);
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
for(int i = ; i < n; ++i) {
scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[i].flag = false;
}
for(int i = n; i < *n; ++i) {
scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[i].flag = true;
}
sort(pt, pt + *n, x_cmp);
printf("%.3f\n", shortest_distance(, * n - ));
}
}
暴力枚举+剪枝:1032MS
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; const int MAXN = ; struct Point {
double x, y;
bool flag;
}; inline double dist(const Point &a, const Point &b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
} Point pt[MAXN]; inline bool cmp(const Point &a, const Point &b) {
if(a.x == b.x) return a.y < b.y;
return a.x < b.x;
} double shortest_distance(int pn) {
double mind = dist(pt[], pt[pn/]);
sort(pt, pt + pn, cmp);
for(int i = ; i < pn; ++i) {
for(int j = i + ; j < pn; ++j) {
if(pt[i].flag == pt[j].flag) continue;
double t = dist(pt[i], pt[j]);
if(mind > t) mind = t;
if(pt[j].x - pt[i].x >= mind) break;
}
}
return mind;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
int n;
scanf("%d", &n);
for(int i = ; i < n; ++i) {
scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[i].flag = false;
}
for(int i = n; i < *n; ++i) {
scanf("%lf%lf", &pt[i].x, &pt[i].y);
pt[i].flag = true;
}
printf("%.3f\n", shortest_distance( * n));
}
}
后记:这分治法能卡(暴力剪枝肯定能卡……),下面是生成卡分治数据的代码……
void gen1()
{
int N = , i;
printf("%d\n", N);
for( i = ; i < N; ++i )
printf("%d %d\n", -+i, i+);
for( i = ; i < N; ++i )
printf("%d %d\n", +i, i+); printf("%d\n", N);
for( i = ; i < N; ++i )
printf("%d %d\n", i-, -+i);
for( i = ; i < N; ++i )
printf("%d %d\n", i-, +i);
}
然后别人给的代码,待研究,出处不明……某份数据依然要4秒之多,但是要是用上面我的代码直接就不会动了……
初步估计,下面的代码用到了那个啥K-D tree,就是把一个二维空间先分x轴、再分y轴、再分x轴、再分y轴……分成多个平面,而不是像上面那样只分x(或y)轴……然后还是分治……代码我就懒得看了……超出我的水平上限了……
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <iostream>
#include <cmath> using namespace std; //维数
int K = ;
long long ans; struct kdNode
{
long long x[];
int div;
}; struct qNode
{
long long r;
kdNode p;
qNode( long long _r, kdNode _p )
{
r = _r;
p = _p;
}
bool operator<( const qNode& x ) const
{
return r < x.r;
}
}; int cmpNo;
int cmp( kdNode a, kdNode b )
{
return a.x[cmpNo] < b.x[cmpNo];
} long long dis2( kdNode& a, kdNode& b )
{
long long res = ;
for( int i = ; i < K; ++i )
res += (a.x[i]-b.x[i])*(a.x[i]-b.x[i]);
return res;
} void buildKD( int s, int e, kdNode* p, int d )
{
if( s > e ) return;
int mid = (s+e)/;
cmpNo = d;
nth_element(p+s, p+mid, p+e+, cmp);
p[mid].div = d;
buildKD(s, mid-, p, (d+)%K);
buildKD(mid+, e, p, (d+)%K);
} priority_queue<qNode> Q; void findKD( int s, int e, kdNode tar, kdNode* p, int cnt )
{
if( s > e ) return;
int mid = (s+e)/;
long long r = dis2(p[mid], tar);
ans = min(ans, r);
//t也许需要改成long long
long long t = tar.x[ p[mid].div ] - p[mid].x[ p[mid].div ];
if( t <= )
{
findKD(s, mid-, tar, p, cnt);
if( ans > t*t )
findKD(mid+, e, tar, p, cnt);
}
else if( t > )
{
findKD(mid+, e, tar, p, cnt);
if( ans > t*t )
findKD(s, mid-, tar, p, cnt);
}
} kdNode p[], q; int main()
{
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
int T, n, i, j;
int x, y;
scanf("%d", &T);
while( T-- )
{
scanf("%d", &n);
for( i = ; i < n; ++i )
{
scanf("%d %d", &x, &y);
p[i].x[] = x;
p[i].x[] = y;
}
ans = 4100000000000000000LL;
buildKD(, n-, p, K-);
for( i = ; i < n; ++i )
{
scanf("%d %d", &x, &y);
q.x[] = x;
q.x[] = y;
findKD(, n-, q, p, );
}
printf("%.3f\n", sqrt(ans+.));
}
return ;
}
POJ 3714 Raid(计算几何の最近点对)的更多相关文章
- 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design
题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...
- poj 3714 Raid(平面最近点对)
Raid Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7473 Accepted: 2221 Description ...
- POJ 3714 Raid
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】
题目: http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...
- POJ 3714 Raid 近期对点题解
版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- POJ 3714 Raid(平面近期点对)
解题思路: 分治法求平面近期点对.点分成两部分,加个标记就好了. #include <iostream> #include <cstring> #include <cst ...
- (洛谷 P1429 平面最近点对(加强版) || 洛谷 P1257 || Quoit Design HDU - 1007 ) && Raid POJ - 3714
这个讲的好: https://phoenixzhao.github.io/%E6%B1%82%E6%9C%80%E8%BF%91%E5%AF%B9%E7%9A%84%E4%B8%89%E7%A7%8D ...
- 【POJ 3714】 Raid
[题目链接] http://poj.org/problem?id=3714 [算法] 分治求平面最近点对 [代码] #include <algorithm> #include <bi ...
- 【POJ 3714】Raid
[题目链接]:http://poj.org/problem?id=3714 [题意] 给你两类的点; 各n个; 然后让你求出2*n个点中的最近点对的距离; 这里的距离定义为不同类型的点之间的距离; [ ...
随机推荐
- mysql复制表结构和数据
1.复制表结构: create table newName like oldName;//可以复制所有结构. 或者: create table newName select * from oldNam ...
- jquery优化
选择器优化执行的速度 选择器 优先:id>元素>类 使用对象缓存:即使用变量来保存对象名,var $myDiv = $("#myDiv"):$myDiv.show(); ...
- laydate5.0 设置最大最小值
由于新版的laydate时间插件在初始化时已设置时间最大最小范围,且生成对象,无法重新渲染改变其日期最大最小值. 有网友经实验贴出如下方法可达成目的,故做记录. //开始时间 var startDat ...
- S3C2440上LCD驱动(FrameBuffer)实例开发讲解(一)
一.开发环境 主 机:VMWare--Fedora 9 开发板:Mini2440--64MB Nand, Kernel:2.6.30.4 编译器:arm-linux-gcc-4.3.2 二.背景知识 ...
- Makefile中的$(MAKE)
今天看uboot2018顶层的Makefile中发现文件中export一个MAKE变量,export是为了向底层的Makefile传递这些变量参数,但是找了半天没有找到这个MAKE变量在哪定义的. 决 ...
- 用Python生成词云
词云以词语为基本单元,根据词语在文本中出现的频率设计不同大小的形状以形成视觉上的不同效果,从而使读者只要“一瞥“即可领略文本的主旨.以下是一个词云的简单示例: import jieba from wo ...
- win10 禁用自动更新
管理员身份运行CMD,输入REG add "HKLM\SYSTEM\CurrentControlSet\Services\UsoSvc" /v "Start" ...
- BZOJ2761_不重复数字_KEY
题目传送门 Map水过(或set也行). code: /************************************************************** Problem: ...
- 北京Uber优步司机奖励政策(3月23日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- MySQL高级-全局查询日志
注意:全局查询日志不要在生成环境中启用 一.配置启用 二.编码启用