Raid
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 10625   Accepted: 3192

Description

After successive failures in the battles against the Union, the Empire retreated to its last stronghold. Depending on its powerful defense system, the Empire repelled the six waves of Union's attack. After several sleepless nights of thinking, Arthur, General of the Union, noticed that the only weakness of the defense system was its energy supply. The system was charged by N nuclear power stations and breaking down any of them would disable the system.

The general soon started a raid to the stations by N special agents who were paradroped into the stronghold. Unfortunately they failed to land at the expected positions due to the attack by the Empire Air Force. As an experienced general, Arthur soon realized that he needed to rearrange the plan. The first thing he wants to know now is that which agent is the nearest to any power station. Could you, the chief officer, help the general to calculate the minimum distance between an agent and a station?

Input

The first line is a integer T representing the number of test cases.
Each test case begins with an integer N (1 ≤ N ≤ 100000).
The next N lines describe the positions of the stations. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the station.
The next following N lines describe the positions of the agents. Each line consists of two integers X (0 ≤ X ≤ 1000000000) and Y (0 ≤ Y ≤ 1000000000) indicating the positions of the agent.  

Output

For each test case output the minimum distance with precision of three decimal placed in a separate line.

Sample Input

2
4
0 0
0 1
1 0
1 1
2 2
2 3
3 2
3 3
4
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0

Sample Output

1.414
0.000

Source

______________________________

平面最近点对,先按照x排序。然后二分。合并两个子问题时,先取两个子问题的最小值x,然后把切分的位置左右的点找出来(在x以内的)。 
然后把这些点按照y排序。然后枚举。显然是单调的,如果超过就跳出(强力剪枝)。注意计算距离时,如果两个点在同一个子集里,返回无限大的值。

________________分析懒得写了,直接搬运隔壁Orion_Rigel的______________

WA了好多次,最后试了一下把读入的坐标从int改成double就AC了

之后试了试另外一种,两个集合分开算的写法,不幸WA。嘛,不管了。

先放AC的代码:

 #include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const double INF=;
