cswork / OA打开练习工作台

MicrosoftOA #59

Retain Top K Values

估计简单可在线判题

考点(AI 标注)原地数组单调栈与堆

Select the K largest elements of the array and output them in their original relative order. When equal values straddle the cutoff, prefer the ones that appear earliest, so that exactly K are chosen. These rules are stated in full in the source problem's two example explanations.

This site uses a standard input/output protocol.

输入格式

The first line contains N K, and the second line contains N integers. The source only gives 0≤K≤N; this site uses 1≤N≤100000 and elements in −10⁹..10⁹.

输出格式

Output exactly K values in their original relative order; output an empty line when K is 0.

样例 1

输入:

5 3
5 1 3 5 2

输出:

5 3 5

样例 2

输入:

4 2
4 4 4 2

输出:

4 4

样例 3

输入:

2 0
1 2

输出:

样例说明

Example 1: choose indices 0, 2, 3 and output them in original order as 5 3 5. Example 2: take the earliest two of the three 4s and output 4 4. Example 3: K is 0, so output an empty line.