Under construction

Still being built. Things may change

Decryption

Time limit1sMemory limit512 MB

Summary
Split the sequence into the fewest non-overlapping segments where each segment's first element is its minimum and last element is its maximum.
Level

Hard8 of 10

Topics
Greedy, Stack, Array, Prefix sum
Solved
No attempts yet

Problem

It is known that if you keep the first and last letters of each word in a text and rearrange the remaining letters arbitrarily, the resulting text can still be read fairly easily. A computer science laboratory studies an analogous phenomenon for number sequences.

A sequence of positive integers is called correct if its first number is the minimum and its last number is the maximum. For example, the sequences [1,3,2,4][1, 3, 2, 4] and [1,2,1,2][1, 2, 1, 2] are correct, while the sequence [1,3,2][1, 3, 2] is not.

A sequence [a1,a2,…,an][a_1, a_2, \ldots, a_n] is given. A segment of consecutive elements [al,al+1,…,ar][a_l, a_{l+1}, \ldots, a_r] of the given sequence is called correct if it forms a correct sequence: ala_l is the minimum on this segment and ara_r is the maximum.

As part of the study, the given sequence must be split into the minimum number of non-overlapping correct segments. For example, the sequence [2,3,1,1,5,1][2, 3, 1, 1, 5, 1] can be split into three correct segments: [2,3][2, 3], [1,1,5][1, 1, 5], and [1][1].

Write a program that, given a sequence, determines the minimum number of correct segments it can be split into.

Input

The first line contains the integer nn, the number of elements in the given sequence (1≤n≤300 0001 \le n \le 300\,000).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n, the given sequence (1≤ai≤1091 \le a_i \le 10^9).

Output

Print a single number: the minimum number of correct segments into which the given sequence can be split.

Examples3

  1. Example 1

    Input
    5
    5 4 3 2 1
    
    Expected output
    5
    
  2. Example 2

    Input
    4
    1 3 2 4
    
    Expected output
    1
    
  3. Example 3

    Input
    6
    2 3 1 1 5 1
    
    Expected output
    3