2C. Commentator problem

一、Problem

The Olympic Games in Bercouver are in full swing now. Here everyone has their own objectives: sportsmen compete for medals, and sport commentators compete for more convenient positions to give a running commentary. Today the main sport events take place at three round stadiums, and the commentator’s objective is to choose the best point of observation, that is to say the point from where all the three stadiums can be observed. As all the sport competitions are of the same importance, the stadiums should be observed at the same angle. If the number of points meeting the conditions is more than one, the point with the maximum angle of observation is prefered.

Would you, please, help the famous Berland commentator G. Berniev to find the best point of observation. It should be noted, that the stadiums do not hide each other, the commentator can easily see one stadium through the other.

Input
The input data consists of three lines, each of them describes the position of one stadium. The lines have the format x,  y,  r, where (x, y) are the coordinates of the stadium’s center ( -  103 ≤ x,  y ≤ 103), and r (1 ≤ r  ≤ 103) is its radius. All the numbers in the input data are integer, stadiums do not have common points, and their centers are not on the same line.

Output
Print the coordinates of the required point with five digits after the decimal point. If there is no answer meeting the conditions, the program shouldn’t print anything. The output data should be left blank.

二、Example

input

0 0 10
60 0 10
30 30 10

output

30.00000 0.00000

三、Code

package com.codeforces.problemset;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class CommentatorProblem {

    private static BufferedReader in;
    private static PrintWriter out;
    private static StringTokenizer st;

    static int[] x = new int[3];
    static int[] y = new int[3];
    static int[] r = new int[3];
    static double[] d = new double[3];

    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        double dx = 0, dy = 0;
        for (int i = 0; i < 3; i++) {
            x[i] = nextInt();
            y[i] = nextInt();
            r[i] = nextInt();
            dx += x[i];
            dy += y[i];
        }
        dx /= 3;
        dy /= 3;
        double step = 1.0;
        while (step > 1e-6) {
            if (deviation(dx, dy) > deviation(dx + step, dy))
                dx += step;
            else if (deviation(dx, dy) > deviation(dx - step, dy))
                dx -= step;
            else if (deviation(dx, dy) > deviation(dx, dy + step))
                dy += step;
            else if (deviation(dx, dy) > deviation(dx, dy - step))
                dy -= step;
            else
                step *= 0.7;
        }
        if (deviation(dx, dy) < 1e-5) {
            out.printf("%.5f %.5f\n", dx, dy);
        }
        out.close();
    }

    private static double deviation(double dx, double dy) {
        for (int i = 0; i < 3; i++) {
            // Distances from point to centers / r
            d[i] = Math.sqrt(sqr(x[i] - dx) + sqr(y[i] - dy)) / r[i];
        }
        // Value for minimization
        double s = sqr(d[0] - d[1]) + sqr(d[1] - d[2]) + sqr(d[2] - d[0]);
        return s;
    }

    public static String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }

    private static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    private static Long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    private static Double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    public static double sqr(double x) {
        return x * x;
    }
}
发布了332 篇原创文章 · 获赞 198 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/riemann_/article/details/103500089