Guest User

Untitled

a guest
Jan 18th, 2020
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.sql.Array;
  5. import java.util.*;
  6. import java.util.stream.*;
  7. import java.util.stream.Collectors;
  8.  
  9. import static java.lang.Character.isLetterOrDigit;
  10.  
  11. public class firstStQexercise {
  12.  
  13.     public static void main(String[] args) throws IOException {
  14.         Scanner scanner = new Scanner(System.in);
  15.  
  16.         int plantNumber = Integer.parseInt(scanner.nextLine());
  17.  
  18.         List <Integer> plantsPesticides;
  19.  
  20.         plantsPesticides = Arrays.stream(scanner.nextLine().
  21.                 split("\\s+")).map(Integer::parseInt).collect(Collectors.toList());
  22.  
  23.         ArrayDeque<Integer> plantsToRemoveIndexesQueue = new ArrayDeque<>();
  24.  
  25.         int counter = 0;
  26.         while (plantsPesticides.size() > 1) {
  27.             for (int i = 1; i < plantsPesticides.size(); i++) {
  28.                 int first = plantsPesticides.get(i);
  29.                 int second = plantsPesticides.get(i - 1);
  30.                 if (first > second) {
  31.                     plantsToRemoveIndexesQueue.offer(i);
  32.                 }
  33.             }
  34.             if (plantsToRemoveIndexesQueue.isEmpty()){
  35.                 System.out.println(counter);
  36.                 return;
  37.             }
  38.  
  39.             int removedCounter = 0;
  40.             while (!plantsToRemoveIndexesQueue.isEmpty()){
  41.                 int indexToRemove = plantsToRemoveIndexesQueue.poll() - removedCounter;
  42.                 plantsPesticides.remove(indexToRemove);
  43.                 removedCounter++;
  44.             }
  45.  
  46.             counter++;
  47.         }
  48.  
  49.         System.out.println(counter);
  50.  
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment