sunfresh

Jumps

Jan 15th, 2023
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. Jumps
  2. Given a sequence of elements(numbers), calculate the longest possible sequence of 'jumps' from each number.
  3.  
  4. Each 'jump' must be made according to the following rules:
  5.  
  6. You can only 'jump' on a number that is greater than the current one;
  7. You can 'jump' on a number, only if there isn't one with a greater value between;
  8. You can 'jump' only from left to right;
  9. Input
  10. Read from the standard input
  11.  
  12. On the first line, you will find the number N
  13. The number of elements
  14. On the second line you will find N numbers, separated by a space
  15. The elements themselves
  16. The input will be correct and in the described format, so there is no need to check it explicitly.
  17.  
  18. Output
  19. Print to the standard output
  20.  
  21. On the first line, print the length of the longest sequence of jumps
  22. On the second line, print the lengths of the sequences, starting from each element
  23. Constraints
  24. The N will always be less than 103344
  25. Sample Tests
  26. Input
  27. 6
  28. 1 4 2 6 3 4
  29. Output
  30. 2
  31. 2 1 1 0 1 0
  32. Explanation
  33. Element 1:
  34. 1 -> 4 -> 6 (2 jumps)
  35. Element 2:
  36. 4 -> 6 (1 jump)
  37. Element 3:
  38. 2 -> 6 (1 jump)
  39. Element 4:
  40. 6 (0 jumps)
  41. Element 5:
  42. 3 -> 4 (1 jump)
  43. Element 6:
  44. 4 -> (0 jumps)
  45. Input
  46. 5
  47. 1 1 1 1 1
  48. Output
  49. 0
  50. 0 0 0 0 0
  51.  
  52.  
  53.  
  54. import java.util.ArrayDeque;
  55. import java.util.Deque;
  56. import java.util.Scanner;
  57.  
  58. public class Main {
  59. public static void main(String[] args) {
  60. Scanner sc = new Scanner(System.in);
  61.  
  62. int n = Integer.parseInt(sc.nextLine());
  63.  
  64. Deque<Integer> queue = new ArrayDeque<>();
  65. int maxValue = Integer.MIN_VALUE;
  66. for (int i = 0; i < n; i++) {
  67. int currentNumber = sc.nextInt();
  68. maxValue = Math.max(maxValue, currentNumber);
  69. queue.offer(currentNumber);
  70. }
  71. StringBuilder result = new StringBuilder();
  72. int maxJumps = 0;
  73. while (!queue.isEmpty()) {
  74. int counter = 0;
  75. int storeHighest = queue.poll();//извлича и премахва
  76. for (int checkValue : queue) {
  77. if (checkValue > storeHighest) {
  78. counter++;
  79. storeHighest = checkValue;
  80. }
  81. if (checkValue == maxValue) break;
  82. }
  83. maxJumps = Math.max(counter, maxJumps);
  84. result.append(counter).append(" ");
  85. }
  86. result.insert(0, maxJumps + "\n");
  87. System.out.println(result);
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment