题解 | #查找第K小数#优先级队列
查找第K小数
https://www.nowcoder.com/practice/204dfa6fcbc8478f993d23f693189ffd
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> #include <map> #include <queue> #include <cmath> using namespace std; int main() { int n; int k; priority_queue<int> que; while (scanf("%d", &n) != EOF) { while (!que.empty()) { que.pop(); } for (int i = 0; i < n; i++) { int t; scanf("%d", &t); que.push(-t); //底层是一个大根堆,所以存相反数变成小根堆 } scanf("%d", &k); int i = 1; int pre; while (i != k) { pre = que.top(); while (pre == que.top()) { //防止有重复的 que.pop(); } i++; } printf("%d\n", -que.top()); } }