#SRD0000137. Sắp xếp các đống cỏ khô (Minimizing Haybales)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sắp xếp các đống cỏ khô (Minimizing Haybales)
Minimizing Haybales
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
Farmer John has stacks of haybales arranged in a row. The -th stack has height haybales. Bessie may rearrange the stacks by using the following operation:
Choose two adjacent stacks with heights . If , the stacks may be swapped; otherwise that swap is forbidden.
She may perform any number of valid swaps, including zero. Among all reachable sequences of stack heights, find the lexicographically smallest one: the sequence with the smaller value at the first position where two sequences differ.
Stacks of equal height are still separate occurrences in the initial sequence; the output contains only their heights.
Input
- The first line contains .
- Each of the next lines contains one height , in the original left-to-right order.
Output
Print exactly lines. Line contains the height at position in the lexicographically smallest reachable sequence.
Subtasks
- Subtask 1 (10 points): ; ; for every .
- Subtask 2 (20 points): ; ; for every .
- Subtask 3 (70 points): ; ; for every .
Examples
Example 1
Input
5 3
7
7
3
6
2
Output
6
7
7
2
3
Explanation
Initially the stacks are 7 7 3 6 2, with . A valid sequence is 7 7 3 6 2 7 7 6 3 2 7 7 6 2 3 7 6 7 2 3 6 7 7 2 3. Height cannot cross either height- stack because their difference is . Height cannot cross a preceding height- stack either. Therefore is the smallest possible first value, and the remaining choices yield the displayed lexicographic minimum.
Example 2
Input
4 1
5
1
3
2
Output
5
1
2
3
Explanation
Stacks and differ by one, so they may be swapped to obtain 5 1 2 3. Height cannot cross height because they differ by four, and height cannot cross height because they differ by two. Thus the first two heights remain , while 2 3 is the best order for the last two.