#C3724. Minimum Subsequences to Sort

    ID: 47183 Type: Default 1000ms 256MiB

Minimum Subsequences to Sort

Minimum Subsequences to Sort

You are given a sequence of N integers which represent the lengths of strings of lights. The goal is to determine the minimum number of contiguous subsequences that must be individually sorted such that when they are concatenated, the entire sequence becomes sorted in non-decreasing order.

More formally, you are given an array \(A = [a_1, a_2, \dots, a_N]\). You must count the number of indices \(i\) (with \(2 \le i \le N\)) where \(a_i < a_{i-1}\). The answer is \(1 + \) (the number of such transitions). That is, if \(T\) is the number of positions where \(a_i < a_{i-1}\), then the minimum number of subsequences needed is \(T+1\).

Example:

Input: N = 7, lengths = [5, 2, 3, 6, 7, 2, 8]
Output: 3

Explanation: The transitions occur between 5->2 and 7->2, thus 2 transitions result in 3 contiguous subsequences.

</p>

inputFormat

The first line of the input contains a single integer \(N\) \( (1 \le N \le 10^5)\) representing the number of strings.

The second line contains \(N\) space-separated integers \(a_1, a_2, \dots, a_N\) (\(1 \le a_i \le 10^9\)) representing the lengths of the strings of lights.

outputFormat

Output a single integer which is the minimum number of contiguous subsequences that need to be sorted individually so that the entire sequence becomes sorted in non-decreasing order.

## sample
7
5 2 3 6 7 2 8
3

</p>