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. Spring 一二事(4) - 单例

    spring bean配置后再默认情况下是单例的,如果需要配置可以选择 prototype, request, session和global session 在配置spring mvc的action时 ...

  2. flex4 s:Datagrid <s:typicalItem

    <s:DataGird <s:typicalItem 这个标签相信大家很陌生吧, 我也是今天准备讲的时候才看到,估计是 flex4.5.1 新加东西,果然摸索 了下,这个标签作用也蛮好用的 ...

  3. smarty中三种变量的访问方式

    在模板中smarty有三种变量,第一种,php分配的变量,第二种配置文件里的变量,第三种,PHP全局数组里的变量,配置文件里变量的访问方式可以是{#bgcolor#},"#"必须紧 ...

  4. setAttribute改变属性,动态改变类

    <style type="text/css"> .box{color:red;} </style> <div>通过setAttribute添加d ...

  5. eval() 函数

    eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. var str = '12+45*45'; alert(eval(str))//计算结果 还有一个重要作用可以把字符串 ...

  6. iOS之block

    1. Block的声明和线程安全Block属性的声明,首先需要用copy修饰符,因为只有copy后的Block才会在堆中,栈中的Block的生命周期是和栈绑定的,可以参考之前的文章(iOS: 非ARC ...

  7. Android Studio使用中的异常

    Android studio教程:[4]真机测试 1.连不上手机 Android Studio识别不了手机(最后还是恢复成勾中的状态),重启,Android Studio连接真机没反应? 2.连上手机 ...

  8. C语言 百炼成钢14

    //题目40:输入3个数a,b,c,按大小顺序输出.(使用指针完成) #include<stdio.h> #include<stdlib.h> //分析:用指针完成,说明不可以 ...

  9. 挖Linux中的古老缩略语

    [2005-06-22 15:23][Nigel McFarlane][TechTarget] <<阅读原文>> Unix已经有35年历史了.许多人认为它开始于中世纪,这个中世 ...

  10. 使用Uploadify实现上传图片生成缩略图例子,实时显示进度条

    不了解Uploadify的,先看看前一篇详细说明 http://www.cnblogs.com/XuebinDing/archive/2012/04/26/2470995.html Uploadify ...