Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.jetbrains.annotations.Contract;
- import org.jetbrains.annotations.NotNull;
- import java.io.IOException;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.util.ArrayDeque;
- import java.util.function.BiFunction;
- import java.util.stream.Collectors;
- final class Pair<F, S> {
- public F first;
- public S second;
- @Contract(pure = true)
- Pair(final F f, final S s) {
- first = f;
- second = s;
- }
- }
- public enum Main {;
- private static final @NotNull <T, R> R fold(
- final @NotNull Iterable<T> iterable,
- final @NotNull R init, final BiFunction<? super R, ? super T, ? extends R> accumulator
- ) {
- var acc = init;
- for (final var x : iterable)
- acc = accumulator.apply(acc, x);
- return acc;
- }
- public static final void main(final String[] args) throws IOException {
- try (final var reader = new BufferedReader(new FileReader("kek.txt"))) {
- final var input = reader.lines().collect(Collectors.toList());
- final var t = Integer.parseInt(input.get(0).split(" ")[1]);
- final var viruses = input
- .stream()
- .map((x) -> {
- final var strInp = x.split(" ");
- return new Pair<>(Integer.parseInt(strInp[0]), Integer.parseInt(strInp[1]));
- })
- .collect(Collectors.toList());
- final var mid = fold(viruses, 0, (acc, x) -> acc + x.first) * 1F / viruses.size();
- final var superViruses = viruses
- .stream()
- .filter((x) -> x.first > mid)
- .map((x) -> x.second)
- .sorted()
- .collect(Collectors.toCollection(ArrayDeque::new));
- final var simpleViruses = viruses
- .stream()
- .filter((x) -> x.first <= mid)
- .map((x) -> x.second)
- .sorted()
- .collect(Collectors.toCollection(ArrayDeque::new));
- var curT = 0;
- var removedViruses = 0;
- var superTime = 0;
- while (true) {
- if (simpleViruses.isEmpty())
- break;
- if (superViruses.isEmpty()) {
- while (!simpleViruses.isEmpty() && curT + simpleViruses.getFirst() <= t) {
- curT += simpleViruses.removeFirst();
- removedViruses++;
- }
- break;
- } else {
- var curSuper = superViruses.getFirst();
- var curSimple = simpleViruses.getFirst();
- if (curT + curSuper + curSimple > t) {
- while (!simpleViruses.isEmpty() && curT + simpleViruses.getFirst() <= t) {
- curT += simpleViruses.removeFirst();
- removedViruses++;
- }
- break;
- } else {
- superTime += superViruses.getFirst();
- curT += simpleViruses.removeFirst() + superViruses.removeFirst();
- removedViruses += 2;
- }
- }
- }
- System.out.printf("%d %d", removedViruses, superTime);
- }
- }
- }
Add Comment
Please, Sign In to add comment