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. AutoIT简介

    AutoIT最初是为了帮助IT管理和维护而开发的工具,能自动完成软件的安装.由于自动化安装和自动化测试在功能需求上有许多相似之处,都要模拟用户对软件进行操作,并验证执行的结果,所以,AutoIT逐渐成 ...

  2. linux强制用户下线

    Linux系统为多用户多任务系统,因此允许多个用户登录到系统,有时候,我们需要强制某些用户下线. 前提:必须是root权限操作:(1)使用who查看目前有哪些用户登录了服务器,见下图 [root@vm ...

  3. 经典71道Android试题及答案

    本文为开发者奉献了70道经典Android面试题加答案--重要知识点几乎都涉及到了,你还等啥,赶紧收藏吧!! 1. 下列哪些语句关于内存回收的说明是正确的? (b) A. 程序员必须创建一个线程来释放 ...

  4. 注解与反射 ---Spring与Mybatis等框架的实现原理

    Java中各大框架,无论是AOP 还是 IoC 其基本实现思路都是源自Java 运行时支撑的反射功能, 而反射最基本的一点就是 任何一个类 其在JVM环境中,都有一个对象的Class对象,这个对象提供 ...

  5. 如何获取内联样式的width值

    如图,如何获取内联样式的width值 不用attr 用css这样写

  6. RDLC系列之七 条码打印

    参考: C# 条码标签打印程序,RDLC报表动态显示多条码标签的方法 http://www.cnblogs.com/vice/p/4105898.html 我做的思路是:不使用数据库存储image的b ...

  7. 在 WinForm 中使用 Direct2D

    在 C# 的 WinForm 应用中,界面的绘制使用的是 GDI+.不过在一些特别的应用中,可能需要用硬件加速来提高绘制的效率.下面就来介绍两种在 WinForm 应用中嵌入 Direct2D 的方法 ...

  8. Java系列:Add Microsoft SQL JDBC driver to Maven

    Maven does not directly support some libraries, like Microsoft's SQL Server JDBC. This tutorial will ...

  9. 在View页面,使用@if(){ }输出判断正确的内容

    @if (true) { Html.Raw("已结束"); } 发现这段代码是正确,但是页面不输出"已结束"三个字,但是也不报错 @if (DateTime.N ...

  10. iOS——关于打印控件

    20.UIPrintFormatterUIPrintFormatter时打印格式化的抽象基类:展示了传统的可打印的内容对象可以跨页边界.由于打印格式化,打印系统,可以自动打印与打印格式化的内容相关联的 ...