【poj3714】 Raid
http://poj.org/problem?id=3714 (题目链接)
现在才搞平面最近点对。。感觉有点尴尬
题意
给出平面上两组点,每组n个,求两组点之间最短距离
Solution1
平面最近点对,分治即可。
将点按横坐标排序,然后每次二分成左边和右边分别计算最小距离,再计算中间的最小距离,这里需要把中间符合条件的点按照纵坐标排序,然后当当前枚举的两点的纵坐标之差大于答案时break,否则会TLE。
代码1
// poj3714
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<vector>
#define inf 2147483640
#define LL long long
#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
LL x=0,f=1;char ch=getchar();
while (ch>'9' || ch<'0') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
} const int maxn=1000010;
struct point {double x,y;int flag;}p[maxn];
int n,tmp[maxn]; bool cmpx(point a,point b) {
return a.x==b.x ? a.y<b.y : a.x<b.x;
}
bool cmpy(int a,int b) {
return p[a].y==p[b].y ? p[a].x<p[b].x : p[a].y<p[b].y;
}
double dis(point a,point b) {
return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double solve(int l,int r) {
double res=1e60;
if (l==r) return res;
if (l+1==r) {
if (p[l].flag==p[r].flag) return res;
return dis(p[l],p[r]);
}
int mid=(l+r)>>1;
res=solve(l,mid);
res=min(res,solve(mid+1,r));
int num=0;
for (int i=l;i<=r;i++)
if (fabs(p[i].x-p[mid].x)<=res) tmp[++num]=i;
sort(tmp+1,tmp+num+1,cmpy);
for (int i=1;i<=num;i++)
for (int j=i+1;j<=num;j++) {
if (fabs(p[tmp[i]].y-p[tmp[j]].y)>=res) break; //剪枝
if (p[tmp[i]].flag!=p[tmp[j]].flag) res=min(res,dis(p[tmp[i]],p[tmp[j]]));
}
return res;
}
int main() {
int T;
scanf("%d",&T);
while (T--) {
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y),p[i].flag=0;
for (int i=1;i<=n;i++) scanf("%lf%lf",&p[i+n].x,&p[i+n].y),p[i+n].flag=1;
n<<=1;
sort(p+1,p+1+n,cmpx);
printf("%.3f\n",solve(1,n));
}
return 0;
}
Solution2
hzwer上惊现平面最近点对的随机化算法(貌似是随机分块),于是我就蒯了过来,虽然并不知道为什么可以这样写,但是好像很厉害的样子。
上网搜了下,发现期望复杂度是O(n)的。度娘链接
然而= =:

