Untitled

mail@pastecode.io avatar
unknown
plain_text
11 days ago
2.4 kB
2
Indexable
Never
import java.util.*;

public class TrainingSchedule {
    public static int findMinDays(List<Integer> training1, List<Integer> training2, List<int[]> commitments) {
        int maxTime = 0;
        int days = 0;
        
        // Calculate the maximum possible time required for training
        for (int i = 0; i < training1.size(); i++) {
            maxTime += Math.max(training1.get(i), training2.get(i));
        }

        while (days <= maxTime) {
            int totalTime = 0;
            boolean isValidSchedule = true;

            // Check if the current day falls within any commitment period
            for (int i = 0; i < commitments.size(); i++) {
                int[] commitment = commitments.get(i);
                if (commitment[0] <= days && days <= commitment[1]) {
                    isValidSchedule = false;
                    break;
                }
            }

            if (isValidSchedule) {
                int j = 0;
                for (; j < training1.size(); j++) {
                    totalTime += Math.max(training1.get(j), training2.get(j));
                    if (days <= totalTime) {
                        break;
                    }
                }
                
                if (j == training1.size()) {
                    return days;
                }
            }
            days++;
        }
        return -1;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();
        
        List<Integer> training1 = new ArrayList<>();
        List<Integer> training2 = new ArrayList<>();
        
        for (int i = 0; i < m; i++) {
            int time = scanner.nextInt();
            training1.add(time);
        }
        
        for (int i = 0; i < m; i++) {
            int time = scanner.nextInt();
            training2.add(time);
        }
        
        int p = scanner.nextInt();
        List<int[]> commitments = new ArrayList<>();
        
        for (int i = 0; i < p; i++) {
            int startTime = scanner.nextInt();
            int endTime = scanner.nextInt();
            commitments.add(new int[]{startTime, endTime});
        }
        
        int minDays = findMinDays(training1, training2, commitments);
        System.out.println(minDays);
        scanner.close();
    }
}
Leave a Comment