Under construction

Still being built. Things may change

Nayeon Sort

Interview

Time limit2sMemory limit1024 MB

Summary
Given an array, find the minimum number of stacks needed to sort it in ascending order using Nayeon sort.
Level

Medium5 of 10

Topics
Array, Binary search, Dynamic programming
Solved
No attempts yet

Problem

Nayeon, who studied hard in her data structures class, devised a new sorting algorithm and named it Nayeon sort after herself. Given an array A=[a1,a2,⋯ ,aN]A=[a_1,a_2,\cdots,a_N] to sort, Nayeon sort works as follows.

  1. Declare as many empty stacks as you want.
  2. Insert each element from a1a_1 to aNa_N, in order, into any stack you choose.
  3. Declare an empty array BB.
  4. Repeat until every stack is empty: pop one element from any stack you choose and append it to the end of BB.

Whether Nayeon sort can sort the array depends on the array and on how many stacks you use. For example, if A=[9,2,8,7,1]A=[9,2,8,7,1], put [9,8][9,8], [2][2], and [7,1][7,1] into three stacks, then pop from the third, second, third, first, and first stacks in that order. The result is the array sorted in ascending order. Two stacks are enough to sort AA, but with only one stack, AA cannot be sorted in ascending order.

Nayeon offers to secretly add one problem to your HCPC solve count if you find the minimum number of stacks needed to sort the given array in ascending order. Solve it before anyone else and get one step closer to winning HCPC!

Input

The first line contains the length NN of the array AA. (1≤N≤500,0001 \le N \le 500,000)

The second line contains NN integers a1,a2,⋯ ,aNa_1, a_2, \cdots, a_N, separated by spaces. (1≤ai≤1091 \le a_i \le 10^9)

Output

On the first line, print the minimum number of stacks needed to sort AA in ascending order.

Examples2

  1. Example 1

    Input
    5
    9 2 8 7 1
    
    Expected output
    2
    
  2. Example 2

    Input
    10
    10 9 8 7 6 5 4 3 2 1
    
    Expected output
    1