Advertisement
Guest User

PoisonousPlantsRework

a guest
Feb 12th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 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 PoisonousPlantsRework {
  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.            
  19.             boolean willPlantsDieNextDay = false;
  20.  
  21.             plantsLeft.push(plants[0]);
  22.  
  23.             for (int i = 1; i < plants.length; i++) {
  24.                 int currentPlant = plants[i];
  25.                 int previousPlant = plants[i - 1];
  26.  
  27.                 if (currentPlant <= previousPlant) {
  28.                     int lastPlant = plantsLeft.peek();
  29.                     if (currentPlant > lastPlant) {
  30.                         willPlantsDieNextDay = true;
  31.                     }
  32.                     plantsLeft.push(currentPlant);
  33.                 }
  34.             }
  35.             day++;
  36.  
  37.             if (!willPlantsDieNextDay) {
  38.                 System.out.println(day);
  39.                 return;
  40.             }
  41.  
  42.             plants = new int[plantsLeft.size()];
  43.  
  44.             for (int i = plants.length - 1; i >= 0; i--) {
  45.                 plants[i] = plantsLeft.poll();
  46.             }
  47.  
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement