#P9474. [yLOI2022] 长安幻世绘
[yLOI2022] 长安幻世绘
Background
The bright moon over Chang'an is clear. Shadows remain, pipa songs go on, and the drinkers are half awake.
You ask, but cannot see clearly. Red gauze curtains, green silk ribbons, and the flying apsaras descend.
A beauty bends her slim waist, her jingling necklaces crisp. With a delicate hand she raises the pipa alone and kneels, her eyes still full of charm.
Musk touches her lips, and the wine becomes more intoxicating. In the drunken lantern light, the colors grow messier yet more beautiful.— Yin Lin, “Chang'an Fantasy Paintings”
Problem Description
There are colored lanterns arranged in a row from left to right, numbered from to . The brightness of lantern is . For , we say lantern and lantern are adjacent.
It is guaranteed that the brightness values of these lanterns are all distinct.
The harmony of a group of lanterns is defined as the difference between the maximum and minimum brightness among the lanterns in the group.
Fusu wants to choose non-adjacent lanterns from these lanterns as a group, and she wants the harmony of this group to be as small as possible. Please help her find this minimum value.
Formally, you need to select a subsequence of length with pairwise non-adjacent elements from the sequence (where all elements are distinct), such that the range of the subsequence is minimized.
Input Format
The first line contains two integers, representing the number of lanterns and the number of lanterns to pick , respectively.
The second line contains integers. The -th integer represents the brightness of lantern .
Output Format
Output one line with one integer, the answer.
5 3
1 2 3 4 5
4
6 3
1 7 8 3 4 6
4
见附加文件中的 D3.in
见附加文件中的 D3.ans
Hint
Explanation for Sample 1
You can only choose lanterns , because any other selection would cause some lanterns to be adjacent.
Explanation for Sample 2
You can choose lanterns . Their brightness values are , and the range is .
Constraints
- For of the testdata, .
- For of the testdata, .
- For another of the testdata, .
- For another of the testdata, is strictly increasing.
- For of the testdata, .
- For of the testdata, , , , and all are distinct.
Translated by ChatGPT 5