zoukankan      html  css  js  c++  java
  • HDU 1007 Quoit Design(计算几何の最近点对)

    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.

    题目大意:输出最近点对的距离的一半

    思路:分治法,先按X轴排序。然后两边分治算最近距离,设为res,然后按y轴合并,只提取出离中间的点的x的距离不超过res的点,采用7点比较法。

    代码(1687MS):

     1 #include <cstring>
     2 #include <cstdio>
     3 #include <cmath>
     4 #include <algorithm>
     5 #include <vector>
     6 using namespace std;
     7 typedef long long LL;
     8 
     9 struct Point {
    10     double x, y;
    11     Point() {}
    12     Point(double x, double y): x(x), y(y) {}
    13     void read() {
    14         scanf("%lf%lf", &x, &y);
    15     }
    16     double length() {
    17         return sqrt(x * x + y * y);
    18     }
    19     Point operator - (const Point &rhs) const {
    20         return Point(x - rhs.x, y - rhs.y);
    21     }
    22 };
    23 
    24 double dist(const Point &a, const Point &b) {
    25     return (a - b).length();
    26 }
    27 
    28 bool x_cmp(const Point &a, const Point &b) {
    29     return a.x < b.x;
    30 }
    31 
    32 bool y_cmp(const Point &a, const Point &b) {
    33     return a.y < b.y;
    34 }
    35 
    36 const int MAXN = 100010;
    37 
    38 Point p[MAXN];
    39 double ans;
    40 int n;
    41 
    42 double closest_pair(Point *p, int n) {
    43     if(n <= 1) return 1e100;
    44     int mid = n / 2;
    45     double x = p[mid].x;
    46     double res = min(closest_pair(p, mid), closest_pair(p + mid, n - mid));
    47     inplace_merge(p, p + mid, p + n, y_cmp);
    48     vector<Point> q;
    49     for(int i = 0; i < n; ++i) {
    50         if(fabs(p[i].x - x) >= res) continue;
    51         for(vector<Point>::reverse_iterator it = q.rbegin(); it != q.rend(); ++it) {
    52             if(p[i].y - it->y >= res) break;
    53             res = min(res, dist(p[i], *it));
    54         }
    55         q.push_back(p[i]);
    56     }
    57     return res;
    58 }
    59 
    60 int main() {
    61     while(scanf("%d", &n) != EOF && n) {
    62         for(int i = 0; i < n; ++i) p[i].read();
    63         sort(p, p + n, x_cmp);
    64         printf("%.2f
    ", closest_pair(p, n) / 2);
    65     }
    66 }
    View Code
  • 相关阅读:
    toString()和toLocaleString()有什么区别
    解决浏览器跨域问题
    JavaScript排序,不只是冒泡
    input disabled的情况下在IOS设备中怎么修改颜色
    jquery api 中文文档
    javascript中var that=this的意思
    JS中(function(){xxx})(); 这种写法的意思
    http-equiv 的使用
    $(function(){......})的意思
    js中(function(){…})()立即执行函数写法理解
  • 原文地址:https://www.cnblogs.com/oyking/p/3198860.html
Copyright © 2011-2022 走看看