(hdu 7.1.8)Quoit Design(最小點對——在n個點中,找到兩個點間的最小距離),hduquoit

來源:互聯網
上載者:User

(hdu 7.1.8)Quoit Design(最小點對——在n個點中,找到兩個點間的最小距離),hduquoit

題目:

Quoit Design
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 136 Accepted Submission(s): 77
 
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


題目分析:

               最小點對問題。所謂的最小點對問題就是,在n個點中找到2個點間的最短距離。這種題有兩種思路:

1)直接暴力。看一看資料規模,n都在100000左右了,O(n^2)的演算法,不出意外,會TLE。

2)分治。這道題用的是吉林大學的模板。直接套進去就行了。


與最小點對問題對應的是最大點對問題(不知道有沒有這個名詞,如果沒有就當是我瞎編的吧。所謂的最大點對問題,在我的定義裡就是,在n個點中找到兩個點之間的最大距離)。能夠產生最大距離的這兩個點一定在凸包上,這時候我們只要枚舉凸包上的任意兩個點即可。其實這時候除了盲目枚舉外,還有一種更好的演算法來解決這個問題——旋轉卡殼演算法。




代碼如下:

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;/** * 求n個點中,2個點之間的最短距離。 * 1)直接暴力。肯定會TLE * 2)使用吉林大學的模板 */const int N = 100005;const double MAX = 10e100, eps = 0.00001;struct Point {double x, y;int index;};Point a[N], b[N], c[N];double closest(Point *, Point *, Point *, int, int);double dis(Point, Point);int cmp_x(const void *, const void*);int cmp_y(const void *, const void*);int merge(Point *, Point *, int, int, int);inline double min(double, double);int main(){int n;while(scanf("%d",&n)!=EOF,n){int i;for(i = 0 ; i < n ; ++i){scanf("%lf %lf",&a[i].x,&a[i].y);}qsort(a,n,sizeof(a[0]),cmp_x);for(i = 0 ; i < n ; ++i){a[i].index = i;}/** * memcpy(目標地址,起始地址,n個位元組) * 作用:從起始地址拷貝n個位元組到目標地址 * 標頭檔: 盡量把 <cstring>引入 * */memcpy(b,a,n*sizeof(a[0]));qsort(b,n,sizeof(b[0]),cmp_y);double ans = closest(a,b,c,0,n-1);printf("%.2lf\n",ans/2);}return 0;}double closest(Point a[], Point b[], Point c[], int p, int q) {if (q - p == 1){return dis(a[p], a[q]);}if (q - p == 2) {double x1 = dis(a[p], a[q]);double x2 = dis(a[p + 1], a[q]);double x3 = dis(a[p], a[p + 1]);if (x1 < x2 && x1 < x3){return x1;}else if (x2 < x3){return x2;}else{return x3;}}int i, j, k, m = (p + q) / 2;double d1, d2;for (i = p, j = p, k = m + 1; i <= q; i++){if (b[i].index <= m){c[j++] = b[i];}// 數組c 左半部儲存劃分後左部的點,  且對y 是有序的.else{c[k++] = b[i];}}d1 = closest(a, c, b, p, m);d2 = closest(a, c, b, m + 1, q);double dm = min(d1, d2);// 數組c 左右部分分別是對y 座標有序的,  將其合并到b.merge(b, c, p, m, q);for (i = p, k = p; i <= q; i++){if (fabs(b[i].x - b[m].x) < dm){c[k++] = b[i];}}// 找出離劃分基準左右不超過dm 的部分,  且仍然對y 座標有序.for (i = p; i < k; i++){for (j = i + 1; j < k && c[j].y - c[i].y < dm; j++) {double temp = dis(c[i], c[j]);if (temp < dm){dm = temp;}}}return dm;}double dis(Point p, Point q) {double x1 = p.x - q.x, y1 = p.y - q.y;return sqrt(x1 * x1 + y1 * y1);}int merge(Point p[], Point q[], int s, int m, int t) {int i, j, k;for (i = s, j = m + 1, k = s; i <= m && j <= t;) {if (q[i].y > q[j].y){p[k++] = q[j], j++;}else{p[k++] = q[i], i++;}}while (i <= m){p[k++] = q[i++];}while (j <= t){p[k++] = q[j++];}memcpy(q + s, p + s, (t - s + 1) * sizeof(p[0]));return 0;}int cmp_x(const void *p, const void *q) {double temp = ((Point*) p)->x - ((Point*) q)->x;if (temp > 0){return 1;}else if (fabs(temp) < eps){return 0;}else{return -1;}}int cmp_y(const void *p, const void *q) {double temp = ((Point*) p)->y - ((Point*) q)->y;if (temp > 0){return 1;}else if (fabs(temp) < eps){return 0;}else{return -1;}}inline double min(double p, double q) {return (p > q) ? (q) : (p);}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.