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. 控制器View的加载过程

    1.控制器内部的view是延迟加载 1> 用到时再加载2> 加载完毕后会调用控制器的viewDidLoad方法 2.创建控制器的方式 1> 直接通过代码创建OneViewContro ...

  2. Sql Server使用技巧

    1.修改表的字段时,提示不能更改: 工具>选项>设计器>取消 阻止保存要求重新创建表的更改 2.更改选择多少行,编辑多少行: 工具>选项>Sql Server对象资源管理 ...

  3. nodejs概论(实操篇)

    什么是模块? 模块分为原生模块(node.jsAPI提供的原生模块,在启动时已经被加载)和 文件模块(动态加载模块,主要由原生模块module来实现和完成.通过调 用node.js的require方法 ...

  4. 数据库备份工具mysqldump重要参数详解

    1. --single-transaction InnoDB 表在备份时,通常启用选项 --single-transaction 来保证备份的一致性,实际上它的工作原理是设定本次会话的隔离级别为:RE ...

  5. PHP应用程序的安全性

    无论在开发中,还是在面试时或者技术讨论时,安全性都是需要深入了解及掌握的. 目标 本教程目标是使您了解应该如何保护自己构建的 Web 应用程序.讲解如何防御最常见的安全威胁:SQL 注入.操纵 GET ...

  6. OOCSS学习(一)

    OOCSS —— 面向对象CSS 搜集一些该搜集的,然后汇总一下. 1.OOCSS 概念篇: 1)什么是面向对象 确定“对象”,并给这个对象创建CSS样式规则. 2)面向对象的CSS理论 OOCSS最 ...

  7. python序列化之pickle

    来自引用: 1.什么东西能用pickle模块存储? 所有Python支持的 原生类型 : 布尔, 整数, 浮点数, 复数, 字符串, bytes(字节串)对象, 字节数组, 以及 None. 由任何原 ...

  8. Boost使用笔记(Smart_ptr)

    我是Word写的,复制过来实在懒得在排版了,有兴趣的朋友可以去我的百度文库看,谢谢 http://wenku.baidu.com/view/34e485e2f61fb7360b4c653e.html ...

  9. Linux文本操作三大利器总结:sed、awk、grep

    grep:(去除一行中需要的信息,同类与cut) grep全称是Global Regular Expression Print #常规用法 # grep -n root /etc/passwd :ro ...

  10. 也谈 Python 的中文编码处理

    最近业务中需要用 Python 写一些脚本.尽管脚本的交互只是命令行 + 日志输出,但是为了让界面友好些,我还是决定用中文输出日志信息. 很快,我就遇到了异常: UnicodeEncodeError: ...