AmazonOA #288
Min Operations to Sort All Packages
估计中等可在线判题
The input is a permutation of 1..n. Initially the next number to collect is 1. In each pass you scan from start to end; whenever you meet the next number, you collect it and increase the target by 1, skipping everything else. Find how many passes it takes to collect 1..n. The real operation is restored from the explicit formula sortingSequence[i]==arrangedCount+1 in the original statement; the introduction is not misread as describing a different target permutation.
输入格式
The first line contains n and the second line contains sortingSequence. On this site 1≤n≤200000, and the input is guaranteed to be a permutation of 1..n.
输出格式
Output a single integer, the answer.
样例 1
输入:
4
2 4 1 3
输出:
3
样例 2
输入:
3
1 2 3
输出:
1
样例 3
输入:
3
3 2 1
输出:
3
样例说明
The answers are 3, 1 and 3 respectively. In the first example the three passes collect 1; 2 and 3; and 4. You cannot simply count the descents between adjacent array elements.