Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class PreparingOlympiad {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner scanner = new Scanner(new File("preparing-olympiad.txt"));
- int questions = scanner.nextInt();
- long minimumTotal = scanner.nextLong();
- long maximumTotal = scanner.nextLong();
- int minimumDifference = scanner.nextInt(); // min diff between max difficulty and min difficulty
- int[] difficulties = new int[questions];
- for (int i = 0; i < questions; i++) {
- difficulties[i] = scanner.nextInt();
- }
- int answers = 0;
- long[] absoluteSum = new long[questions];
- int[] absoluteLowestNumber = new int[questions];
- int[] absoluteHighestNumber = new int[questions];
- absoluteSum[0] = difficulties[0];
- absoluteLowestNumber[0] = difficulties[0];
- absoluteHighestNumber[0] = difficulties[0];
- for (int currentIndex = 0; currentIndex < difficulties.length; currentIndex++) {
- for (int otherIndex = currentIndex + 1; otherIndex < difficulties.length; otherIndex++) {
- absoluteSum[otherIndex] = absoluteSum[currentIndex] + difficulties[otherIndex];
- absoluteLowestNumber[otherIndex] = Math.min(absoluteLowestNumber[currentIndex], difficulties[otherIndex]);
- absoluteHighestNumber[otherIndex] = Math.max(absoluteHighestNumber[currentIndex], difficulties[otherIndex]);
- if (currentIndex != 0) {
- if (isViableProblemSet(absoluteSum[otherIndex], absoluteLowestNumber[otherIndex], absoluteHighestNumber[otherIndex], minimumTotal, maximumTotal, minimumDifference)) {
- answers++;
- }
- }
- long localSum = difficulties[currentIndex] + difficulties[otherIndex];
- int localLowest = Math.min(difficulties[currentIndex], difficulties[otherIndex]);
- int localHighest = Math.max(difficulties[currentIndex], difficulties[otherIndex]);
- if (isViableProblemSet(localSum, localLowest, localHighest, minimumTotal, maximumTotal, minimumDifference)) {
- answers++;
- }
- }
- }
- System.out.println(answers);
- }
- private static boolean isViableProblemSet(long sum, int lowestNumber, int highestNumber, long minimumSum, long maximumSum, int minimumDifference) {
- return sum >= minimumSum && sum <= maximumSum && Math.abs(highestNumber - lowestNumber) >= minimumDifference;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement