HDU 1007 Quoit Design(計算幾何 平面最近點對)

來源:互聯網
上載者:User

Quoit Design

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22349    Accepted Submission(s): 5735


Problem DescriptionHave you ever played quoit in a playground? Quoit is a game in which flat rings are pitched at some toys, with all the toys encircled awarded.
In the field of Cyberground, the position of each toy is fixed, and the ring is carefully designed so it can only encircle one toy at a time. On the other hand, to make the game look more attractive, the ring is designed to have the largest radius. Given a
configuration of the field, you are supposed to find the radius of such a ring.

Assume that all the toys are points on a plane. A point is encircled by the ring if the distance between the point and the center of the ring is strictly less than the radius of the ring. If two toys are placed at the same point, the radius of the ring is considered
to be 0. 


InputThe input consists of several test cases. For each case, the first line contains an integer N (2 <= N <= 100,000), the total number of toys in the field. Then N lines follow, each contains a pair of (x, y) which are the coordinates of a toy. The input is terminated
by N = 0. 


OutputFor each test case, print in one line the radius of the ring required by the Cyberground manager, accurate up to 2 decimal places.  


Sample Input

20 01 121 11 13-1.5 00 00 1.50
 


Sample Output

0.710.000.75
 


AuthorCHEN, Yue 


SourceZJCPC2004 


RecommendJGShining 對於這道題目我首先抱怨一下,逾時超的我想哭,原因不是因為演算法思路錯了,是一開是在對點按照X值從左向右排序的時候用的是qsort,後來改成sort就A了,傷心,等會要好好研究sort和qsort的區別 好了,還是介紹一下最近點對的演算法思想吧!總體思路是遞迴與分治思想,首先按照點的X值從左至右排序下,然後分成兩半,依次求左邊和右邊的最近點對然後取最下但是這裡問題出現了,如果最近的兩個點同時都在分開的兩邊,那麼就簡單了,但是如果這兩個點分在不同的地區,這下就麻煩了,這個問題的解決是這個演算法正確性和複雜度度關鍵位置首先在L和R區間內找到可能是最小點對中的所有點,然後再進行處理我們這樣先在X軸方向全部合格,然後再按照Y軸大小排序,然後開始選擇最小的在按照X軸選擇的時候這樣,所有到中間位置的X軸距離小於當前在左邊和右邊找到最小點對的距離的點然後按照Y軸從小到大排序,然後這裡有個鴿巢原理,其實很簡單:(轉)d是兩個半平面各自內,任意兩點的最小距離,因此在同一個半平面內,任何兩點距離都不可能超過d。   我們又要求P1和P2的水平距離不能超過d,垂直距離也不能超過d,在這個d*2d的小方塊內,最多隻能放下6個距離不小於d的點。   然後就沒了!

#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;}node[100100];int my_rec[100100];int n;double dis(point &a,point &b){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 /*node[my_rec[j]].y-node[my_rec[i]].y < temp1*/;j++)//這裡利用上面推論只掃描六個點{tmp=dis(node[my_rec[i]],node[my_rec[j]]);if(tmp < temp1)temp1=tmp;}return temp1;}int main(){int i,j,k;while(scanf("%d",&n),n){for(i=0;i<n;i++)scanf("%lf%lf",&node[i].x,&node[i].y);if(n==1){printf("0.00\n");continue;}if(n==2){printf("%.2lf\n",dis(node[0],node[1])/2);continue;}sort(node,node+n,cmp);printf("%.2lf\n",find_min(0,n-1)/2);}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.