POJ3714 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的更多相关文章
- poj3714 Raid(分治求平面最近点对)
题目链接:https://vjudge.net/problem/POJ-3714 题意:给定两个点集,求最短距离. 思路:在平面最近点对基础上加了个条件,我么不访用f做标记,集合1的f为1,集合2的f ...
- POJ-3714 Raid 平面最近点对
题目链接:http://poj.org/problem?id=3714 分治算法修改该为两个点集的情况就可以了,加一个标记... //STATUS:C++_AC_2094MS_4880KB #incl ...
- POJ3714 Raid 分治/K-D Tree
VJ传送门 简要题意:给出两个大小均为\(N\)的点集\(A,B\),试在\(A\)中选择一个点,在\(B\)中选择一个点,使得它们在所有可能的选择方案中欧几里得距离最小,求出这个距离 下面给出的两种 ...
- 【poj3714】 Raid
http://poj.org/problem?id=3714 (题目链接) 现在才搞平面最近点对..感觉有点尴尬 题意 给出平面上两组点,每组n个,求两组点之间最短距离 Solution1 平面最近点 ...
- 【POJ3714】Raid:平面最近点对
Description After successive failures in the battles against the Union, the Empire retreated to its ...
- $Poj3714/AcWing\ Raid$ 分治/平面最近点对
$AcWing$ $Sol$ 平面最近点对板子题,注意要求的是两种不同的点之间的距离. $Code$ #include<bits/stdc++.h> #define il inline # ...
- 『Raid 平面最近点对』
平面最近点对 平面最近点对算是一个经典的问题了,虽然谈不上是什么专门的算法,但是拿出问题模型好好分析一个是有必要的. 给定\(n\)个二元组\((x,y)\),代表同一平面内的\(n\)个点的坐标,求 ...
- 一张“神图”看懂单机/集群/热备/磁盘阵列(RAID)
单机部署(stand-alone):只有一个饮水机提供服务,服务只部署一份 集群部署(cluster):有多个饮水机同时提供服务,服务冗余部署,每个冗余的服务都对外提供服务,一个服务挂掉时依然可用 热 ...
- 什么是RAID?RAID有什么用?RAID原理
什么是RAID 硬盘是个很脆弱的东西,它经常会坏掉.所以,为了保证服务器可靠耐用,硬盘必须时时刻刻保持可用.所以有了RAID这个东西.它的目的是将好几个硬盘合并在一起,就算硬盘坏了一个,剩下还有好几个 ...
随机推荐
- Linux由管道组成的值得学习的命令
1.Linux查找不以#开头的行的命令如:cat /etc/vsftpd/vsftpd.conf | grep -v "#"
- request模块提交数据
http://ctf8.shiyanbar.com/jia/ #coding:utf-8import re,requestsurl = r"http://ctf8.shiyanbar.com ...
- angularjs: ng-select和ng-options
angular.js有一个很强大的指令: ng-select 它可以帮助你通过数据模型来创建select元素.它很好的支持了select标签的语法,但是却有点坑. 假设有如下一段json数据: { & ...
- WPF学习笔记:MVVM模式下,ViewModel如何关闭View?
原文:http://blog.csdn.net/leftfist/article/details/32349731 矫枉过正,从一个极端走向另一个极端.MVVM模式,View只负责呈现,虽然也有后台代 ...
- javascript限制上传文件大小
在FireFox.Chrome浏览器中可以根据document.getElementById(“id_file”).files[0].size 获取上传文件的大小(字节数)使用的 api是FileRe ...
- [TCPIP]代理arp
一 理论概述 \ 二 实验 实验一:代理arp在nat中的作用(实验发现一下是错的) 实验二.代理arp pc访问服务器想让走路由器(写32bit静态路由),右边的R arp server的时 ...
- EBS 用户及其联系人的失效时间
联系人失效时间还有一个SQL,从页面的联系人详情简化取得的,不如直接用pos_supplier_users_v视图 SELECT * FROM (SELECT NULL contact_req_id, ...
- 用python简单处理图片(5):图像直方图
我们先来看两个函数reshape和flatten: 假设我们先生成一个一维数组: vec=np.arange(15) print vec 显示为: [ 0 1 2 3 4 5 6 7 8 9 10 1 ...
- Android tab导航的几种方法:ActionBar tab +fragment,Viewpager+pagerTitleStrip,开源框架ViewPageIndicator 和 ViewPager
action来实现tab标签 并跟fragment结合 因为要写新闻客户端这个tab导航是必须的 这里我写几个小练习,希望大家融会贯通. 1actionbar设置tab +fragment 布局是个l ...
- Matlab txt内容替换函数 fgetl fseek
Data Import and Export :Low-Level File I/O the contents of the file: 16 5 9 4 2 ...