#P1799. Maximizing Fixed Points in a Subsequence
Maximizing Fixed Points in a Subsequence
Maximizing Fixed Points in a Subsequence
In this problem, you are given a sequence of integers. You are allowed to remove some elements (possibly none) from the sequence. Then, the remaining elements are compressed into a new sequence by keeping their original order. In the new sequence, an element is called a fixed point if its value is equal to its position (positions are numbered from 1).
For example, consider the sequence \(1,\;1,\;2,\;5,\;4\). If you remove the element at the second position, the remaining sequence becomes \(1,\;2,\;5,\;4\) when compressed. In this new sequence, the elements at positions 1, 2, and 4 are fixed points because \(1=1,\;2=2,\;4=4\) (note that the element 5 at position 3 is not a fixed point since \(5\neq3\)). Thus, the number of fixed points in this case is 3.
Your task is to determine the maximum possible number of fixed points you can achieve by removing an appropriate subset of the given sequence.
Note: The order of elements cannot be changed apart from removing some elements. Once removed, the remaining elements are reindexed consecutively starting from 1.
inputFormat
The first line contains a single integer \(n\) (\(1 \le n \le 1000\)), the number of elements in the sequence.
The second line contains \(n\) space‐separated integers \(a_1, a_2, \ldots, a_n\) (each \(a_i\) is in the range of a 32-bit signed integer).
outputFormat
Output a single integer --- the maximum number of fixed points that can be achieved by removing some elements from the sequence.
sample
5
1 1 2 5 4
3