Advertisement
damesova

Poisonous Plants [Mimi]{JA]

May 20th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 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.  
  6. public class _10_PoisonousPlants {
  7.  
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11.         int n = Integer.parseInt(reader.readLine());
  12.         String[] plants = reader.readLine().split("\\s+");
  13.         ArrayDeque<Integer> s = new ArrayDeque<>();
  14.         s.push(0);
  15.  
  16.         int[] days = new int[n];
  17.  
  18.         for (int i = 0; i < n; i++) {
  19.             int maxDays = 0;
  20.  
  21.             while (!s.isEmpty() && Integer.parseInt(plants[s.peek()]) >= Integer.parseInt(plants[i])) {
  22.                 maxDays = Math.max(maxDays, days[s.pop()]);
  23.             }
  24.  
  25.             if (!s.isEmpty()) {
  26.                 days[i] = maxDays + 1;
  27.             }
  28.  
  29.             s.push(i);
  30.         }
  31.         System.out.println(max(days));
  32.     }
  33.  
  34.     private static int max(int[] days) {
  35.         int max = Integer.MIN_VALUE;
  36.         for (int day : days) {
  37.             if (day > max) {
  38.                 max = day;
  39.             }
  40.         }
  41.         return max;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement