杭电ACM OJ 1007 Quoit Design 最近点对 分治 递归

来源:互联网 发布:php短信平台源码 编辑:程序博客网 时间:2024/06/03 16:48

Quoit Design

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


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
20 01 121 11 13-1.5 00 00 1.50
 

Sample Output
0.710.00

0.75

问题翻译:输入几个点,求最近点对距离的一半

在这里有详细解答http://blog.csdn.net/qq_36523667/article/details/78444052,不过传的值只能是int型的

下面是解决double型的

public class QuoitDesign {    static List<Double> listX;    static List<Double> listY;    public static void main(final String[] args) throws Exception {        initData();        double result = caculate(listX, listY);        System.out.println(result);    }    private static void initData() {        listX = new ArrayList<>();        listY = new ArrayList<>();        //不明白怎么保存浮点型的点,惭愧        listX.add(-1.5);listY.add(0.0);        listX.add(0.0);listY.add(0.0);        listX.add(1.5);listY.add(0.0);        //另一组测试数据//        listX.add(1.0);listY.add(2.0);//        listX.add(33.0);listY.add(7.0);//        listX.add(9.0);listY.add(6.0);//        listX.add(1000.0);listY.add(2.0);//        listX.add(-505.0);listY.add(41.0);//        listX.add(13.0);listY.add(12.0);//        listX.add(18.0);listY.add(15.0);//        listX.add(11.0);listY.add(2.0);//        listX.add(-1000.0);listY.add(-1000.0);        BubbleSort();//保证坐标按x增序    }    private static void BubbleSort() {        int size = listX.size();        for (int i = 0; i < size - 1; i++) {            for (int j = 0; j < size - i - 1; j++) {                if (listX.get(j) > listX.get(j+1)) {                    Collections.swap(listX, j, j+1);                    Collections.swap(listY, j, j+1);                }            }        }    }    private static double dis(double x1, double y1, double x2, double y2) {        return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));    }    private static double caculate(List<Double> listX, List<Double> listY) {        double result = Double.MAX_VALUE;        int size = listX.size();        if (size == 3) {            double dis1 = dis(listX.get(0), listY.get(0), listX.get(1), listY.get(1));            double dis2 = dis(listX.get(0), listY.get(0), listX.get(2), listY.get(2));            double dis3 = dis(listX.get(1), listY.get(1), listX.get(2), listY.get(2));            return Math.min(Math.min(dis1, dis2), dis3);        }        if (size == 2) {            return dis(listX.get(0), listY.get(0), listX.get(1), listY.get(1));        }        //分割        int leftSize = size / 2;// 9 45 /8 44        int rightSize = size - size / 2;        List<Double> leftListX = new ArrayList<>();        List<Double> leftListY = new ArrayList<>();        List<Double> rightListX = new ArrayList<>();        List<Double> rightListY = new ArrayList<>();        for (int i = 0; i < leftSize; i++) {            leftListX.add(listX.get(i));            leftListY.add(listY.get(i));        }        for (int i = 0; i < rightSize; i++) {            int mI = i + leftSize;            rightListX.add(listX.get(mI));            rightListY.add(listY.get(mI));        }        //假设分割的左右已经在考虑了所有的情况以后取得最小值        result = Math.min(caculate(leftListX, leftListY), caculate(rightListX, rightListY));        //除了左右,还需要考虑中间的,如果超过中轴线result距离的,就绝对不可能        //求中轴线        //分割的左右数目相等        double mid = 0;        if (leftSize == rightSize) {            mid = (listX.get(leftSize) + listX.get(leftSize + 1))/2;        }        //右比左多一个        else if (leftSize + 1 == rightSize) {            mid = listX.get(leftSize + 1);        }        //求左右可行范围,并把可行的放入新集合        double leftX = mid - result;        double rightX = mid + result;        List<Double> midListX = new ArrayList<>();        List<Double> midListY = new ArrayList<>();        int midSize = 0;        for (int i = 0; i < size; i++) {            double x = listX.get(i);            if (x <= rightX && x >= leftX) {                midListX.add(listX.get(i));                midListY.add(listY.get(i));                midSize ++;            }        }        //计算mid最小,跟左右最小比        for (int i = 0; i < midSize; i++) {            for (int j = i + 1; j < midSize; j ++) {                double dis = dis(midListX.get(i), midListY.get(i), midListX.get(j), midListY.get(j));                if (dis < result) {                    result = dis;                }            }        }        return result;    }}

阅读全文
0 0
原创粉丝点击