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 descriptionhave 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.
Inputthe 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.
Outputfor 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
20 01 121 11 13-1.5 00 00 1.50
Sample output
0.710.000.75
Given N points, calculate half of the distance between the two points with the shortest distance.
Solution: start to use the force, the result times out, and then change to sub-governance. The sub-governance process is to first read the coordinates of each point into the array, and then sort the array by X coordinate, then, divide the Sub-rule to find the minimum value. The recursive termination condition is that there are only two or three elements left. However, if the final result is only sorted by X, it is not necessarily the minimum value, because it is possible that the elements on the left and the elements on the right constitute the minimum value, you need to sort them again based on the Y value. At this time, the data size is already quite small and can be solved using brute force.
Rule separation code:
#include <stdio.h>#include <math.h>#include <algorithm>#define maxn 100002using 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;}
Original TLE code:
#include <stdio.h>#include <math.h>#define maxn 100002struct 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;}