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的更多相关文章

  1. 【POJ3714】Raid:平面最近点对

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

  2. [转帖]【译】RAID的概念和RAID对于SQL性能的影响

    [译]RAID的概念和RAID对于SQL性能的影响 https://www.cnblogs.com/VicLiu/p/11479427.html 简介 我们都听说过RAID,也经常作为SQL DBA. ...

  3. 【转】RAID 简介

    原文:http://wiki.dzsc.com/info/4972.html RAID 的英文全称为 Redundant Array of Inexpensive(或 Independent) Dis ...

  4. 【转载】RAID写惩罚(Write Penalty)与IOPS计算

    浅谈RAID写惩罚(Write Penalty)与IOPS计算 Character is what you are in the dark. 暗处最能反映一个人真正品格. ---------Apri ...

  5. 【存储】RAID磁盘阵列选择

    RAID磁盘阵列(Redundant Arrays of Inexpensive Disks) 一个基本思想:将多个容量较小.相对廉价的磁盘进行有机组合,从而以较低的成本获得与昂贵大容量磁盘相当的容量 ...

  6. 【转】RAID 技术发展综述

    原文地址:https://blog.csdn.net/liuaigui/article/details/4581970   摘要 :现代企业信息化水平不断提高,数据已经取代计算成为了信息计算的中心.这 ...

  7. 【译】RAID的概念和RAID对于SQL性能的影响

    简介 我们都听说过RAID,也经常作为SQL DBA.开发人员或构架师在工作中讨论RAID.但是,其实我们很多人都对RAID的原理,等级,以及RAID是如何影响SQL Server性能并不甚了解. 本 ...

  8. 【硬盘】RAID

    RAID是英文Redundant Array of Independent Disks(独立磁盘冗余阵列),简称磁盘阵列.下面将各个级别的RAID介绍如下. 一.为什么使用Raid? 1.对磁盘高速存 ...

  9. 【硬盘】RAID卡

    独立磁盘冗余阵列,或简称磁盘阵列(Redundant Array of Independent Disks) RAID是一种把多块独立的物理硬盘按不同方式组合起来形成一个逻辑硬盘,一般分为硬RAID卡 ...

随机推荐

  1. 常用的adb命令

    在平时的工作中,会经常用到adb命令,在这里稍微整理了一下. 一.概要 1.什么是adb? adb全称为Android Debug Bridge,就是起到调试桥的作用.顾名思义,adb就是一个debu ...

  2. pl/sql developer中的SQL语句

    1.无论是在查询还是在插入的时候,值都必须是单引号‘’,否则会报错,ORA-00904:标识符无效.

  3. 16Mybatis_动态sql_if判断

    mybatis的核心就是动态sql. 什么是动态sql:对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 这篇文章讲解sql中的if语句.它可以对查询条件进行判断,如果输入参 ...

  4. U3D5.3.5f Monodevelop 仅支持到.NET 3.5

    2016年12月2号:发现这个标题是错误的,可以在monodevelop中选择.NET的版本,如下:打开solution,右击 Assembly-CSharp,options, build, gene ...

  5. 基于EventAggregator的事件发布及订阅

    EventAggregator简介 EventAggregator是Prism中专门处理ViewModel与ViewModel之间事件传递的类对象,它提供了针对事件的发布方法和订阅方法,所以可以非常方 ...

  6. 如何利用花生壳和VisualSVN Server建立远程代码仓库

    如何利用花生壳和VisualSVN建立远程代码仓库 最近由于项目需要,要远程访问实验室的svn服务器,但是实验室没有固定域名和ip,因此就打算用花生壳申请一个免费的域名构建一个服务器,再把Visual ...

  7. 超全!iOS 面试题汇总

    之前看了很多面试题,感觉要不是不够就是过于冗余,于是我将网上的一些面试题进行了删减和重排,现在分享给大家.(题目来源于网络,侵删) 1. Object-c的类可以多重继承么?可以实现多个接口么?Cat ...

  8. rem详解及使用方法

    好像有一段时间没有写博客了……今天刚好总结一下rem的使用方法 首先,先说一个常识,浏览器的默认字体高都是16px.步入正题-----〉 兼容性: 目前,IE9+,Firefox.Chrome.Saf ...

  9. Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tool

    重装操作系统后,要重新配置Android开发环境.配置成功后,添加原本项目时却出现了错误! Android requires compiler compliance level 5.0 or 6.0. ...

  10. CUDA编程学习(三)

    我们知道一个grid包含多个block,而一个block又包含多个thread,下面将是如何进行下thread中的并行. /**** Splot a block into parallel threa ...