zoukankan      html  css  js  c++  java
  • HDU1007(最近点对)

    Quoit Design

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


    Problem Description

    Have 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.
     

    Input

    The 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.
     

    Output

    For 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

    2 0 0 1 1 2 1 1 1 1 3 -1.5 0 0 0 0 1.5 0
     

    Sample Output

    0.71 0.00 0.75
     

    Author

    CHEN, Yue
     

    Source

     
     1 //2017-08-09
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 #include <cmath>
     7 #define mid ((l+r)>>1)
     8 
     9 using namespace std;
    10 
    11 const int N = 100010;
    12 struct Point{
    13     double x, y;
    14 }P[N], p1[N], p2[N];
    15 int n;
    16 
    17 bool cmp_x(Point a, Point b){
    18     return a.x < b.x;
    19 }
    20 
    21 bool cmp_y(Point a, Point b){
    22     return a.y < b.y;
    23 }
    24 
    25 double distance(Point *a, Point *b){
    26     return sqrt((a->x - b->x)*(a->x - b->x) + (a->y - b->y)*(a->y - b->y));
    27 }
    28 
    29 //分治,solve(l, r)表示区间[l, r]内的最近点对,solve(l, r) = min(solve(l, mid), solve(mid+1, r), 跨左右子区间的最近点对)
    30 double solve(int l, int r){
    31     if(l >= r)return 0;
    32     if(r - l == 1)return distance(&P[l], &P[r]);
    33     if(r - l == 2)return min(distance(&P[l], &P[l+1]), distance(&P[l+1], &P[r]));
    34     double ans = min(solve(l, mid), solve(mid+1, r));
    35     //暴力x坐标与mid的x坐标相差不超过当前最优解ans的点
    36     int m = 0;
    37     for(int i = l; i <= r; i++){
    38         if(fabs(P[mid].x - P[i].x) <= ans){
    39             p1[m++] = P[i];
    40         }
    41     }
    42     sort(p1, p1+m, cmp_y);
    43     for(int i = 0; i < m; i++){
    44         for(int j = i+1; j < m; j++){
    45             if(p1[j].y - p1[i].y > ans)break;
    46             ans = min(ans, distance(&p1[i], &p1[j]));
    47         }
    48     }
    49     return ans;
    50 }
    51 
    52 int main()
    53 {
    54     //freopen("dataIn.txt", "r", stdin);
    55     while(scanf("%d", &n)!=EOF && n){
    56         for(int i = 0; i < n; i++)
    57               scanf("%lf%lf", &P[i].x, &P[i].y);
    58         sort(P, P+n, cmp_x);
    59         printf("%.2lf
    ", solve(0, n-1)/2);
    60     }
    61 
    62     return 0;
    63 }
  • 相关阅读:
    Redis宣言
    软件工程
    分布式编程
    编程泛型
    tcp/ip高效编程总结
    IP协议详解
    gevent程序员指南
    网站架构
    这些话,是乔布斯给世间留下的真正伟大礼物
    Flink/Spark 如何实现动态更新作业配置
  • 原文地址:https://www.cnblogs.com/Penn000/p/7325701.html
Copyright © 2011-2022 走看看