Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayDeque;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class Main {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- int numberOfRows = Integer.parseInt(reader.readLine());
- ArrayDeque<Integer> platesQueue = new ArrayDeque<>();
- Arrays.stream(reader.readLine().split("\\s+"))
- .mapToInt(Integer::parseInt).forEach(platesQueue::offer);
- ArrayDeque<Integer> waveStack = new ArrayDeque<>();
- for (int i = 1; i <= numberOfRows; i++) {
- int[] arr = Arrays.stream(reader.readLine().split("\\s+"))
- .mapToInt(Integer::parseInt).toArray();
- if (platesQueue.isEmpty()) {
- break;
- }
- Arrays.stream(arr).forEach(waveStack::push);
- // Arrays.stream(reader.readLine().split("\\s+"))
- // .mapToInt(Integer::parseInt).forEach(waveStack::push);
- if (i % 3 == 0) {
- int newPlate = Integer.parseInt(reader.readLine());
- platesQueue.offer(newPlate);
- }
- while (platesQueue.size() > 0 && waveStack.size() > 0) {
- int currPlate = platesQueue.poll();
- int currWarrior = waveStack.pop();
- if (currPlate != currWarrior) {
- if (currWarrior > currPlate) {
- currWarrior -= currPlate;
- waveStack.push(currWarrior);
- } else { //currWarrior < currPlate
- currPlate -= currWarrior;
- platesQueue.offer(currPlate);
- }
- }
- }
- }
- if (platesQueue.size() > 0) {
- System.out.println("The Spartans successfully repulsed the Trojan attack.");
- System.out.println("Plates left: " + platesQueue.toString()
- .replaceAll("[\\[\\]]", ""));
- } else {
- System.out.println("The Trojans successfully destroyed the Spartan defense.");
- System.out.println("Warriors left: " + waveStack.toString()
- .replaceAll("[\\[\\]]", ""));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement