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个点中的最近点对的距离; 这里的距离定义为不同类型的点之间的距离; [ ...
随机推荐
- HDU 5572--An Easy Physics Problem(射线和圆的交点)
An Easy Physics Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- PHP程序员面试中经常被提问的问题【转载】
1. Include 与 require的区别,require和require_once的效率哪个高? Php在遇到include时就解释一次,如果页面中出现10次include,php就解释10次, ...
- jQuery基本toggle() toggleClass() 使用
今天来学习一下jQuery的基本函数的使用,很简单. 首先写一个button做控制按钮,然后写一个div用按钮控制idv做动画,从而测试JQuery的动画函数 <head> <met ...
- 「PHP」观察者模式模式
引言 所属:行为型模式,常用设计模式之一 学习资料: <大话设计模式>程杰 模式概述 观察者模式定义了一种一对多的依赖关系,让多个观察者对象监听某一个主题对象.这个主题 ...
- 微信小程序数据分析之自定义分析
在小程序后台,微信已经提供了强大的数据分析功能,包括实时统计.访问分析.来源分析和用户画像功能,可以说对一般的数据分析已经完全足够了,但有时应用需要做一些更加精准的数据分析,比如具体到某一个页面的分享 ...
- OpenWrt超时检测
参考http://www.right.com.cn/forum/thread-261702-1-1.html vim /home/ihid/chaos_calmer/feeds/luci/module ...
- consonant_摩擦音
consonant_摩擦音_[t∫].[dʒ].[tr].[dr].[ts].[dz] 破擦音:即有爆破音又有摩擦音. [t∫]:噘嘴,舌尖抵住上牙龈,舌头下切,用一瞬间的气流发出声音,不震动. ch ...
- Leecode刷题之旅-C语言/python-121买卖股票的最佳时机
/* * @lc app=leetcode.cn id=121 lang=c * * [121] 买卖股票的最佳时机 * * https://leetcode-cn.com/problems/best ...
- C++实现json字符串与map的转换
开源资源库 jsoncpp-src-0.5.0.tar.gz:https://sourceforge.net/projects/jsoncpp/ jsoncpp-master.ziphttps://g ...
- C语言小程序-基于链表的学生信息管理
程序支持增加.查询.删除.存盘和读取操作 一 程序定义和函数声明 头文件studentsys.h定义如下 /* student management system by list */ #ifndef ...