valerielashvili

Hornet Assault

Sep 3rd, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class p03_Hornet_Assault {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         String beehivesInput = scanner.nextLine();
  10.         String hornetsInput = scanner.nextLine();
  11.  
  12.         ArrayList<Long> beehives = new ArrayList<>();
  13.         ArrayList<Long> hornets = new ArrayList<>();
  14.  
  15.         if (beehivesInput.length() > 0) {
  16.             beehives = Arrays.stream(beehivesInput.split(" "))
  17.                     .mapToLong(Long::parseLong).boxed().collect(Collectors.toCollection(ArrayList::new));
  18.         }
  19.         if (hornetsInput.length() > 0) {
  20.             hornets = Arrays.stream(hornetsInput.split(" "))
  21.                     .mapToLong(Long::parseLong).boxed().collect(Collectors.toCollection(ArrayList::new));
  22.         }
  23.  
  24.         for (int i = 0; i < beehives.size(); i++) {
  25.             long hornetsPower = hornets.stream().mapToLong(Long::valueOf).sum();
  26.             long beesAlive = beehives.get(i) - hornetsPower;
  27.  
  28.             if (beehives.get(i) >= hornetsPower) {
  29.                 if (hornets.size() > 0) {
  30.                     hornets.remove(0);
  31.                     if (beesAlive <= 0) {
  32.                         beehives.remove(i);
  33.                         i--;
  34.                     } else {
  35.                         beehives.set(i, beesAlive);
  36.                     }
  37.                 }
  38.             } else {
  39.                 beehives.remove(i);
  40.                 i--;
  41.             }
  42.         }
  43.  
  44.         if (beehives.size() > 0) {
  45.             System.out.println(beehives.stream().map(Object::toString).collect(Collectors.joining(" ")));
  46.         } else {
  47.             if (hornets.size() > 0) {
  48.                 System.out.println(hornets.stream().map(Object::toString).collect(Collectors.joining(" ")));
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment