hdu1007 Quoit Design (最近点对)

Quoit Design

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27500    Accepted Submission(s): 7256


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
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
import java.io.PrintWriter;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.OutputStreamWriter;
import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Formatter;

class Main
{
	public static final boolean DEBUG = true;
	public int n;
	public Point[] p;
	public Scanner cin;
	
	public Comparator<Point> cmpy = new Comparator<Point>() {
		public int compare(Point a, Point b)
		{
			if (a.y < b.y) return -1;
			else if (Math.abs(a.y - b.y) < 1e-9) return 0;
			else return 1;
		}
	};
	
	public Comparator<Point> cmpx = new Comparator<Point>() {
		public int compare(Point a, Point b)
		{
			if (a.x < b.x) return -1;
			else if (Math.abs(a.x - b.x) < 1e-9) return 0;
			else return 1;
		}
	};

	class Point
	{
		double x, y;
	}
	
	public void init() throws IOException
	{
		if (DEBUG) {
			cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));
		} else {
			cin = new Scanner(System.in);
		}
	}
	
	public boolean input() throws IOException
	{
		
		n = cin.nextInt();
		
		if (n == 0) return false;
		
		p = new Point[n];
		
		for (int i = 0; i < n; i++) {
			p[i] = new Point();
			p[i].x = cin.nextDouble();
			p[i].y = cin.nextDouble();
		}
		
		return true;
	}
	
	public double dis(Point a, Point b)
	{
		double x = a.x - b.x;
		double y = a.y - b.y;
		
		return Math.sqrt(x * x + y * y);
	}
	
	public double closestPair(int l, int r)
	{
		if (r - l == 1) {
			return dis(p[l], p[r]);
		} else if (r - l == 2) {
			double d1 = dis(p[l], p[l + 1]);
			double d2 = dis(p[l + 1], p[r]);
			double d3 = dis(p[l], p[r]);
			
			return Math.min(d1, Math.min(d2, d3));
		}
		
		int m = (l + r) >> 1;
		double dm = Math.min(closestPair(l, m), closestPair(m + 1, r));
		
		List<Point> q = new ArrayList<Point>();
		for (int i = l; i <= r; i++) {
			if (Math.abs(p[i].x - p[m].x) < dm) q.add(p[i]);
		}
		
		Collections.sort(q, cmpy);
		
		int size = q.size();
		for (int i = 0; i < size; i++) {
			for (int j = i + 1; j < size && q.get(j).y - q.get(i).y < dm; j++) {
				double tmp = dis(q.get(j), q.get(i));
				dm = Math.min(tmp, dm);
			}
		}
		
		return dm;
		
	}
	
	
	public double solve()
	{
		Arrays.sort(p, cmpx);
		double ans = closestPair(0, n - 1);
		
		return ans / 2;
	}
	
	public static void main(String[] args) throws IOException
	{
		Main solver = new Main();
		solver.init();
		
		while (solver.input()) {
			double ans = solver.solve();
			//System.out.printf("%.2f\n", ans);
			Formatter fmt = new Formatter();
			fmt.format("%.2f", ans);
			System.out.println(fmt);
		}
	}
}



你可能感兴趣的:(hdu1007 Quoit Design (最近点对))