Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Jumps
- Given a sequence of elements(numbers), calculate the longest possible sequence of 'jumps' from each number.
- Each 'jump' must be made according to the following rules:
- You can only 'jump' on a number that is greater than the current one;
- You can 'jump' on a number, only if there isn't one with a greater value between;
- You can 'jump' only from left to right;
- Input
- Read from the standard input
- On the first line, you will find the number N
- The number of elements
- On the second line you will find N numbers, separated by a space
- The elements themselves
- The input will be correct and in the described format, so there is no need to check it explicitly.
- Output
- Print to the standard output
- On the first line, print the length of the longest sequence of jumps
- On the second line, print the lengths of the sequences, starting from each element
- Constraints
- The N will always be less than 103344
- Sample Tests
- Input
- 6
- 1 4 2 6 3 4
- Output
- 2
- 2 1 1 0 1 0
- Explanation
- Element 1:
- 1 -> 4 -> 6 (2 jumps)
- Element 2:
- 4 -> 6 (1 jump)
- Element 3:
- 2 -> 6 (1 jump)
- Element 4:
- 6 (0 jumps)
- Element 5:
- 3 -> 4 (1 jump)
- Element 6:
- 4 -> (0 jumps)
- Input
- 5
- 1 1 1 1 1
- Output
- 0
- 0 0 0 0 0
- import java.util.ArrayDeque;
- import java.util.Deque;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = Integer.parseInt(sc.nextLine());
- Deque<Integer> queue = new ArrayDeque<>();
- int maxValue = Integer.MIN_VALUE;
- for (int i = 0; i < n; i++) {
- int currentNumber = sc.nextInt();
- maxValue = Math.max(maxValue, currentNumber);
- queue.offer(currentNumber);
- }
- StringBuilder result = new StringBuilder();
- int maxJumps = 0;
- while (!queue.isEmpty()) {
- int counter = 0;
- int storeHighest = queue.poll();//извлича и премахва
- for (int checkValue : queue) {
- if (checkValue > storeHighest) {
- counter++;
- storeHighest = checkValue;
- }
- if (checkValue == maxValue) break;
- }
- maxJumps = Math.max(counter, maxJumps);
- result.append(counter).append(" ");
- }
- result.insert(0, maxJumps + "\n");
- System.out.println(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment