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个点中的最近点对的距离; 这里的距离定义为不同类型的点之间的距离; [ ...
随机推荐
- Vue.js与 ASP.NET Core 服务端渲染功能整合
http://mgyongyosi.com/2016/Vuejs-server-side-rendering-with-aspnet-core/ 原作者:Mihály Gyöngyösi 译者:oop ...
- golang刷Leetcode系列 --- 加1
加一 给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 ...
- 竞赛题解 - Karp-de-Chant Number(BZOJ-4922)
Karp-de-Chant Number(BZOJ-4922) - 竞赛题解 进行了一次DP的练习,选几道题写一下博客~ 标签:BZOJ / 01背包 / 贪心 『题目』 >> There ...
- Mac下PHP的环境搭建
* 前段时间手欠 ... 入手了一个二手的Macbook pro ! 配置挺高的 16款13寸的基本顶配了 ... 只差 硬盘不是1T的 ... 可以脑补一下配置了* 话说 不是所有程序猿都说 每个程 ...
- 使用PHPExcel 读取 表格数据, 发现中文全变成 FALSE??
出现这样的情况, 你可以看看你的表格是不是 CSV 格式的. 如果是, 那就赶紧另保存为 xls.xlsx 等格式的表格 . 因为 PHPExcel 对 Csv 的表格不感冒....
- 第2天 Java基础语法
第2天 Java基础语法 今日内容介绍 变量 运算符 变量 变量概述 前面我们已经学习了常量,接下来我们要学习变量.在Java中变量的应用比常量的应用要多很多.所以变量也是尤为重要的知识点! 什么是变 ...
- C语言小程序-基于链表的学生信息管理
程序支持增加.查询.删除.存盘和读取操作 一 程序定义和函数声明 头文件studentsys.h定义如下 /* student management system by list */ #ifndef ...
- 【数据结构】线性表&&顺序表详解和代码实例
喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 01 预备知识 1.0 什么是线性表? 线性表(List)是零个或者多个数据元素的有限序列. 首先它是一个序列.里面的元素是有顺 ...
- 如何在VMware Fusion中导入windows下的虚拟机
最近换了新款的mbp,因为偷懒,便将之前在windows台式机上的虚拟机搬了过来. 特此记录下搬运过程,方便以后查看. 一 操作过程 安装激活VMware 常规操作,无需赘言 拷贝windows下虚拟 ...
- Applied Cloud Deep Semantic Recognition: Advanced Anomaly Detection(应用云深层语义识别:高级异态检测)
亚马逊链接 引言 (by Mehdi Roopaei & Paul Rad) 异态检测与情境感知 在数据分析领域,异态检测讲的是在一个数据集中,发现到其中不符合预期模式的物体,动作,行为或事件 ...