#P4378. [USACO18OPEN] Out of Sorts S

[USACO18OPEN] Out of Sorts S

Description

With an eye on the possibility of a long-term career off the farm, the cow Bessie has started learning algorithms on various online programming sites.

Her favorite algorithm so far is bubble sort. This is Bessie's cow-code implementation for sorting an array AA of length NN.

sorted = false
while (not sorted):
   sorted = true
   moo
   for i = 0 to N-2:
      if A[i+1] < A[i]:
         swap A[i], A[i+1]
         sorted = false

Obviously, the 'moo' instruction in the cow-code simply prints 'moo'. Strangely, Bessie seems fixated on using this statement at different places in her code.

Given an input array, predict how many times Bessie's code will print 'moo'.

Input Format

The first line contains NN (1N100,0001 \leq N \leq 100{,}000). The next NN lines describe A[0]A[N1]A[0] \ldots A[N-1], each an integer in the range 01090 \ldots 10^9. The values are not guaranteed to be distinct.

Output Format

Output the number of times 'moo' is printed.

5
1
5
3
8
2
4

Hint

Setter: Brian Dean.

Translated by ChatGPT 5