Raid
| Time Limit: 5000MS |
|
Memory Limit: 65536K |
| Total Submissions: 7289 |
|
Accepted: 2157 |
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 240 00 11 01 12 22 33 23 340 00 00 00 00 00 00 00 0 Sample Output 1.4140.000 Source POJ Founder Monthly Contest – 2008.12.28, Dagger |
這個題目是平面最近點對問題,不過稍微有點不一樣,這個題目在赤裸裸的最近點對上麵包裝了一層,開始拿到這個題目不知道怎麼下手
因為上午寫了一個最近點對的一個題目,所以就想用來套一下,但是感覺不怎麼好用,難道要重寫,重新定義結構體來重新遞迴分治寫?
我想多了!其實這個題目稍微的要動下腦經,思維還是要開闊一下,其實這個題目可以按照我我上一篇部落格寫的平面最近點對問題把dis求
距離的函數改寫一下,也就是如果是相同類型的點那麼就認為他們的距離是無窮大,這樣就避免了把兩種不同的點放在一起而無法區分的
尷尬了,表示這個想法開始沒想到,這個題目糾結了一會遲遲不想下手!
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>#include <math.h>#include <iostream>using namespace std;#define inf 1e8struct point{double x;double y;bool flag;}node[200100];int my_rec[200100];int n;double dis(point &a,point &b){if(a.flag == b.flag)return inf;return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));}int cmp(const point &a,const point &b){return a.x < b.x;}bool cmpy(const int &a,const int &b){return node[a].y < node[b].y;}double find_min(int l,int r){int i,j,k;double tmp;if(l==r)return inf;if(l==r-1)return dis(node[l],node[r]);double temp1=min(find_min(l,(l+r)/2),find_min((l+r)/2,r));int mid=(l+r)/2;k=0;for(i=l;i<=r;i++)if(fabs(node[i].x - node[mid].x) < temp1)my_rec[k++]=i;sort(my_rec,my_rec+k,cmpy);for(i=0;i<k-1;i++)for(j=i+1;j<k && j<i+7;j++){tmp=dis(node[my_rec[i]],node[my_rec[j]]);if(tmp < temp1)temp1=tmp;}return temp1;}int main(){int i,j,k;int t;scanf("%d",&t);while(t--){scanf("%d",&n);for(i=0;i<n;i++){scanf("%lf%lf",&node[i].x,&node[i].y);node[i].flag=1;}for(i=n,n*=2;i<n;i++){scanf("%lf%lf",&node[i].x,&node[i].y);node[i].flag=0;}if(n==2){printf("%.3lf\n",dis(node[0],node[1]));continue;}sort(node,node+n,cmp);//for(i=0;i<n;i++)//printf("%lf\n",node[i].x);printf("%.3lf\n",find_min(0,n-1));}return 0;}