paranid5

07.02 4

Feb 7th, 2022
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1. import org.jetbrains.annotations.Contract;
  2. import org.jetbrains.annotations.NotNull;
  3.  
  4. import java.io.IOException;
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7. import java.util.ArrayDeque;
  8. import java.util.function.BiFunction;
  9. import java.util.stream.Collectors;
  10.  
  11. final class Pair<F, S> {
  12.     public F first;
  13.     public S second;
  14.  
  15.     @Contract(pure = true)
  16.     Pair(final F f, final S s) {
  17.         first = f;
  18.         second = s;
  19.     }
  20. }
  21.  
  22. public enum Main {;
  23.     private static final @NotNull <T, R> R fold(
  24.             final @NotNull Iterable<T> iterable,
  25.             final @NotNull R init, final BiFunction<? super R, ? super T, ? extends R> accumulator
  26.     ) {
  27.         var acc = init;
  28.  
  29.         for (final var x : iterable)
  30.             acc = accumulator.apply(acc, x);
  31.  
  32.         return acc;
  33.     }
  34.  
  35.     public static final void main(final String[] args) throws IOException {
  36.         try (final var reader = new BufferedReader(new FileReader("kek.txt"))) {
  37.             final var input = reader.lines().collect(Collectors.toList());
  38.             final var t = Integer.parseInt(input.get(0).split(" ")[1]);
  39.  
  40.             final var viruses = input
  41.                     .stream()
  42.                     .map((x) -> {
  43.                         final var strInp = x.split(" ");
  44.                         return new Pair<>(Integer.parseInt(strInp[0]), Integer.parseInt(strInp[1]));
  45.                     })
  46.                     .collect(Collectors.toList());
  47.  
  48.             final var mid = fold(viruses, 0, (acc, x) -> acc + x.first) * 1F / viruses.size();
  49.  
  50.             final var superViruses = viruses
  51.                     .stream()
  52.                     .filter((x) -> x.first > mid)
  53.                     .map((x) -> x.second)
  54.                     .sorted()
  55.                     .collect(Collectors.toCollection(ArrayDeque::new));
  56.  
  57.             final var simpleViruses = viruses
  58.                     .stream()
  59.                     .filter((x) -> x.first <= mid)
  60.                     .map((x) -> x.second)
  61.                     .sorted()
  62.                     .collect(Collectors.toCollection(ArrayDeque::new));
  63.  
  64.             var curT = 0;
  65.             var removedViruses = 0;
  66.             var superTime = 0;
  67.  
  68.             while (true) {
  69.                 if (simpleViruses.isEmpty())
  70.                     break;
  71.  
  72.                 if (superViruses.isEmpty()) {
  73.                     while (!simpleViruses.isEmpty() && curT + simpleViruses.getFirst() <= t) {
  74.                         curT += simpleViruses.removeFirst();
  75.                         removedViruses++;
  76.                     }
  77.  
  78.                     break;
  79.                 } else {
  80.                     var curSuper = superViruses.getFirst();
  81.                     var curSimple = simpleViruses.getFirst();
  82.  
  83.                     if (curT + curSuper + curSimple > t) {
  84.                         while (!simpleViruses.isEmpty() && curT + simpleViruses.getFirst() <= t) {
  85.                             curT += simpleViruses.removeFirst();
  86.                             removedViruses++;
  87.                         }
  88.  
  89.                         break;
  90.                     } else {
  91.                         superTime += superViruses.getFirst();
  92.                         curT += simpleViruses.removeFirst() + superViruses.removeFirst();
  93.                         removedViruses += 2;
  94.                     }
  95.                 }
  96.             }
  97.  
  98.             System.out.printf("%d %d", removedViruses, superTime);
  99.         }
  100.     }
  101. }
Add Comment
Please, Sign In to add comment