#P10392. [蓝桥杯 2024 省 A] 封印宝石
[蓝桥杯 2024 省 A] 封印宝石
Problem Description
During an adventure, the hero Xiao Lan found gems shining with a strange light. Each gem contains magical energy, denoted as . Xiao Lan plans to use special magic boxes to seal these gems, to prevent their magical energy from being misused.
Sealing gems consumes Xiao Lan’s stamina. Specifically, putting the -th gem into the -th box costs Xiao Lan stamina points (note: to put the -th gem into the -th box for a valid seal, it must satisfy ). Xiao Lan may also choose to leave boxes empty, saving stamina for later use.
In addition, to avoid magic conflicts, each box can hold at most one gem (and each gem can be put into only one box), and any two adjacent boxes cannot contain gems with the same magic value. Adjacent boxes are allowed to be empty at the same time.
Xiao Lan’s initial stamina is . Without exceeding the stamina limit, Xiao Lan wants to find a placement method such that the sequence of gem magic values in these boxes has the maximum lexicographical order (note: an empty box is recorded as in this sequence).
As a follower of Xiao Lan, please help him find this placement method.
Explanation of lexicographical order: In this problem, lexicographical order is compared by gem magic values. For two magic value sequences and of the same length , if there exists a position such that holds for all , but , then sequence is lexicographically smaller than sequence .
Conversely, if , then sequence is lexicographically larger than sequence . If no such exists, then and are lexicographically equal.
Input Format
The first line contains two integers and , separated by one space, representing the number of gems and Xiao Lan’s initial stamina.
The second line contains integers , separated by one space, representing the magical energy value of each gem.
Output Format
Output one line containing integers, separated by one space, representing the magical energy value of the gem in each magic box. If a box is empty, output at the corresponding position.
3 3
1 3 2
3 2 -1
Hint
Before starting to place gems, the stamina is , and the arrangement of gems in the boxes is .
- Put the -nd gem into the -st box, obtaining , with remaining stamina .
- Put the -rd gem into the -nd box, obtaining , with remaining stamina .
Finally, the arrangement of gems in the boxes is . Clearly, there is no better placement method than this.
Constraints for of the testdata: , , .
Constraints for all testdata: , , .
Translated by ChatGPT 5