const int mxn=;
struct node{
double x,y;//坐标也可能是实数
int f;
}a[mxn];
int pt[mxn];
double ans;
int n;
int cmpx(node a,node b){
return a.x<b.x;
}
int cmpy(int b,int c){
return a[b].y<a[c].y;
}
double dist(node a,node b){
if(a.f==b.f)return INF;//如果是同集合的点,返回INF
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double mindis(int l,int r){//求最小距离
if(l==r)return INF;
if(r-l==) return dist(a[r],a[l]);
double res=INF;
int mid=(l+r)>>;
res=mindis(l,mid);
res=min(res,mindis(mid+,r));//分治
int cnt=;
int i,j;
for(i=l;i<=r;++i)//剪枝,只从可能更优的点里找
if(fabs(a[i].x-a[mid].x)<=res)pt[cnt++]=i;
sort(pt,pt+cnt,cmpy);
double mind=INF;
for(i=;i<cnt;i++){
for(j=i+;j<cnt;j++){
if(fabs(a[pt[i]].y-a[pt[j]].y)>=res)break;
if((mind=dist(a[pt[i]],a[pt[j]]))<res)res=mind;
}
}
return res;
}
int main(){
int T;
scanf("%d",&T);
int i,j;
while(T--){
scanf("%d",&n);
int x,y;
for(i=;i<=n;i++)scanf("%lf%lf",&a[i].x,&a[i].y),a[i].f=;
int nn=n*;
for(i=n+;i<=nn;i++)scanf("%lf%lf",&a[i].x,&a[i].y),a[i].f=;
sort(a+,a+nn+,cmpx);
ans=mindis(,nn);
printf("%.3lf\n",ans);
}
return ;
}

下面是WA的算法,路过的大神求指点……

 #include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int mxn=;
struct node{
double x,y;
}s[mxn],a[mxn];
int cmpx(node a,node b){
return a.x<b.x;
}
int cmpy(node a,node b){
return a.y<b.y;
}
double mans;
int n;
int mxx=;
inline double dist(int x1,int y1,int x2,int y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void cl(int l,int r){
if(l>=r-)return;
int mid=(l+r)/;
cl(l,mid);
cl(mid,r);
if(r-l>mans)return;
int i,j;
int dx=mid-mans;
int dx2=mid+mans;
for(i=;i<=n;i++){
if(s[i].x<dx)continue;
if(s[i].x>mid)break;
for(j=;a[j].x<dx2 && j<=n;j++){
if(a[j].x<dx)continue;
if(a[j].y<s[i].y-mans || a[j].y>s[i].y+mans)continue;
mans=min(mans,dist(s[i].x,s[i].y,a[j].x,a[j].y));
}
}
return;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
int i,j;
for(i=;i<=n;i++){
scanf("%lf%lf",&s[i].x,&s[i].y);
}
for(i=;i<=n;i++){
scanf("%lf%lf",&a[i].x,&a[i].y);
}
sort(s+,s+n+,cmpx);
sort(a+,a+n+,cmpx);
mans=;
cl(,*n);
printf("%.3lf\n",mans);
}
}

POJ3714 Raid的更多相关文章

  1. poj3714 Raid(分治求平面最近点对)

    题目链接:https://vjudge.net/problem/POJ-3714 题意:给定两个点集,求最短距离. 思路:在平面最近点对基础上加了个条件,我么不访用f做标记,集合1的f为1,集合2的f ...

  2. POJ-3714 Raid 平面最近点对

    题目链接:http://poj.org/problem?id=3714 分治算法修改该为两个点集的情况就可以了,加一个标记... //STATUS:C++_AC_2094MS_4880KB #incl ...

  3. POJ3714 Raid 分治/K-D Tree

    VJ传送门 简要题意:给出两个大小均为\(N\)的点集\(A,B\),试在\(A\)中选择一个点,在\(B\)中选择一个点,使得它们在所有可能的选择方案中欧几里得距离最小,求出这个距离 下面给出的两种 ...

  4. 【poj3714】 Raid

    http://poj.org/problem?id=3714 (题目链接) 现在才搞平面最近点对..感觉有点尴尬 题意 给出平面上两组点,每组n个,求两组点之间最短距离 Solution1 平面最近点 ...

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

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

  6. $Poj3714/AcWing\ Raid$ 分治/平面最近点对

    $AcWing$ $Sol$ 平面最近点对板子题,注意要求的是两种不同的点之间的距离. $Code$ #include<bits/stdc++.h> #define il inline # ...

  7. 『Raid 平面最近点对』

    平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...

  8. 一张“神图”看懂单机/集群/热备/磁盘阵列(RAID)

    单机部署(stand-alone):只有一个饮水机提供服务,服务只部署一份 集群部署(cluster):有多个饮水机同时提供服务,服务冗余部署,每个冗余的服务都对外提供服务,一个服务挂掉时依然可用 热 ...

  9. 什么是RAID?RAID有什么用?RAID原理

    什么是RAID 硬盘是个很脆弱的东西,它经常会坏掉.所以,为了保证服务器可靠耐用,硬盘必须时时刻刻保持可用.所以有了RAID这个东西.它的目的是将好几个硬盘合并在一起,就算硬盘坏了一个,剩下还有好几个 ...

随机推荐

  1. C r and n(组合数)

    找出n个数的r个数的组合,如下形式: 输入:n,r分别为 5, 3 输出: 5    4     3 5    4     2 5    4     1 5    3     2 5    3     ...

  2. JS的Document属性和方法

    Attributes 存储节点的属性列表(只读)childNodes 存储节点的子节点列表(只读)dataType 返回此节点的数据类型Definition 以DTD或XML模式给出的节点的定义(只读 ...

  3. JavaScript---闭包和作用域链

    作用域和作用域链: 参考文章 :http://www.cnblogs.com/malinlin/p/6028842.html  http://www.cnblogs.com/lhb25/archive ...

  4. 谁可以说出HashMap和HashSet的相同点和不同点。

    谁可以说出HashMap和HashSet的相同点和不同点. 2011-11-15 20:46ruoshui_t | 浏览 20310 次  Perl 2011-11-15 21:17 #知道行家专业创 ...

  5. Mac 使用SSH远程登录

    一.打开ssh Mac Terminal是自带SSH的,可以用whereis来看看: $ whereis ssh 但是在现有进程中找不到ssh对应的进程: $ ps aux | grep ssh ap ...

  6. C++ 排序、查找的应用

    // order.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "string.h" #includ ...

  7. [vim]vim 在win下乱码解决

    vim在win下遇到汉字乱码早就知晓,本以为通过如下设置即可解决乱码问题 set encoding=utf-8 set fileencoding=utf-8,chinese 这样设置是可以解决源码文件 ...

  8. 《JavaScript高级程序设计》chapter 1: javascript 简介

    1.2.2 文档对象模型     DHTML的出现让开发人员无需重新加载页面就可以修改其外观了. 1.2.3 浏览器对象模型(BOM)     BOM真正与众不同的地方在于他作为javascript实 ...

  9. [MySQL] 按日期进行统计(前一天、本周、某一天)

    在mysql数据库中,常常会遇到统计当天的内容.例如,在user表中,日期字段为:log_time统计当天 sql语句为: select * from user where date(log_time ...

  10. ubuntu apt-get修改源地址

    亲测搜狐可用,其他备用 1.修改源地址:cp /etc/apt/sources.list /etc/apt/sources.list.bakvim /etc/apt/sources.list 修改之后 ...