比分治还跑的慢,坑比东西。
代码2
// poj3714
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std;
inline LL getint() {
int f,x=0;char ch=getchar();
while (ch<='0' || ch>'9') {if (ch=='-') f=-1;else f=1;ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
} const int maxn=1000010;
struct point {double x,y;int flag;}p[maxn];
int n,block,m; bool cmp(point a,point b) {
return a.x==b.x ? a.y<b.y : a.x<b.x;
}
point rotate(point a,double x) {
return (point){(double)a.x*cos(x)-(double)a.y*sin(x),(double)a.y*cos(x)+(double)a.x*sin(x),a.flag};
}
double dis(point a,point b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main() {
int T;scanf("%d",&T);
while (T--) {
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y),p[i].flag=0;
for (int i=1;i<=n;i++) scanf("%lf%lf",&p[i+n].x,&p[i+n].y),p[i+n].flag=1;
n<<=1;
block=(int)sqrt(n);
m=n/block+(n%block!=0);
double t=rand()/10000;
for (int i=1;i<=n;i++) p[i]=rotate(p[i],t);
sort(p+1,p+1+n,cmp);
double ans=1e60;
for (int i=1;i<=m;i++) {
int t1=block*(i-1),t2=min(block*i,n);
for (int j=t1;j<=t2;j++)
for (int k=t1+1;k<=t2;k++) if (p[j].flag!=p[k].flag) ans=min(ans,dis(p[j],p[k]));
}
printf("%.3f\n",ans);
}
return 0;
}
【poj3714】 Raid的更多相关文章
- 【POJ3714】Raid:平面最近点对
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- [转帖]【译】RAID的概念和RAID对于SQL性能的影响
[译]RAID的概念和RAID对于SQL性能的影响 https://www.cnblogs.com/VicLiu/p/11479427.html 简介 我们都听说过RAID,也经常作为SQL DBA. ...
- 【转】RAID 简介
原文:http://wiki.dzsc.com/info/4972.html RAID 的英文全称为 Redundant Array of Inexpensive(或 Independent) Dis ...
- 【转载】RAID写惩罚(Write Penalty)与IOPS计算
浅谈RAID写惩罚(Write Penalty)与IOPS计算 Character is what you are in the dark. 暗处最能反映一个人真正品格. ---------Apri ...
- 【存储】RAID磁盘阵列选择
RAID磁盘阵列(Redundant Arrays of Inexpensive Disks) 一个基本思想:将多个容量较小.相对廉价的磁盘进行有机组合,从而以较低的成本获得与昂贵大容量磁盘相当的容量 ...
- 【转】RAID 技术发展综述
原文地址:https://blog.csdn.net/liuaigui/article/details/4581970 摘要 :现代企业信息化水平不断提高,数据已经取代计算成为了信息计算的中心.这 ...
- 【译】RAID的概念和RAID对于SQL性能的影响
简介 我们都听说过RAID,也经常作为SQL DBA.开发人员或构架师在工作中讨论RAID.但是,其实我们很多人都对RAID的原理,等级,以及RAID是如何影响SQL Server性能并不甚了解. 本 ...
- 【硬盘】RAID
RAID是英文Redundant Array of Independent Disks(独立磁盘冗余阵列),简称磁盘阵列.下面将各个级别的RAID介绍如下. 一.为什么使用Raid? 1.对磁盘高速存 ...
- 【硬盘】RAID卡
独立磁盘冗余阵列,或简称磁盘阵列(Redundant Array of Independent Disks) RAID是一种把多块独立的物理硬盘按不同方式组合起来形成一个逻辑硬盘,一般分为硬RAID卡 ...
随机推荐
- 真人动作捕捉系统 for Unity
真人动作捕捉 在Asset Store中浏览Mecanim相关的资源时,发现了这个 资源信息 Asset Store:https://www.assetstore.unity3d.com/#/cont ...
- 使用PS3手柄在PC玩Unity3D游戏
PS3手柄玩Unity游戏 今天把公司的PS3手柄接到PC上,想用手柄试一下玩赛车的感觉,老感觉用键盘按键玩的不爽. 把PS3的手柄接到PC上之后,系统提示正在安装驱动--,百度找资料,如何在PC上使 ...
- iOS app上传错误集锦(转载)
1.工程里增加了版本自动更新. 2.未增加判断网络状态的类Reachability. 3.问题:error itms -90049 This bundel is invalid. The bundle ...
- js模拟手机触摸屏
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- PHP安装memcache扩展接口步骤
1.将php_memcache.dll文件保存到php的应用程序扩展ext目录中 2.在php.ini配置文件添加扩展的位置,加入一行extension=php_memcache.dll 3.重新启动 ...
- javascript js string.Format()收集
方案1http://www.cnblogs.com/loogn/archive/2011/06/20/2085165.html String.prototype.format = function(a ...
- 阻塞与非阻塞IO step by step
谈到IO,阻塞.非阻塞,异步.同步是绕不开的话题.说实话,我也没搞清楚,网上查了许多资料,大家众说纷纭,一种比较靠谱的说法是:”在处理 IO 的时候,阻塞和非阻塞都是同步 IO,使用使用了特殊的API ...
- ztree插件(JQuery Tree)
本次使用的ztree插件,基本上所有的需求都能满足,可谓功能强大. * [http://www.ztree.me/v3/api.php zTree v3.0 API 文档] * [http://www ...
- [iOS翻译]《iOS 7 Programming Cookbook》:iOS文件与文件夹管理(上)
简介: iOS基于OS X,而OSX本身基于Unix操作系统.在iOS里面,操作系统的完全路径结构是不可见的,因为每个APP的数据都存储自身的沙盒里面.沙盒环境实际上听起来像这样:一个只允许当前APP ...
- ZooKeeper学习第八期——ZooKeeper伸缩性
一.ZooKeeper中Observer 1.1 ZooKeeper角色 经过前面的介绍,我想大家都已经知道了在ZooKeeper集群当中有两种角色Leader和Follower.Leader可以接受 ...