#P16318. [ICPC 2023 Jinan R] 彩虹子数组

[ICPC 2023 Jinan R] 彩虹子数组

Problem Description

"Rainbow Sequence" is a tense and exciting board game involving luck and auctions. Players can rely on luck to draw more cards, or use money to buy other cards. The goal is to form the longest possible number sequence using cards of each color. Next, we consider a problem related to this game.

:::align{center}

A photo taken by Instagram user @freethemeeple :::

Given a sequence of length nn, a1,a2,⋯ ,ana_1, a_2, \cdots, a_n, we call its contiguous subarray al,al+1,al+2,⋯ ,ara_l, a_{l + 1}, a_{l + 2}, \cdots, a_r a rainbow subarray if for all l≤i<rl \le i < r, it satisfies ai+1−ai=1a_{i + 1} - a_i = 1. In particular, a subarray of length 11 is always a rainbow subarray.

You can perform at most kk operations. In each operation, you can increase or decrease one element in the sequence by 11. Find the maximum possible length of the longest rainbow subarray after the operations.

Input Format

There are multiple test cases. The first line contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and kk (1≤n≤5×1051 \le n \le 5 \times 10^5, 0≤k≤10150 \le k \le 10^{15}), representing the length of the sequence and the maximum number of operations you may perform.

The second line contains nn integers a1,a2,⋯ ,ana_1, a_2, \cdots, a_n (1≤ai≤1091 \le a_i \le 10^9), representing the sequence.

It is guaranteed that the sum of nn over all test cases does not exceed 5×1055 \times 10^5.

Output Format

For each test case, output one integer per line, representing the maximum possible length of the longest rainbow subarray after performing at most kk operations.

5
7 5
7 2 5 5 4 11 7
6 0
100 3 4 5 99 100
5 6
1 1 1 1 1
5 50
100 200 300 400 500
1 100
3
4
3
5
1
1

Hint

For the first sample, we can perform 44 operations and change the sequence to {7,3,4,5,6,11,7}\{7, 3, 4, 5, 6, 11, 7\}. The longest rainbow subarray is {3,4,5,6}\{3, 4, 5, 6\}, so the answer is 44.

For the second sample, we cannot perform any operations. The longest rainbow subarray is {3,4,5}\{3, 4, 5\}, so the answer is 33.

For the third sample, we can perform 66 operations and change the sequence to {−1,0,1,2,3}\{-1, 0, 1, 2, 3\}. The entire sequence is a rainbow subarray, so the answer is 55.

Translated by ChatGPT 5