POJ 3714 Raid
2016-05-15 21:48
309 查看
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
Sample Output
在模版里的dis函数中特别地把相同集合的点距设为INF
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 200060;
// 分治算法求最近点对
struct point//保存每一个点
{
double x,y;
int mm;
} p[maxn];
int a[maxn];//保存筛选的坐标点的索引即2min(dl,dh)区间的坐标点索引
int cmpx(point a,point b)//对n个点按横坐标由小到大排序
{
return a.x<b.x;
}
int cmpy(int a,int b)//(这里用的是下标索引)对筛选的点按纵坐标由小到大排序
{
return p[a].y<p[b].y;
}
inline double dis(point a,point b)//求点对间的距离
{
if(a.mm==b.mm) return INF;
else
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double closest(int low,int high)//求最近点对
{
int i,j,k;
if(low+1==high)//只有两个点
return dis(p[low],p[high]);
if(low+2==high)//只有三个点
return min(dis(p[low],p[high]),min(dis(p[low],p[low+1]),dis(p[low+1],p[high])));
int mid=(low+high)>>1;//求中点即左右子集的分界线
double d=min(closest(low,mid),closest(mid+1,high));
for(i=low,k=0; i<=high; i++) //把x坐标在p[mid].x-d ~ p[mid].x+d范围内的点筛选出来
{
if(p[i].x >=p[mid].x-d&&p[i].x<=p[mid].x+d)
a[k++]=i;//保存这些点的下标索引
}
sort(a,a+k,cmpy);//按y坐标进行升序排序
for(i=0; i<k; i++)
{
for(j=i+1; j<k; j++)
{
if(p[a[j]].y-p[a[i]].y>=d)//注意下标索引
break;
d=min(d,dis(p[a[i]],p[a[j]]));
}
}
return d;
}
int main()
{
int i,n,T;
double MIN,a,b;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);//n个点
MIN=INF;
for(i = 0 ; i < n ; ++i)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
p[i].mm=0;
}
for(i = n ; i < n*2 ; ++i)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
p[i].mm=1;
}
sort(p , p + n*2 , cmpx);
printf("%.3f\n",closest(0 , n*2-1 ));
}
return 0;
}
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
在模版里的dis函数中特别地把相同集合的点距设为INF
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn = 200060;
// 分治算法求最近点对
struct point//保存每一个点
{
double x,y;
int mm;
} p[maxn];
int a[maxn];//保存筛选的坐标点的索引即2min(dl,dh)区间的坐标点索引
int cmpx(point a,point b)//对n个点按横坐标由小到大排序
{
return a.x<b.x;
}
int cmpy(int a,int b)//(这里用的是下标索引)对筛选的点按纵坐标由小到大排序
{
return p[a].y<p[b].y;
}
inline double dis(point a,point b)//求点对间的距离
{
if(a.mm==b.mm) return INF;
else
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double closest(int low,int high)//求最近点对
{
int i,j,k;
if(low+1==high)//只有两个点
return dis(p[low],p[high]);
if(low+2==high)//只有三个点
return min(dis(p[low],p[high]),min(dis(p[low],p[low+1]),dis(p[low+1],p[high])));
int mid=(low+high)>>1;//求中点即左右子集的分界线
double d=min(closest(low,mid),closest(mid+1,high));
for(i=low,k=0; i<=high; i++) //把x坐标在p[mid].x-d ~ p[mid].x+d范围内的点筛选出来
{
if(p[i].x >=p[mid].x-d&&p[i].x<=p[mid].x+d)
a[k++]=i;//保存这些点的下标索引
}
sort(a,a+k,cmpy);//按y坐标进行升序排序
for(i=0; i<k; i++)
{
for(j=i+1; j<k; j++)
{
if(p[a[j]].y-p[a[i]].y>=d)//注意下标索引
break;
d=min(d,dis(p[a[i]],p[a[j]]));
}
}
return d;
}
int main()
{
int i,n,T;
double MIN,a,b;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);//n个点
MIN=INF;
for(i = 0 ; i < n ; ++i)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
p[i].mm=0;
}
for(i = n ; i < n*2 ; ++i)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
p[i].mm=1;
}
sort(p , p + n*2 , cmpx);
printf("%.3f\n",closest(0 , n*2-1 ));
}
return 0;
}
相关文章推荐
- http://blog.csdn.net/rosten/article/details/17068285
- RHEL7磁盘加密和RAID0创建
- mha 自动failover 原创
- UVA 10245 The Closest Pair Problem
- HDU 3268 Columbus’s bargain(最短路)
- 服务器有大量的time_wait进程怎么办?
- HDU 3264 Open-air shopping malls(二分查找)
- Faster RCNN train.py
- Faster RCNN train_faster_rcnn_alt_opt.py
- Gmail附件大小及格式限制全面解析
- AIDL
- 51Nod-1305-Pairwise Sum and Divide
- 打印一个楼梯(Staircase)
- Git学习 -- 远程操作遇到error: failed to push some refs to问题解决方法
- A failure of one rush
- Synchronized关键字与线程通信
- 人工智能假如失控 将会带来何种危害程度?
- URAL 1890 . Money out of Thin Air (dfs序hash + 线段树)
- 列车调度(Train)
- svn服务器搭建和使用:Custom action GenerateSSLKey failed: Command terminated with non-zero exit code