Huawei OD real test - street lighting

/**
* Street lighting problem
*
* N street lights are installed on a straight road, starting from position 0, and the distance between street lights is fixed at 100 meters.
*
* Each street light has its own lighting radius, please calculate the sum of the lengths of the intervals that cannot be illuminated between the first street light and the last street light.
*
* Input description
*
* The first line is a number N, indicating the number of street lights, 1<=N<=100000
*
* The second line is a number separated by N spaces, indicating the lighting radius of the path, 1<=lighting radius<= 100000*100
*
* Output description
*
* Between the first street light and the last street light, the sum of the length of the interval that cannot be illuminated
*
* Example 1 The input and output examples are only for debugging, and the background judgment data generally does not include examples
*
* Input
*
* 2
*
* 50 50
*
* Output
*
* 0
*
* Description
*
* Streetlight 1 covers 0-50, Streetlight 2 covers 50-100, there is no uncovered interval between Streetlight 1 and Streetlight 2 (0m-100m) .
*
* Example 2 The input and output examples are for debugging only, and the background judgment data generally does not include examples
*
* Input
*
* 4
*
* 50 70 20 70
*
* Explanation:
* *
Streetlight 1 covers 0-50
*
* Streetlight 2 covers 30-170 * * Streetlight 3 covers 180-220
* * Streetlight 4 covers 230-370 * * Output * * 20 * * Explanation * * [170,180],[220,230], two uncovered intervals, the total mileage is 20 */











public class StreetLighting {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int lightNum = Integer.parseInt(sc.nextLine());
        List<Integer> radius = new ArrayList<>();
        for (int i = 0; i < lightNum; i++){
            radius.add(sc.nextInt());
        }
        int range = rang(radius);
        System.out.println(range);
    }

    public static int rang(List<Integer> radis){
        int range = 0;
        int left;
        int right = radis.get(0);
        int beforeRight = right;
        for (int i = 1; i < radis.size(); i++) {
            int value = 100 * (i);
            left = value - radis.get(i);
            right = value + radis.get(i);
            if (left > beforeRight) {
                range = range + (left - beforeRight);
            }
            beforeRight = right;
        }
        return range;
    }
}

Guess you like

Origin blog.csdn.net/weixin_42450130/article/details/131609143