782B The Meeting Place Cannot Be Changed(二分)

链接:http://codeforces.com/problemset/problem/782/B

题意: N个点,需要找到一个点使得每个点到这个点耗时最小,每个点都同时开始,且都拥有自己的速度

题解: 对于一个确定的位置,如果耗时最久的点在右边,则这个位置可以往右靠,否则就往左靠,这样,一个二分的解法就形成了

import java.lang.Math;
import java.util.Scanner;

public class CodeForces_403_B {
    private static final int N = (int) 6e4 + 10;
    static int a[] = new int[N];
    static int v[] = new int[N];
    static double eps = 1e-6;

    public static void main(String args[]) {
        // System.out.println("dfwaed");
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            for (int i = 0; i < n; i++)
                a[i] = sc.nextInt();
            for (int i = 0; i < n; i++)
                v[i] = sc.nextInt();

            double l = 1, r = Double.MAX_VALUE, tmp = 0, tmp1, ans = 0;
            int tmp2 = 0;
            while (Math.abs(r - l) > eps) {

                double mid = (l + r) / 2;
                tmp = Double.MIN_VALUE;
                for (int i = 0; i < n; i++) {
                    tmp1 = Math.abs(a[i] - mid) / v[i];
                    if (tmp1 > tmp) {
                        tmp = tmp1;
                        tmp2 = i;
                    }
                }

                if ((double) a[tmp2] > mid) {
                    l = mid;
                    ans = tmp;
                } else
                    r = mid;
            }
            System.out.printf("%.6f\n", ans);
        }
        sc.close();
    }
}

 

posted @ 2017-03-07 20:46  江南何采莲  阅读(180)  评论(0)    收藏  举报