[Time Limit Exceeded]Quoit Design

Posted ixummer的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Time Limit Exceeded]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. 
 
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
 
运行超时未AC,下面是代码:
 1 import java.text.DecimalFormat;
 2 import java.util.Arrays;
 3 import java.util.Comparator;
 4 import java.util.Scanner;
 5 
 6 
 7 class Point {   //定义一个点的类
 8     double x;
 9     double y;
10 
11     public Point(double x, double y) {
12         this.x = x;
13         this.y = y;
14     }
15 }
16 
17 class MyComprator implements Comparator<Point> {   
18     public int compare(Point t1, Point t2) {
19         double a = t1.x;
20         double b = t2.x;
21         if (a == b)
22             return t1.y > t2.y ? 1 : -1;
23         else
24             return t1.x > t2.x ? 1 : -1;
25     }
26 }
27 public class Main {
28     public static int max = 1000001;
29     public static Point[] ivec = new Point[max];
30     public static Point[] tvec = new Point[max];
31     public static Point[] rvec = new Point[max];
32 
33     private static double getDist(Point a, Point b) {
34         return Math.sqrt(Math.pow(a.x - b.x, 2) + Math.pow(a.y - b.y, 2));
35     }
36 
37     private static double getMin(int start, int end) {
38         if (start == end)
39             return 0;
40         else if (start + 1 == end)
41             return getDist(ivec[start], ivec[end]);
42         else if (start + 2 == end) {    //3个点
43             double d1 = getDist(ivec[start], ivec[end]);
44             double d2 = getDist(ivec[start], ivec[start + 1]);
45             double d3 = getDist(ivec[start + 1], ivec[end]);
46             return min(d1, min(d2, d3));
47         } else {
48             int mid = (start + end) / 2;
49             double d = min(getDist(ivec[start], ivec[mid]), getDist(ivec[mid], ivec[end]));
50             int i, k = 0;
51             for (i = start, k = 0; i <= end; i++) {
52                 if (Math.abs(ivec[i].x - ivec[mid].x) < d) {
53                     tvec[k++] = ivec[i];
54                 }
55             }
56             int len = k;
57             mid = k / 2;
58             for (i = 0, k = 0; i < len; i++) {
59                 if (Math.abs(tvec[i].y - tvec[mid].y) < d)
60                     rvec[k++] = tvec[i];
61             }
62             len = k;
63             for (int i1 = 0; i1 < len - 1; i1++)
64                 for (int j = i1 + 1; j < len; j++)
65                     d = min(d, getDist(rvec[i1], rvec[j]));
66             return d;
67         }
68     }
69 
70     private static double min(double a, double b) {
71         return a < b ? a : b;
72     }
73 
74     public static void main(String args[]) {
75         Scanner sc = new Scanner(System.in);
76         int n;
77         while ((n = sc.nextInt()) != 0) {
78             int start = 0;
79             int end = n - 1;
80             int i;
81             for (i = 0; i < n; i++) {
82                 ivec[i] = new Point(sc.nextDouble(), sc.nextDouble());
83             }
84             Arrays.sort(ivec, 0, n - 1, new MyComprator());
85             double result = getMin(start, end) / 2;
86             System.out.println(new DecimalFormat("0.00").format(result));
87         }
88     }
89 }

 

以上是关于[Time Limit Exceeded]Quoit Design的主要内容,如果未能解决你的问题,请参考以下文章

Time Limit Exceeded的原因及避免方法

C语言编程Time Limit Exceeded问题

SPOJ Time Limit Exceeded(高维前缀和)

python3OJ 1001 487-3279 Time Limit Exceeded

spark 读取mongodb失败,报executor time out 和GC overhead limit exceeded 异常

30秒运行超时的错误(Maximum execution time of 30 seconds exceeded)