#P9842. [ICPC2021 Nanjing R] Klee in Solitary Confinement

[ICPC2021 Nanjing R] Klee in Solitary Confinement

题目描述

Since the traveler comes, People in Monstadt suddenly raise great interest in computer programming and algorithms, including Klee, the Spark Knight of the Knights of Favonius.

Being sent to solitary confinement by Jean again, Klee decides to spend time learning the famous Mo's algorithm, which can compute with a time complexity of O(n1.5)\mathcal{O}(n^{1.5}) for some range query problem without modifications.

To check whether Klee has truly mastered the algorithm (or in fact making another bombs secretly), Jean gives her a problem of an integer sequence a1,a2,,ana_1, a_2, \cdots, a_n along with some queries [li,ri][l_i, r_i] requiring her to find the mode number in the contiguous subsequence ali,ali+1,,aria_{l_i}, a_{l_i + 1}, \cdots, a_{r_i}. The mode number is the most common number (that is to say, the number which appears the maximum number of times) in the subsequence.

With the help of Mo's algorithm, Klee solves that problem without effort, but another problem comes into her mind. Given an integer sequence a1,a2,,ana_1, a_2, \cdots, a_n of length nn and an integer kk, you can perform the following operation at most once: Choose two integers ll and rr such that 1lrn1 \le l \le r \le n and add kk to every aia_i where lirl \le i \le r. Note that it is OK not to perform this operation. Compute the maximum occurrence of the mode number of the whole sequence if you choose to perform (or not perform) the operation optimally.

输入格式

There is only one test case in each test file.

The first line of the input contains two integers nn and kk (1n1061 \le n \le 10^6, 106k106-10^6 \le k \le 10^6) indicating the length of the sequence and the additive number.

The second line of the input contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n (106ai106-10^6 \le a_i \le 10^6) indicating the original sequence.

输出格式

Output one line containing one integer indicating the maximum occurrence of the mode number of the whole sequence after performing (or not performing) the operation.

题目大意

给定 n,kn,k 和一个长为 nn 的序列,你可以选择对区间 [l,r][l, r] 的数整体加上 kk,也可以不加。最大化众数出现次数并输出。

5 2
2 2 4 4 4

5

7 1
3 2 3 2 2 2 3

6

7 1
2 3 2 3 2 3 3

5

9 -100
-1 -2 1 2 -1 -2 1 -2 1

3

提示

For the first sample test case, choose l=1l = 1 and r=2r = 2 and we'll result in the sequence {4,4,4,4,4}\{4, 4, 4, 4, 4\}. The mode number is obviously 44 which appears 55 times.

For the second sample test case, choose l=4l = 4 and r=6r = 6 and we'll result in the sequence {3,2,3,3,3,3,3}\{3, 2, 3, 3, 3, 3, 3\}. The mode number is 33 which appears 66 times.

For the fourth sample test case, choose not to perform the operation. The mode number is 11 and 2-2 which both appear 33 times.