405108: GYM101798 C Forest (A) - Egg
Description
The memory limit for this problem is 4 MB.
In computer science, a tree is a connected graph with no cycles. Each node in a tree has exactly one parent, except for the root, which doesn't have a parent.
A forest is a graph that consists of one or more trees.
We introduce the following method to generate a forest of n nodes:
- The input for the method is an array A of n distinct integers.
- Edges are generated in the following way: the parent of node i is j, where j is the maximum index such that j < i and Aj > Ai. If such index doesn't exist, then node i has no parent.
Note that the generated trees are rooted.
Given an array of n distinct integers, find the number of trees in the forest generated using this array.
InputThe first line of input contains a single integer n (1 ≤ n ≤ 2 × 106), the size of the array.
The second line contains n distinct integers A1, A2, ..., An (1 ≤ Ai ≤ n), representing the values of the array.
As the memory limit for this problem is 4 MB, you need to solve it without storing the array.
OutputPrint a single integer that represents the number of trees in the generated forest.
ExamplesInput3Output
2 1 3
2Input
5Output
5 4 3 2 1
1Input
1Output
1
1