Quoit Design

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 30505    Accepted Submission(s): 8017

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

题意:给定n个点,求距离最短的两点的距离的一半。

题解:開始用暴力法。结果超时。然后换成分治就过了,分治的过程是先将每一个点的坐标读入到数组里,再将数组依照x坐标排序,然后分治找最小值。递归终止条件是仅仅剩两个元素或三个元素,可是若仅依照x排序终于结果不一定是最小值,由于有可能左边的元素与右边的元素构成最小值,所以须要再依据y值进行一次排序,此时数据规模已经相当小了。能够用暴力直接求解。

分治代码:

#include <stdio.h>
#include <math.h>
#include <algorithm>
#define maxn 100002
using std::sort; struct Node{
double x, y;
} arr[maxn], temp[maxn]; bool cmpx(Node a, Node b)
{
return a.x < b.x;
} bool cmpy(Node a, Node b)
{
return a.y < b.y;
} double calDist(int i, int j)
{
double x = arr[i].x - arr[j].x;
double y = arr[i].y - arr[j].y;
return sqrt(x * x + y * y);
} double divideAndConquer(int l, int r)
{
if(r - l == 1) return calDist(l, r);
else if(r - l == 2){
double a = calDist(l, l + 1);
double b = calDist(l + 1, r);
double c = calDist(l, r);
if(b > c) b = c;
return a < b ? a : b;
}
int mid = (l + r) >> 1, i, j, id = 0;
double a = divideAndConquer(l, mid);
double b = divideAndConquer(mid + 1, r);
double min = a < b ? a : b;
for(i = l; i <= r; ++i)
if(fabs(arr[i].x - arr[mid].x) < min) temp[id++] = arr[i];
sort(temp, temp + id, cmpy);
for(i = 0; i < id; ++i)
for(j = i + 1; j < id; ++j){
a = temp[j].y - temp[i].y;
if(a >= min) break;
b = temp[j].x - temp[i].x;
a = sqrt(a * a + b * b);
if(a < min) min = a;
}
return min;
} int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n, i, j;
double ans, x, y, len;
while(scanf("%d", &n), n){
for(i = 0; i < n; ++i)
scanf("%lf%lf", &arr[i].x, &arr[i].y);
sort(arr, arr + n, cmpx);
printf("%.2lf\n", divideAndConquer(0, n - 1) / 2);
}
return 0;
}

原TLE代码:

#include <stdio.h>
#include <math.h>
#define maxn 100002 struct Node{
double x, y;
} arr[maxn]; double cal(int i, int j)
{
double x = arr[i].x - arr[j].x;
double y = arr[i].y - arr[j].y;
return sqrt(x * x + y * y);
} int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
int n, i, j;
double ans, x, y, len;
while(scanf("%d", &n), n){
for(i = 0, ans = -1; i < n; ++i){
scanf("%lf%lf", &arr[i].x, &arr[i].y);
for(j = 0; j < i; ++j){
len = cal(i, j);
if(len < ans || ans < 0) ans = len;
}
}
printf("%.2lf\n", ans / 2);
}
return 0;
}

HDU1007 Quoit Design 【分治】的更多相关文章

  1. HDU1007 Quoit Design掷环游戏

    Quoit Design 看懂题意以后发现就是找平面最近点对间距离除以2. 平面上最近点对是经典的分治,我的解析 直接上代码 #include<bits/stdc++.h> using n ...

  2. hdu 1007 Quoit Design 分治求最近点对

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. (hdu1007)Quoit Design,求最近点对

    Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...

  4. HDU-1007 Quoit Design 平面最近点对

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 简单裸题,测测模板,G++速度快了不少,应该是编译的时候对比C++优化了不少.. //STATU ...

  5. HDU1007.Quoit Design

    -- 点我 -- 题目大意 :给你一堆点,求一个最小圆能够覆盖两个点的半径(最近两点距离的一半): 最多100000个点,暴力即O(n^2)会超时,考虑二分,先求左边最短距离dl,右边dr, 和一个点 ...

  6. Quoit Design(最近点对+分治)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1007 Quoit Design Time Limit: 10000/5000 MS (Java/Oth ...

  7. HDU 1007 Quoit Design【计算几何/分治/最近点对】

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. Quoit Design(hdu1007)

    ---恢复内容开始--- Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  9. ACM-计算几何之Quoit Design——hdu1007 zoj2107

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. Swift - 07 - 布尔类型

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  2. 你好,C++(7)第三部分 C++世界众生相 3.2.1 变量的定义与初始化

    第3部分 C++世界众生相 在听过了HelloWorld.exe的自我介绍,完成了与C++世界的第一次亲密接触后,大家是不是都急不可待地想要一试身手,开始编写C++程序了呢?程序的两大任务是描述数据和 ...

  3. dota监测

    漫漫长假一个人无聊得很,整日DOTA,打的腰酸背痛腿抽筋的.就想着写一个脚本记录自己每天打游戏的时间,于是就产生了下面的这个东西... 运行环境:win7 32位. python版本:3.4.1 由于 ...

  4. php中json_decode()和json_encode()

    1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...

  5. phalcon在phpstorm里的配置视频

    phalcon在phpstorm里的配置视频:http://www.tudou.com/programs/view/yXw6e_Rshwk/

  6. WEB编码事项

    标准 WEB开发标准是一系列标准的集合, 包含HTML结构标准.CSS表现标准.JS行为标准.代码标准.标准测试. 目标 WEB开发流程统一标准化,实现页面结构.表现.行为适当分离,提高页面易维护性, ...

  7. 黑马程序员—C语言的判断语句

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 一.分支结构 结构化程序设计(英语:Structured programming),一种编程范型 ...

  8. ZOJ1074 (最大和子矩阵 DP)

    F - 最大子矩阵和 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u   Descri ...

  9. Codeforces 556A Case of the Zeros and Ones(消除01)

    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u   Description Andr ...

  10. bzoj4160: [Neerc2009]Exclusive Access 2

    Description 给出 N 个点M 条边的无向图,定向得到有向无环图,使得最长路最短. N ≤ 15, M ≤ 100 Input 第一行一个数M (1≤M≤100). 接下来M行,每行两个大写 ...