Raid
Time Limit: 5000MS |
|
Memory Limit: 65536K |
Total Submissions: 1944 |
|
Accepted: 623 |
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
题意:
给我们n个a点和n个b点,问我们ab之间最近的距离是多少
找了merge模版,先排序,再用merge递归求解
开始一直超时,然后把qsort( )换成sort( ),居然卡到1719MS
qsort( )和sort( )应该是差不多速度的,只不过sort( )在某些情况下还是占有优势的
OK!
MARK
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct point{
double x,y;
int rand;
}p[200020];//两种点存在一起,方便操作
int n;
double dis(point a,point b)
{
if(a.rand==b.rand)
return 100000000;//这里考虑了同种点比较的情况
double x=a.x-b.x;
double y=a.y-b.y;
return sqrt(x*x+y*y);
}
double MY_min(double a,double b)
{
return a<b?a:b;
}
bool cmpx(point p1,point p2)
{
if ((p1.x<p2.x)||(p1.x==p2.x&&p1.y<p2.y)) return true;
return false;
}
double mergepoint(int l,int r) //归并函数,模版
{
double resultl=0,resultr=0,minlen=10000000;
if(r>l+2)
{
int mid=(l+r)>>1;
double tempdis;
resultl=mergepoint(l,mid);
resultr=mergepoint(mid,r);
minlen=MY_min(resultl,resultr);
for(int i=mid;i>=l&&p[i].x>p[mid].x-minlen;i--)
{
for(int j=mid;j<=r&&p[j].x<p[mid].x+minlen;j++)
{
if(p[i].rand!=p[j].rand)
{
tempdis=dis(p[i],p[j]);
if(tempdis<minlen)
{
minlen=tempdis;
}
}
}
}
}
else
{
double distemp;
for(int i=l;i<r;i++)
{
for(int j=l+1;j<=r;j++)
{
if(j==i)
{
continue;
}
if(p[i].rand!=p[j].rand)
{
distemp=dis(p[i],p[j]);
minlen=minlen>distemp?distemp:minlen;
}
}
}
}
return minlen;
}
int main()
{
int t;
int i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
p[i].rand=0;
}
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&p[i+n].x,&p[i+n].y);
p[i].rand=1;
}
n*=2;
sort(p+1,p+1+n,cmpx);//数据比较弱,用x排序就行了
double results=mergepoint(1,n);
printf("%.3lf/n",results);
}
return 0;
}