#C3227. Book Return Sequence Validation
Book Return Sequence Validation
Book Return Sequence Validation
In a library system, books are returned one by one. The librarian expects that every book x (where x > 1) can only be returned if its predecessor (x - 1) has already been returned. Given the total number of books n and a sequence in which they are returned, determine whether this book return sequence is valid.
The sequence is valid if for each book in the sequence, whenever the book number is greater than 1, its previous book (i.e., the book with number one less) has already appeared earlier in the sequence. Otherwise, the sequence is invalid.
Note: All formulas are presented in LaTeX format. For example, the condition can be written as: \( \text{if } x > 1, \text{ then book } (x-1) \text{ must appear before } x \).
inputFormat
The input is read from standard input (stdin). The first line contains a single integer n \( (1 \leq n \leq 10^5) \) representing the number of books. The second line contains n space-separated integers representing the sequence in which the books are returned.
outputFormat
Output a single line to standard output (stdout): "YES" if the sequence is valid, and "NO" otherwise.
## sample5
1 2 3 4 5
YES
</p>