Cod sursa(job #2921700)

Utilizator UnknownPercentageBuca Mihnea-Vicentiu UnknownPercentage Data 1 septembrie 2022 14:37:45
Problema Statistici de ordine Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.95 kb
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define vt vector
#define pb push_back
#define em emplace
#define emb emplace_back

#define all(x) x.begin(), x.end()
#define all1(x) x.begin() + 1, x.end()
#define sz(x) (int)(x).size()

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <class T> void re(vt <T>& x);
template <class T> void re(T& x) {
    cin >> x;
}

template <class H, class... T> void re(H &x, T&... y) {
    re(x); re(y...);
}

template <class T> void re(vt <T>& x) {
    for(auto& it : x)
        re(it);
}

template <class T> void wr(T x) {
    cout << x;
}

template <class H, class ...T> void wr(H x, T... y) {
    wr(x); wr(y...);
}

inline void Open(const string Name) {
    #ifndef ONLINE_JUDGE
        (void)!freopen((Name + ".in").c_str(), "r", stdin);
        (void)!freopen((Name + ".out").c_str(), "w", stdout);
    #endif
}

void solve() {
    int n, k; re(n, k);
    vt <int> v(n + 1);
    for(int i = 1;i <= n;i++)
        re(v[i]);

    auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
    mt19937 mt(seed);    
    auto kth = [&](auto&& kth, int l, int r, int k) -> int {
        if(l == r) {
            return v[l];
        }

        uniform_int_distribution<> distr(l, r);
        int val = v[distr(mt)], i = l, j = r;
        while(i <= j) {
            while(val > v[i]) i++;
            while(val < v[j]) j--;
            if(i <= j) {
                swap(v[i++], v[j--]);
            }
        }

        if(k <= j) return kth(kth, l, j, k);
        return kth(kth, j + 1, r, k);
    };

    wr(kth(kth, 1, n, k));
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    Open("sdo");

    int t = 1;
    for(;t;t--) {
        solve();
    }

    return 0;
}