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;
}

最新文章

  1. 【luogu】 P1880 石子合并
  2. JavaScript模板引擎artTemplate.js——两种方法实现性别的判定
  3. Angular.element和$document的使用方法分析,代替jquery
  4. mvc2 To 4
  5. sencha怎么在control层调用按钮
  6. 个性二维码开源专题&lt;后背景&gt;
  7. Linux Linux程序练习十二(select实现QQ群聊)
  8. javaScript call 函数的用法说明
  9. jQuery1.8以上,ajaxSend,ajaxStart等一系列事件要绑定在document上才有效果
  10. Knockoutjs官网翻译系列(一)
  11. android 点击水波纹效果
  12. Eclipse开发Android项目安装配置
  13. C语言入门(17)——C语言数组应用的一个实例
  14. Android 使用 array.xml
  15. 面向对象程序设计-C++ Operator Overloading &amp; Type conversion (Static)【第十一次上课笔记】
  16. Quick Tip: How to Add Syntax Highlighting to Any Project
  17. 2、自动化运维之SaltStack远程执行详解
  18. How do I copy files that need root access with scp
  19. [Swift]LeetCode638. 大礼包 | Shopping Offers
  20. 实现响应式——CSS变量

热门文章

  1. tomcat报404
  2. MySQL user表root用户误删除后恢复
  3. iOS9升级后第三方平台无法分享的问题
  4. Objective-C学习篇01—类的声明与实现
  5. Specified VM install not found: type Standard VM, name jdk1.6.0_05
  6. 用webclient.DownloadFile下载exe文件时大小为0
  7. 驱动读写进程内存R3,R0通信
  8. Pku1947 Rebuilding Roads
  9. 也谈 Python 的中文编码处理
  10. mapreduce (二) MapReduce实现倒排索引(一) combiner是把同一个机器上的多个map的结果先聚合一次