2019 Multi-University Training Contest 1 D. Vacation

Problem Description

Tom and Jerry are going on a vacation. They are now driving on a one-way road and several cars are in front of them. To be more specific, there are n cars in front of them. The ith car has a length of li, the head of it is si from the stop-line, and its maximum velocity is vi. The car Tom and Jerry are driving is l0 in length, and s0 from the stop-line, with a maximum velocity of v0.
The traffic light has a very long cycle. You can assume that it is always green light. However, since the road is too narrow, no car can get ahead of other cars. Even if your speed can be greater than the car in front of you, you still can only drive at the same speed as the anterior car. But when not affected by the car ahead, the driver will drive at the maximum speed. You can assume that every driver here is very good at driving, so that the distance of adjacent cars can be kept to be 0.
Though Tom and Jerry know that they can pass the stop-line during green light, they still want to know the minimum time they need to pass the stop-line. We say a car passes the stop-line once the head of the car passes it.
Please notice that even after a car passes the stop-line, it still runs on the road, and cannot be overtaken.

Input

This problem contains multiple test cases.
For each test case, the first line contains an integer n (1≤n≤105,∑n≤2×106), the number of cars.
The next three lines each contains n+1 integers, li,si,vi (1≤si,vi,li≤109). It’s guaranteed that si≥si+1+li+1,∀i∈[0,n−1]

Output

For each test case, output one line containing the answer. Your answer will be accepted if its absolute or relative error does not exceed 10−6.
Formally, let your answer be a, and the jury’s answer is b. Your answer is considered correct if |a−b|max(1,|b|)≤10−6.
The answer is guaranteed to exist.

Sample Input

1
2 2
7 1
2 1
2
1 2 2
10 7 1
6 2 1

Sample Output

3.5000000000
5.0000000000

Source
2019 Multi-University Training Contest 1


二分找到答案
每次循环都测试是否越过停车线,如果越过了,就说明不需要那么长时间,反之 需要更长的时间
具体见代码:

AC Code:

/*
 * Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
 * @Author: Ng Kimbing, HNU.
 * @LastModified:2019-07-22 T 13:19:28.991 +08:00
 */

package ACMProblems.Summer2019.HDU;

import java.io.*;

import static ACMProblems.ACMIO.*;

public class Vacation {
    //Code for IO has been omitted.
    
    static final int maxn = (int) (2e6 + 5);
    private static int n;
    private static int[] l = new int[maxn];
    private static int[] s = new int[maxn];
    private static int[] v = new int[maxn];
    private static double[] tailPos = new double[maxn];

    private static void input(int[] arr) throws Exception {
        for (int i = 0; i <= n; ++i)
            arr[i] = nextInt();
    }

    /**
     * Return whether my car has pass the stop-line after {@code testTime} minutes
     *
     * @param testTime time
     * @return whether my car has pass the stop-line after {@code testTime} minutes
     */
    private static boolean havePassed(double testTime) {
        //The position of the tail of the first car.
        tailPos[n] = testTime * v[n] - s[n] - l[n];
        for (int i = n - 1; i >= 0; i--) {
            //The position of the front of current car (car[i])
            double currFrontPos = testTime * v[i] - s[i];
            //if catch the car ahead
            if (currFrontPos >= tailPos[i + 1])
                tailPos[i] = tailPos[i + 1] - l[i];
            else tailPos[i] = currFrontPos - l[i];
        }
        double myFront = tailPos[0] + l[0] + 1e-7;
        //Return whether my car have passed the stop-line.
        return myFront >= 0;
    }

    static BufferedReader ansReader;

    static double nextAnswer() throws Exception {
        return Double.parseDouble(ansReader.readLine());
    }

    public static void main(String[] args) throws Exception {
//        ansReader = new BufferedReader(new FileReader(new File("1004.a")));
//        File f = new File("1004");
//        setStream(new FileInputStream(f));
        while (true) {
            try {
                n = nextInt();
                input(l);
                input(s);
                input(v);
                double l = 0.0, r = 1e9, ans;
                while (l + 1e-7 <= r) {
                    double mid = (l + r) / 2.0;
                    if (havePassed(mid)) {
                        r = mid;
                    } else l = mid;
                }
                ans = r;
//                if (ans - nextAnswer() > 1e-6)
//                    System.err.println("Wrong!");
                out.println(ans + 1e-7);
                out.flush();
            } catch (Throwable ignored) {
                out.flush();
                return;
            }
//            System.out.println("right");
        }

    }
}

你可能感兴趣的:(ACM)