zoukankan      html  css  js  c++  java
  • Hdu 1007 最近点对

    题目链接

    Quoit Design

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


    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
     
    计算最小点对的思想是:分治
    注意:在下面代码中,如果使用宏定义来定义来替换取小函数min,则TLE。
    Accepted Code:
      1 /*************************************************************************
      2     > File Name: 1007.cpp
      3     > Author: Stomach_ache
      4     > Mail: sudaweitong@gmail.com
      5     > Created Time: 2014年06月10日 星期二 19时15分21秒
      6     > Propose: 
      7  ************************************************************************/
      8 
      9 #include <cmath>
     10 #include <string>
     11 #include <cstdio>
     12 #include <vector>
     13 #include <cstdlib>
     14 #include <fstream>
     15 #include <cstring>
     16 #include <iostream>
     17 #include <algorithm>
     18 using namespace std;
     19 
     20 #define INF (1000000000.0)
     21 #define MAX_N (100010)
     22 //#define X first
     23 //#define Y second
     24 //#define min(x, y) ((x) < (y) ? (x) : (y))
     25 //typedef pair<double, double> pii;
     26 struct pii {
     27       double X, Y;
     28 };
     29 
     30 int n;
     31 pii A[MAX_N], b[MAX_N];
     32 
     33 double
     34 min(double x, double y) {
     35       return x < y ? x : y;
     36 }
     37 
     38 bool
     39 compare_x(pii a, pii b) {
     40       return a.X < b.X;
     41 }
     42 
     43 bool
     44 compare_y(pii a, pii b) {
     45       return a.Y < b.Y;
     46 }
     47 
     48 double
     49 get_dist(pii a, pii b) {
     50       return sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y));
     51 }
     52 
     53 double
     54 closest_pair(int low, int high) {
     55       //if (high == low) return INF;
     56       if (high - low == 1) return get_dist(A[low], A[high]);
     57       if (high - low == 2) {
     58           double d = get_dist(A[low], A[low+1]);
     59         d = min(d, get_dist(A[low], A[high]));
     60         d = min(d, get_dist(A[low+1], A[high]));
     61         return d;
     62     }
     63     int m = (low + high) / 2;
     64     double x = A[m].X;
     65     double d = min(closest_pair(low, m), closest_pair(m+1, high)); // (1)
     66 
     67     int len = 0;
     68     for (int i = low; i <= high; i++) {
     69           if (fabs(A[i].X - x) < d) {
     70             b[len++] = A[i];
     71         }
     72     }
     73     sort(b, b + len, compare_y);
     74     for (int i = 0; i < len; i++) {
     75           for (int j = i+1; j < len; j++) {
     76             double dx = b[j].X  - b[i].X; 
     77             double dy = b[j].Y - b[i].Y;
     78             if (dy >= d) break; 
     79             d = min(d, sqrt(dx * dx + dy * dy));
     80         }
     81     }
     82 
     83     return d;
     84 }
     85 
     86 void
     87 solve() {
     88       sort(A, A + n, compare_x);
     89     printf("%.2f
    ", closest_pair(0, n - 1) * 0.5);
     90 }
     91 
     92 int
     93 main(void) {
     94       while (scanf("%d", &n) && n) {
     95           for (int i = 0; i < n; i++) 
     96               scanf("%lf %lf", &A[i].X, &A[i].Y);
     97         
     98         solve();
     99     }
    100 
    101     return 0;
    102 }
  • 相关阅读:
    三、Python基础(5)
    三、Python基础(4)
    三、Python基础(3)
    三、Python基础(2)
    mysql 索引的使用
    Java 泛型T
    vueJS
    集群 分布式 负载均衡的区别
    nodeJS vue elementUI mock学习过程笔记,仅用于个人记录巩固
    MAP
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3780974.html
Copyright © 2011-2022 走看看