POJ 3714 Raid

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


0 0
0 1
1 0
1 1
2 2
2 3
3 2
3 3

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

좋은 웹페이지 즐겨찾기