1 条题解
-
0
练58.1 严格排名
解题思路
数组升序排序,使用unique去掉相邻重复值,再访问下标k-1。
正确性说明
按顺序完成所述运算后,每个输入元素都被处理在相应位置或统计项中。保存的中间状态与已处理数据对应,最终输出与题目定义一致。
易错点
严格第k小按不同数值排名,不按原数组位置。
复杂度
时间O(n log n),空间O(n)。
参考代码(C++11)
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector<long long> a(n); for (auto &x : a) cin >> x; sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); cout << a[k - 1] << "\n"; return 0; }
- 1