Greedily Increasing Subsequence
InterviewTime limit1sMemory limit512 MB
Given a permutation, repeatedly take the leftmost unused element larger than the previous pick, and report the resulting subsequence.
- Level
Medium4 of 10
- Topics
- Array, Simulation, Greedy, Implementation
- Solved
- No attempts yet
Problem
Given a permutation of the integers , the greedily increasing subsequence (GIS) is defined as follows.
Let . For every , let be the leftmost integer in that is strictly larger than . If no such integer exists for some , the GIS of the sequence is .
Given a permutation , compute the GIS of .
Input
The first line of input contains an integer , the number of elements of the permutation . The next line contains distinct integers between and , the elements of the permutation .
Output
First, output a line containing the length of the GIS of . Then output integers, the elements of the GIS in order.