StormFalcon32

Dishes

Mar 27th, 2020
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.util.LinkedList;
  8.  
  9. public class Dishwashing {
  10.  
  11. public static void main(String[] args) throws IOException {
  12. BufferedReader in = new BufferedReader(new FileReader("dishes.in"));
  13. PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("dishes.out")));
  14. int N = Integer.parseInt(in.readLine());
  15. int[] plates = new int[N];
  16. int[] placeStack = new int[N + 1];
  17. // placeStack[i] is the index of the soapy stack which i should be placed in
  18. @SuppressWarnings("unchecked")
  19. LinkedList<Integer>[] soapy = new LinkedList[N + 1];
  20. int maxPlaced = -1;
  21. for (int i = 0; i < N; i++) {
  22. plates[i] = Integer.parseInt(in.readLine());
  23. soapy[i] = new LinkedList<Integer>();
  24. }
  25. soapy[N] = new LinkedList<Integer>();
  26. int i = 0;
  27. for (i = 0; i < N; i++) {
  28. int toPlace = plates[i];
  29. if (toPlace < maxPlaced) {
  30. break;
  31. }
  32. for (int j = toPlace; j >= 0 && placeStack[j] == 0; j--) {
  33. placeStack[j] = toPlace;
  34. }
  35. LinkedList<Integer> curr = soapy[placeStack[toPlace]];
  36. while (!curr.isEmpty() && curr.getLast() < toPlace) {
  37. maxPlaced = Math.max(maxPlaced, curr.getLast());
  38. curr.removeLast();
  39. }
  40. curr.add(toPlace);
  41. }
  42. out.println(i);
  43. in.close();
  44. out.close();
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment