Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <vector>
- int KStat(std::vector<int>& v, int k) {
- int l = 0, r = v.size();
- while (true) {
- std::swap(v[(l + r) / 2], v[r - 1]);
- const int& x = v[r - 1];
- const auto i = std::partition(v.begin() + l, v.begin() + r - 1, [&x](const int& e) -> bool {
- return e < x;
- });
- std::swap(*i, v[r - 1]);
- const int p = i - v.begin();
- if (p == k) return v[k];
- if (p > k) {
- r = p;
- } else {
- l = p + 1;
- }
- }
- }
- int main() {
- int n = 0, k = 0;
- std::cin >> n >> k;
- std::vector<int> v(n, 0);
- for (int i = 0; i < n; ++i) {
- std::cin >> v[i];
- }
- std::cout << KStat(v, k);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement