【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卡 ...
随机推荐
- 3D跑酷遇到的问题
前言 项目名称:3D跑酷项目 写作目地:使用Asset Server进行多人协作开发过程中,记录遇到的问题 问题1:UIAtlas无法自动更新 描述:NGUI的Atlas图集(图片)无法自动更新 后果 ...
- 一些MEL命令
这几天写maya脚本,发现一些新命令: 动画命令 cutKey 剪切某段动画曲线 simplify 简化某段曲线 基本命令 getAttr -size 数组属性名 获得数组属性的元素个数 ...
- Node.js开发环境搭建
1.安装express npm install express -g 2.express33.6以后把express-generator分离出来了,所以还需安装express-generator,否则 ...
- [py]os.walk爬目录&sys.argv灵活获取参数
1, 遍历目录 os.walk('/tmp') os.next() 2,sys.argv ######################################## py@lanny:~/t ...
- Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别
Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...
- caffe windows 学习第一步:编译和安装(vs2012+win 64)
没有GPU,没有linux, 只好装caffe的windows版本了. 我的系统是win10(64位),vs 2012版本,其它什么都没有装,因此会需要一切的依赖库. 其实操作系统只要是64位就行了, ...
- 使用ObjectAnimator设置动画
ObjectAnimator是ValueAnimator的子类,他本身就已经包含了时间引擎和值计算,所以它拥有为对象的某个属性设置动画的功能.这使得为任何对象设置动画更加的容易.你不再需要实现 Val ...
- SpringMvc学习心得(五)控制器产生与构建
SpringMvc学习心得(五)控制器产生与构建 标签: springspring mvc框架 2016-03-22 15:29 140人阅读 评论(0) 收藏 举报 分类: Spring(4) ...
- Java系列:JVM指令详解(上)(zz)
一.未归类系列A 此系列暂未归类. 指令码 助记符 说明 59:iastore 60:lload 6 //因为str ...
- 通过数据库方式访问excel 2007及其以后(xlsx)文件的连接字符串
sqlconn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\test3.xls;Extended Properties=&q ...