#K76067. Longest Contiguous Increasing Subsequence After One Modification
Longest Contiguous Increasing Subsequence After One Modification
Longest Contiguous Increasing Subsequence After One Modification
You are given a sequence of n integers. You are allowed to perform exactly one modification: choose any element and replace it with any integer of your choice. Your task is to maximize the length of the longest contiguous strictly increasing subsequence after the modification.
More formally, let the sequence be \(a_0, a_1, \dots, a_{n-1}\). You can change exactly one \(a_i\) to any integer. Determine the largest value of \(L\) such that there exists a contiguous segment \(a_l, a_{l+1}, \dots, a_r\) (with \(r-l+1 = L\)) satisfying:
[ a_{l} < a_{l+1} < \cdots < a_{r} ]
It is guaranteed that the input sequence has at least one element. Note that the answer may exceed n if the modification bridges two increasing subsequences.
Example:
- For
n = 5
and the sequence[1, 2, 5, 3, 4]
, one optimal modification yields a contiguous increasing subsequence of length5
. - For
n = 6
and the sequence[10, 9, 8, 7, 6, 5]
, the maximum length obtainable is2
. - For
n = 4
and the sequence[4, 3, 2, 4]
, the maximum length obtainable is3
.
inputFormat
The input is provided via stdin and has the following format:
n a1 a2 ... an
Where the first line contains a single integer n representing the number of elements. The second line contains n space-separated integers representing the sequence.
outputFormat
Output a single integer via stdout — the maximum length of the longest contiguous strictly increasing subsequence obtainable after exactly one modification.
## sample5
1 2 5 3 4
5