#K76067. Longest Contiguous Increasing Subsequence After One Modification

    ID: 34560 Type: Default 1000ms 256MiB

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 length 5.
  • For n = 6 and the sequence [10, 9, 8, 7, 6, 5], the maximum length obtainable is 2.
  • For n = 4 and the sequence [4, 3, 2, 4], the maximum length obtainable is 3.

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.

## sample
5
1 2 5 3 4
5