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.LinkedHashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.stream.Collectors;
- public class Problem1 {
- public static void main(String[] args) throws IOException {
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- int bunkerCapacity = Integer.parseInt(bf.readLine());
- String line;
- LinkedList<String> bunkers = new LinkedList<>();
- LinkedList<Integer> weapons = new LinkedList<Integer>();
- int currentCapacity = 0;
- while (!(line = bf.readLine()).equals("Bunker Revision")) {
- String[] text = line.split("\\s+");
- for (String item : text) {
- if (!Character.isDigit(item.charAt(0))) {
- bunkers.add(item);
- } else {
- int weaponCapacity = Integer.parseInt(item);
- if (weaponCapacity + currentCapacity <= bunkerCapacity) {
- weapons.add(weaponCapacity);
- currentCapacity += weaponCapacity;
- if (currentCapacity == bunkerCapacity) {
- System.out.printf("%s -> %s\n", bunkers.poll(), weapons.stream()
- .map(String::valueOf).collect(Collectors.joining(", ")));
- currentCapacity = 0;
- weapons.clear();
- }
- } else {
- {
- weapons.add(weaponCapacity);
- if (weaponCapacity <= bunkerCapacity) {
- currentCapacity -= weapons.poll();
- while (currentCapacity + weaponCapacity >= bunkerCapacity && weapons.size() > 1) {
- if (currentCapacity + weaponCapacity == bunkerCapacity) {
- System.out.printf("%s -> %s\n", bunkers.poll(), weapons.stream()
- .map(String::valueOf).collect(Collectors.joining(", ")));
- weapons.clear();
- currentCapacity = 0;
- break;
- }
- currentCapacity -= weapons.poll();
- }
- } else {
- weapons.poll();
- if (bunkers.size() > 1) {
- System.out.printf("%s -> Empty\n", bunkers.poll());
- }
- }
- }
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement