Advertisement
KNikov

CubicArtillery

Jun 20th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayDeque;
  7. import java.util.LinkedHashMap;
  8. import java.util.LinkedList;
  9. import java.util.List;
  10. import java.util.stream.Collectors;
  11.  
  12. public class Problem1 {
  13.  
  14.     public static void main(String[] args) throws IOException {
  15.         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  16.  
  17.         int bunkerCapacity = Integer.parseInt(bf.readLine());
  18.         String line;
  19.         LinkedList<String> bunkers = new LinkedList<>();
  20.         LinkedList<Integer> weapons = new LinkedList<Integer>();
  21.  
  22.         int currentCapacity = 0;
  23.         while (!(line = bf.readLine()).equals("Bunker Revision")) {
  24.  
  25.             String[] text = line.split("\\s+");
  26.             for (String item : text) {
  27.                 if (!Character.isDigit(item.charAt(0))) {
  28.                     bunkers.add(item);
  29.                 } else {
  30.                     int weaponCapacity = Integer.parseInt(item);
  31.                     if (weaponCapacity + currentCapacity <= bunkerCapacity) {
  32.                         weapons.add(weaponCapacity);
  33.                         currentCapacity += weaponCapacity;
  34.                         if (currentCapacity == bunkerCapacity) {
  35.                             System.out.printf("%s -> %s\n", bunkers.poll(), weapons.stream()
  36.                                     .map(String::valueOf).collect(Collectors.joining(", ")));
  37.                             currentCapacity = 0;
  38.                             weapons.clear();
  39.                         }
  40.                     } else {
  41.                         {
  42.                             weapons.add(weaponCapacity);
  43.                             if (weaponCapacity <= bunkerCapacity) {
  44.                                 currentCapacity -= weapons.poll();
  45.                                 while (currentCapacity + weaponCapacity >= bunkerCapacity && weapons.size() > 1) {
  46.                                     if (currentCapacity + weaponCapacity == bunkerCapacity) {
  47.                                         System.out.printf("%s -> %s\n", bunkers.poll(), weapons.stream()
  48.                                                 .map(String::valueOf).collect(Collectors.joining(", ")));
  49.                                         weapons.clear();
  50.                                         currentCapacity = 0;
  51.                                         break;
  52.                                     }
  53.                                     currentCapacity -= weapons.poll();
  54.                                 }
  55.                             } else {
  56.                                 weapons.poll();
  57.                                 if (bunkers.size() > 1) {
  58.                                     System.out.printf("%s -> Empty\n", bunkers.poll());
  59.                                 }
  60.                             }
  61.                         }
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.  
  67.     }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement