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

POJ3714 最近点对

变形了的最近点对,关键在于计算距离的时候,如果同类点的话,直接判定为无穷大即可。 其他闲话: (1)因为一些原因,被迫暂时用回C++. (2)好久没刷题,忘记了数组一开始要开最大,多次new和dele...
  • qiul12345
  • qiul12345
  • 2014年05月07日 09:48
  • 1035

poj 3714 Raid(最近点对)

Raid Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7884   Accepted:...
  • u010527492
  • u010527492
  • 2013年12月16日 13:08
  • 482

POJ 3714 Raid(最近点对)

DescriptionAfter successive failures in the battles against the Union, the Empire retreated to its l...
  • feizaoSYUACM
  • feizaoSYUACM
  • 2017年05月03日 20:56
  • 247

【分治】【二分】POJ 3714 Raid

After successive failures in the battles against the Union, the Empire retreated to its last strongh...
  • C20190102
  • C20190102
  • 2017年07月15日 16:47
  • 261

ACM-计算几何之Raid——poj3714

ACM 计算几何 Raid poj3714 求最近点对
  • lx417147512
  • lx417147512
  • 2014年05月06日 21:24
  • 2384

POJ 3714 Raid(平面最近点对)

解题思路: 分治法求平面最近点对,点分成两部分,加个标记就好了。 #include #include #include #include #include #include #incl...
  • u013967323
  • u013967323
  • 2015年07月19日 22:52
  • 583

poj 3714 Raid(平面最近点对)

给出两个点集,然后求两个点集之间的最近距离。 思路:开始用的旋转卡壳,两个点集先求凸包,这样就变成了两个凸包间最近距离,但是死活TLE,然后就换了平面最近点对来做,把两个点集标记一下,判断下是不是在...
  • crazy852456
  • crazy852456
  • 2013年11月02日 12:34
  • 456

POJ 3714 Raid 最近对点题解

本题是一般最近对点求解,稍微增加点限定:有两个集合点,要求不同集合中的点的最近对。 那么就增加一个判断,如果是同一个集合中的点,那么就返回最大值,其他和一般的最近对点解法一样。 注意:本题数据有重...
  • kenden23
  • kenden23
  • 2014年07月02日 12:50
  • 777

POJ 3714 Raid 平面最近点对

题目大意:给出两个集合的点,问这两个集合之间最近的点对的距离是多少。 思路:先要知道平面最近点对的分治算法,剩下的就简单了,只需要在更新答案的时候判断一下两个点是否属于两个集合就可以了。 ...
  • jiangyuze831
  • jiangyuze831
  • 2015年01月13日 16:27
  • 735

POJ 3714 Raid 最近点对 扫描线

题意:  有N个士兵,N个炮台。给出这2N个点的坐标,求士兵距离炮台的最近距离。 思路:前面用分治法解决了这个问题,但是这个问题还能通过扫描线的方式进行乱搞,但是中间检查的范围是和分治法一样的。 ...
  • u012139398
  • u012139398
  • 2015年01月25日 23:18
  • 447
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 3714 Raid
举报原因:
原因补充:

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