Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Advanced;
- import java.util.ArrayDeque;
- import java.util.Arrays;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- ArrayDeque<Long> licensePlatesQueue = Arrays.stream(scanner.nextLine().split(", "))
- .map(Long::parseLong)
- .collect(Collectors.toCollection(ArrayDeque::new));
- ArrayDeque<Long> carsStack = new ArrayDeque<>();
- Arrays.stream(scanner.nextLine().split(", "))
- .map(Long::parseLong)
- .forEach(carsStack::push);
- int daysCount = 0;
- long registeredCarsCount = 0;
- while (!licensePlatesQueue.isEmpty() && !carsStack.isEmpty()) {
- long currentPlatesCount = licensePlatesQueue.poll();
- long currentCarsCount = carsStack.pop();
- daysCount++;
- long remainingPlates;
- long remainingCars;
- if (currentPlatesCount / 2 < currentCarsCount) {
- registeredCarsCount += currentPlatesCount / 2;
- remainingCars = currentCarsCount - currentPlatesCount / 2;
- carsStack.push(remainingCars);
- } else if (currentPlatesCount / 2 > currentCarsCount) {
- remainingPlates = currentPlatesCount - currentCarsCount * 2;
- licensePlatesQueue.offer(remainingPlates);
- registeredCarsCount += currentCarsCount;
- } else {
- registeredCarsCount += currentPlatesCount / 2;
- }
- }
- System.out.printf("%d cars were registered for %d days!\n", registeredCarsCount, daysCount);
- if (!licensePlatesQueue.isEmpty()) {
- long platesRemain = 0;
- while (!licensePlatesQueue.isEmpty()) {
- platesRemain += licensePlatesQueue.poll();
- }
- System.out.printf("%d license plates remain!", platesRemain);
- } else if (!carsStack.isEmpty()) {
- long unregisteredCars = 0;
- while (!carsStack.isEmpty()) {
- unregisteredCars += carsStack.pop();
- }
- System.out.printf("%d cars remain without license plates!", unregisteredCars);
- } else {
- System.out.print("Good job! There is no queue in front of the KAT!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement