POJ 3714 Raid

原创 2011年01月22日 16:51:00

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;
 
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 3714 Raid(平面最近点对,不同类型点之间)

Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7289   Accepted:...

poj 3714 Raid

Raid Time Limit: 5000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u ...
  • wwwzys
  • wwwzys
  • 2011-10-03 09:30
  • 1388

【解题报告】 POJ 3714 Raid -- 点对最小距离 + 分治法

最近点对_分治算法               O(nlgn)思路:对所有点先按x不减排序,             &...

POJ 3714 Raid(变种最近点对问题:分治+剪枝)

Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11728   Accepted...

POJ3714 Raid

最近点对

POJ 3714 Raid

题目大意: 题目链接 注释代码: 无注释代码: 单词解释:

【分治】【二分】POJ 3714 Raid

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

POJ 3714 Raid 最近点对

求最近点对,只不过这两个点需要属于不同的集合,那么就给两个集合的点分别标记一个id号,在计算时,两个集合合并起来,并排序,递归求解,只不过,求两点距离时,如果id号是同一集合的,直接返回一个很大的数就...

POJ 3714 Raid (最近点对)感觉数据怪怪的

传送门题目大意就是要你从两组点钟选出距离最小的两点,输出他们的距离。 一道经典最小点对问题+模板我看了晚上很多人的代码,这题交的时候感觉怪怪的 比如G++wa , C++ac这类情况我的代码也是用...

POJ--3714[Raid] 分治发求最小点对

题意:给你两个集合A,B(每个集合N个点),要求输出一个最小点对(两点分别在A,B两个集合内)。其实就是分治法求最小点对的模板题,只不过在取最短距离时加上不同集合的限制即可。 感想:但是郁闷的是,居然...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)