Advertisement
Arham-4

Preparing Olympiad

Jan 16th, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. public class PreparingOlympiad {
  6.     public static void main(String[] args) throws FileNotFoundException {
  7.         Scanner scanner = new Scanner(new File("preparing-olympiad.txt"));
  8.         int questions = scanner.nextInt();
  9.         long minimumTotal = scanner.nextLong();
  10.         long maximumTotal = scanner.nextLong();
  11.         int minimumDifference = scanner.nextInt(); // min diff between max difficulty and min difficulty
  12.         int[] difficulties = new int[questions];
  13.         for (int i = 0; i < questions; i++) {
  14.             difficulties[i] = scanner.nextInt();
  15.         }
  16.  
  17.         int answers = 0;
  18.         long[] absoluteSum = new long[questions];
  19.         int[] absoluteLowestNumber = new int[questions];
  20.         int[] absoluteHighestNumber = new int[questions];
  21.  
  22.         absoluteSum[0] = difficulties[0];
  23.         absoluteLowestNumber[0] = difficulties[0];
  24.         absoluteHighestNumber[0] = difficulties[0];
  25.         for (int currentIndex = 0; currentIndex < difficulties.length; currentIndex++) {
  26.             for (int otherIndex = currentIndex + 1; otherIndex < difficulties.length; otherIndex++) {
  27.                 absoluteSum[otherIndex] = absoluteSum[currentIndex] + difficulties[otherIndex];
  28.                 absoluteLowestNumber[otherIndex] = Math.min(absoluteLowestNumber[currentIndex], difficulties[otherIndex]);
  29.                 absoluteHighestNumber[otherIndex] = Math.max(absoluteHighestNumber[currentIndex], difficulties[otherIndex]);
  30.                 if (currentIndex != 0) {
  31.                     if (isViableProblemSet(absoluteSum[otherIndex], absoluteLowestNumber[otherIndex], absoluteHighestNumber[otherIndex], minimumTotal, maximumTotal, minimumDifference)) {
  32.                         answers++;
  33.                     }
  34.                 }
  35.  
  36.                 long localSum = difficulties[currentIndex] + difficulties[otherIndex];
  37.                 int localLowest = Math.min(difficulties[currentIndex], difficulties[otherIndex]);
  38.                 int localHighest = Math.max(difficulties[currentIndex], difficulties[otherIndex]);
  39.                 if (isViableProblemSet(localSum, localLowest, localHighest, minimumTotal, maximumTotal, minimumDifference)) {
  40.                     answers++;
  41.                 }
  42.             }
  43.         }
  44.         System.out.println(answers);
  45.     }
  46.  
  47.     private static boolean isViableProblemSet(long sum, int lowestNumber, int highestNumber, long minimumSum, long maximumSum, int minimumDifference) {
  48.         return sum >= minimumSum && sum <= maximumSum && Math.abs(highestNumber - lowestNumber) >= minimumDifference;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement