【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卡 ...
随机推荐
- JMeter学习(三十一)Access Log Sampler
前提: 在tomcat\conf\server.xml默认情况下,会有一段代码: <Valve className="org.apache.catalina.valves.Access ...
- linux部署的java应用,浏览器访问时,报域名解析错误
工作中,经常需要在Linux环境中部署Tomcat,配置java应用.在浏览器中访问应用时,却报域名解析错误,该怎么样解决呢? 解决方法:关闭防火墙 iptables -L -n 查看已添加的 ...
- Intellij IDEA 快捷键(Mac)
编辑 格式化代码 Alt+Command+L 大小写切换 Shift+Command+U 包围 Alt+Command+T 选中代码抽取方法 Alt+Command+M 调试/运行 查看 类关系视图 ...
- ip routing&no ip routing
ip routing--------查路由表, 如果ping的目的在RT中没有,不发出任何包(arp也不会发出) 如果RT中存在,则arp 下一跳,相当于no ip routing+配置网关 n ...
- 高性能网站性能优化与系统架构(ZT)
转载请保留出处:俊麟 Michael’s blog (http://space.itpub.net/7311285/viewspace-97) 我在CERNET做过拨号接入平台的搭建,而后在Yahoo ...
- [CareerCup] 8.9 An In-memory File System 内存文件系统
8.9 Explain the data structures and algorithms that you would use to design an in-memory file system ...
- Fedora 12 环境搭建
又来折腾发行版了. 这一回是Fedora12,搞的挺艰难的 下载了Fedora-12-i386-DVD.iso,无论使用ultraiso还是dd都无法安装. 后来下载了一个ImageWriter.ex ...
- Material Design For Xamarin.Forms
最近,升级 Xamarin.Forms 到最新版本后,发现Droid 项目下引入了以下几个依赖包: Xamarin.Android.Support.DesignXamarin.Android.Supp ...
- jsp中常用操作字符串的el表达式
由于在JSP页面中显示数据时,经常需要对显示的字符串进行处理,SUN公司针对于一些常见处理定义了一套EL函数库供开发者使用. 准备工作:1)导入jar包:standard.jar和jstl.jar2) ...
- gradle构建android项目
工具: Android Studio2.0 gradle-2.10 一.Android常识 在做Android开发的时候我们首先必须要有一个SDK.一般SDK的主要作用就是将硬件和软件进行分离,做软件 ...