Lanqiao Cup 2023 14th Provincial Competition Real Questions-Plane Landing---Problem Solutions

Table of contents

Lanqiao Cup 2023 14th Provincial Competition Real Questions-Plane Landing

Question description

Input format

Output format

Sample input

Sample output

hint

[Analysis of ideas]

【Code】


If you think you can write well, you can join the QQ group 907575059.

Lanqiao Cup 2023 14th Provincial Competition Real Questions-Plane Landing

Time limit: 3s Memory limit: 320MB Submits: 3890 Resolves: 912

Question description

N planes are preparing to land at an airport with only one runway. Among them, the i-th aircraft arrives over the airport at time Ti. When it arrives, its remaining fuel can continue to hover for Di unit time, that is, it is the earliest

The landing can start at time Ti, and the landing can start at time Ti + Di at the latest. The landing process takes Li units of time.

When one plane completes landing, another plane can immediately start landing at the same time, but it cannot start landing before the previous plane completes its landing.

Please judge whether all N planes can land safely.

Input format

The input contains multiple sets of data.

The first line contains an integer T, representing the number of groups of test data.

For each set of data, the first row contains an integer N.

The following N lines each contain three integers: Ti, Di and Li.

Output format

For each set of data, output YES or NO, indicating whether all can land safely.

Sample input

copy

2
3
0 100 10
10 10 10
0 2 20
3
0 10 20
10 10 20
20 10 20

Sample output

copy

YES
NO

hint

For the first set of data, the third aircraft can be arranged to start landing at time 0 and complete the landing at time 20. Arrange for the second plane to start landing at 20:00 and complete the landing at 30:00. Arrange for the first plane to start landing at 30:00 and complete the landing at 40:00.

For the second set of data, no matter how arranged, there will be planes that cannot land in time.

For 30% of the data, N ≤ 2.

For 100% of the data, 1 ≤ T ≤ 10, 1 ≤ N ≤ 10, 0 ≤ Ti, Di, Li ≤ 10^5.

[Analysis of ideas]

Because the data size is so small, you can directly search for every possibility, up to 10! It's a possibility and it's not complicated. Try every possibility (if there are three planes 1 2 3, 1 3 2, 2 1 3, 2 3 1, 3 1 2, 3 2 1 this is all the possibilities, iterate through every possibility), if there is one If it is possible to land all the planes, output YES, otherwise output NO.

【Code】

import java.util.Scanner;

/**
 * @ProjectName: study3
 * @FileName: Ex7
 * @author:HWJ
 * @Data: 2023/9/18 21:18
 */

public class Ex7 {
    static Node[] nodes;
    static boolean[] use;
    static int n;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        while (t-- > 0) {
            n = scanner.nextInt();
            // Node数组,表示每个飞机
            nodes = new Node[n];
            // use 表示飞机是否被使用过
            use = new boolean[n];
            for (int i = 0; i < n; i++) {
                int a = scanner.nextInt();
                int b = scanner.nextInt();
                int c = scanner.nextInt();
                nodes[i] = new Node(a, b, c);
            }
            // dfs有返回值 如果是true则是YES 是false则是NO
            if (dfs(0, 0)) {
                System.out.println("YES");
            } else {
                System.out.println("NO");
            }
        }
    }

    //  变量last是某个飞机降落完成的最早时间
    // fj 表示已完成飞机数量
    static boolean dfs(int fj, int last) {
        if (fj == n){
            return true;
        }
        for (int i = 0; i < n; i++) { // 可能性遍历的过程
            // 如果此时飞机还能使用的话 就使用它
            if (!use[i] && nodes[i].start + nodes[i].wait >= last){
                use[i] = true;
                if (dfs(fj + 1, Math.max(nodes[i].start, last) + nodes[i].need)){
                    return true;
                }
                use[i] = false; // 如果使用它并不能得到答案,就不使用它。
            }
        }
        return false;
    }

    public static class Node {
        int start, wait, need;

        public Node(int start, int wait, int need) {
            this.start = start;
            this.wait = wait;
            this.need = need;
        }

    }
}

Guess you like

Origin blog.csdn.net/weixin_73936404/article/details/132999852