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(计算几何の最近点对)的更多相关文章

  1. 最近点对问题 POJ 3714 Raid && HDOJ 1007 Quoit Design

    题意:有n个点,问其中某一对点的距离最小是多少 分析:分治法解决问题:先按照x坐标排序,求解(left, mid)和(mid+1, right)范围的最小值,然后类似区间合并,分离mid左右的点也求最 ...

  2. poj 3714 Raid(平面最近点对)

    Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7473   Accepted: 2221 Description ...

  3. POJ 3714 Raid

    Description After successive failures in the battles against the Union, the Empire retreated to its ...

  4. poj 3714 Raid【(暴力+剪枝) || (分治法+剪枝)】

    题目:  http://poj.org/problem?id=3714 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27048#prob ...

  5. POJ 3714 Raid 近期对点题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/.未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  6. POJ 3714 Raid(平面近期点对)

    解题思路: 分治法求平面近期点对.点分成两部分,加个标记就好了. #include <iostream> #include <cstring> #include <cst ...

  7. (洛谷 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 ...

  8. 【POJ 3714】 Raid

    [题目链接] http://poj.org/problem?id=3714 [算法] 分治求平面最近点对 [代码] #include <algorithm> #include <bi ...

  9. 【POJ 3714】Raid

    [题目链接]:http://poj.org/problem?id=3714 [题意] 给你两类的点; 各n个; 然后让你求出2*n个点中的最近点对的距离; 这里的距离定义为不同类型的点之间的距离; [ ...

随机推荐

  1. linux下的学习之路下的小困难

    centos下源码安装python3wget --no-check-certificate https://www.python.org/ftp/python/3.6.2/Python-3.6.2.t ...

  2. Android中的AutoCompleteTextView(随笔提示文本)组件的简单使用

    Android中的随笔提示文本组件AutoCompleteTextView的使用,此组件用于输入文本,然后就会在所配置的适配器中的数据进行查找显示在组件下面. 这里值得注意的是AutoComplete ...

  3. windows下MySQL免安装版配置教程mysql-8.0.12-winx64.zip版本

    引用1:https://blog.csdn.net/weixin_42831477/article/details/81589325 引用2:https://blog.csdn.net/qq_3193 ...

  4. 嵌入式GPIO接口及操作(二)

    目标:C语言实现点亮LED灯 首先是main函数,并不特殊,它是被系统调用来执行的,main函数结束后要返回调用main函数的地址处,那么裸机程序,没有操作系统做这些工作,就要自己写调用main函数的 ...

  5. 5、GDB调试工具的使用

    GDB是GNU发布的一款功能强大的程序调试工具.GDB主要完成下面三个方面的功能: 1.启动被调试程序. 2.让被调试的程序在指定的位置停住. 3.当程序被停住时,可以检查程序状态(如变量值). #i ...

  6. SSH Secure :Algorithm negotiation failed,反复提示输入password对话框

    在嵌入式开发中,SSH Secure File Transfer Client 软件使用,方便了windows和linux之间文件拷贝,尤其是多台主机状况下. 最近装了Ubuntu 16.0.4,在V ...

  7. python 基础练习题, 陆续添加中

    判定用户输入数字是否为闰年 闰年的定义:能够被4整除的年份 #input是自定义输入内容的函数 year = input("请输入年份数字:") #xxx.isdigit方法是检测 ...

  8. docker swarm的应用----docker集群的构建

    一.docker安装 这里我们安装docker-ce 的18.03版本 yum    -y remove docker  删除原有版本 #安装依赖包 [root@Docker ~]# yum -y i ...

  9. window下创建虚拟环境

    一. windows下创建虚拟环境 1. 终端下执行命令:python -m pip install -upgrade pip 2. pip install virtualenv 3. 在本地创建一个 ...

  10. 快速创建一个vue项目

    vue脚手架 用来创建vue项目的工具包 创建项目: npm install -g vue-cli vue init webpack VueDemo 开发环境运行: cd VueDemo npm in ...