LardaX

PoisonousPlants

Feb 12th, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayDeque;
  5. import java.util.Deque;
  6. import java.util.stream.Stream;
  7.  
  8. public class PoisonousPlants {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.         int initialPlantsCount = Integer.parseInt(reader.readLine());
  12.         int[] plants = Stream.of(reader.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  13.         Deque<Integer> plantsLeft = new ArrayDeque<>();
  14.  
  15.         int day = 0;
  16.  
  17.         while (true) {
  18.             if (plants.length == 1) {
  19.                 System.out.println(day);
  20.                 return;
  21.             }
  22.  
  23.             for (int i = plants.length - 1; i >= 1; i--) {
  24.                 int leftPlant = plants[i-1];
  25.                 int currentPlant = plants[i];
  26.  
  27.                 if (currentPlant < leftPlant) {
  28.                     plantsLeft.push(currentPlant);
  29.                 }
  30.  
  31.                 if (i == 1) {
  32.                     plantsLeft.push(leftPlant);
  33.                 }
  34.             }
  35.  
  36.             if (plantsLeft.size() == plants.length) {
  37.                 System.out.println(day);
  38.                 return;
  39.             }
  40.  
  41.             day++;
  42.             plants = new int[plantsLeft.size()];
  43.  
  44.             for (int i = 0; i < plants.length; i++) {
  45.                 plants[i] = plantsLeft.poll();
  46.             }
  47.  
  48.         }
  49.  
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment