#P16796. [蓝桥杯 2026 国 B] 灯带修补

    ID: 19137 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度普及 上传者: 标签>二分前缀和2026双指针 two-pointer蓝桥杯国赛

[蓝桥杯 2026 国 B] 灯带修补

Problem Description

Xiaolan has a circular LED strip. There are NN LEDs on the strip in clockwise order, and the brightness of the ii-th LED is AiA_i.

If the absolute difference in brightness between two adjacent LEDs is greater than KK, then this adjacent pair is called unstable. Since the strip is circular, the NN-th LED and the 11-st LED are also adjacent.

Xiaolan may first choose a cut between any two adjacent LEDs to open the circular strip into a line. Then, he will choose a consecutive segment of LEDs in this line to display.

If the chosen display segment contains LL LEDs, then there are L1L-1 adjacent pairs inside the segment that need to be checked. Xiaolan can repair at most MM unstable adjacent pairs among them. The display segment is valid if and only if the number of unstable adjacent pairs inside the segment does not exceed MM.

Please compute, when the cut and the display segment can be chosen freely, the maximum number of consecutive LEDs Xiaolan can display.

Input Format

The first line contains three integers N,M,KN, M, K, representing the number of LEDs, the maximum number of unstable adjacent pairs that can be repaired, and the threshold for stable brightness difference.

The second line contains NN integers A1,A2,,ANA_1, A_2, \dots, A_N, where AiA_i is the brightness of the ii-th LED.

Output Format

Output one line containing one integer, which is the maximum number of consecutive LEDs that can be selected.

6 1 3
4 6 10 13 30 31
4
5 0 2
1 10 20 30 40
1
4 3 0
5 100 5 100
4

Hint

Sample Explanation 1

You can cut between the 66-th and the 11-st LED. After opening it, choose LEDs 11 to 44, with brightness values 4,6,10,134, 6, 10, 13 in order.

There are 33 adjacent pairs in this segment: 44 and 66 are stable, 66 and 1010 are unstable, and 1010 and 1313 are stable. After repairing the pair 66 and 1010, you can display 44 consecutive LEDs.

For any segment of 55 consecutive LEDs, the segment will contain at least 22 unstable adjacent pairs, which exceeds M=1M = 1, so the answer is 44.

Sample Explanation 2

As long as the display segment length is at least 22, an unstable adjacent pair will appear in the segment. Since M=0M = 0, no unstable adjacent pairs can be repaired, so at most one LED can be displayed.

Sample Explanation 3

By choosing a suitable cut, you can display all 44 LEDs. After opening it, there are only 33 adjacent pairs in the segment that need to be checked. They are all unstable, but they can all be repaired, so the answer is 44.

Constraints and Notes for Test Cases

For 30%30\% of the test cases, 1N2001 \le N \le 200.

For 60%60\% of the test cases, 1N50001 \le N \le 5000.

For all test cases, 1N2×1051 \le N \le 2 \times 10^5, 0MN10 \le M \le N-1, 0K1090 \le K \le 10^9, 1Ai1091 \le A_i \le 10^9.

Translated by ChatGPT 5