Refact.ai Match 1 (Codeforces Round 985)
C
New Rating
Codeforces
1700
原题
← BD →
翻译中…

Hello, Codeforces Forcescode!

翻译中…

Kevin used to be a participant of Codeforces. Recently, the KDOI Team has developed a new Online Judge called Forcescode.

翻译中…

Kevin has participated in $n$ contests on Forcescode. In the $i$-th contest, his performance rating is $a_i$.

翻译中…

Now he has hacked into the backend of Forcescode and will select an interval $[l,r]$ ($1\le l\le r\le n$), then skip all of the contests in this interval. After that, his rating will be recalculated in the following way:

翻译中…
  • Initially, his rating is $x=0$;
  • For each $1\le i\le n$, after the $i$-th contest,
  • If $l\le i\le r$, this contest will be skipped, and the rating will remain unchanged;
    • Otherwise, his rating will be updated according to the following rules:
    • If $a_i \gt x$, his rating $x$ will increase by $1$;
      • If $a_i=x$, his rating $x$ will remain unchanged;
      • If $a_i \lt x$, his rating $x$ will decrease by $1$.
翻译中…

You have to help Kevin to find his maximum possible rating after the recalculation if he chooses the interval $[l,r]$ optimally. Note that Kevin has to skip at least one contest.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 5\cdot 10^4$) — the number of test cases. The description of test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($1\le n\le 3\cdot 10^5$) — the number of contests.

翻译中…

The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$) — the performance ratings in the contests.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer — the maximum possible rating after the recalculation if Kevin chooses the interval optimally.

翻译中…

Note

翻译中…

In the first test case, Kevin must skip at least one contest. If he chooses any interval of length $1$, his rating after the recalculation will be equal to $5$.

翻译中…

In the second test case, Kevin's optimal choice is to select the interval $[3,5]$. During the recalculation, his rating changes as follows:

翻译中…

$$ 0 \xrightarrow{a_1=1} 1 \xrightarrow{a_2=2} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{\mathtt{skip}} 2 \xrightarrow{a_6=3} 3 \xrightarrow{a_7=4} 4 $$

翻译中…

In the third test case, Kevin must skip the only contest, so his rating will remain at the initial value of $0$.

翻译中…

In the fourth test case, Kevin's optimal choice is to select the interval $[7,9]$. During the recalculation, his rating changes as follows:

翻译中…

$$ 0 \xrightarrow{a_1=9} 1 \xrightarrow{a_2=9} 2 \xrightarrow{a_3=8} 3 \xrightarrow{a_4=2} 2 \xrightarrow{a_5=4} 3 \xrightarrow{a_6=4} 4 \xrightarrow{\mathtt{skip}} 4 \xrightarrow{\mathtt{skip}} 4 \xrightarrow{\mathtt{skip}} 4 $$

翻译中…

In the fifth test case, Kevin's optimal choice is to select the interval $[5,9]$.

翻译中…

样例

Input #1
5
6
1 2 3 4 5 6
7
1 2 1 1 1 3 4
1
1
9
9 9 8 2 4 4 3 5 3
10
1 2 3 4 1 3 2 1 1 10
Output #1
5
4
0
4
5
InputCopy OutputCopy
题目信息
题号
2029C
时间限制
2 s
内存限制
512 MB
标签
二分
数据结构
动态规划
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。