Cod sursa(job #3342746)

Utilizator Radu_BicliBiclineru Radu Radu_Bicli Data 25 februarie 2026 15:26:47
Problema Statistici de ordine Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;

#define USE_STD_IO 0
#if USE_STD_IO
    #define fin cin
    #define fout cout
#else
    ifstream fin("sdo.in");
    ofstream fout("sdo.out");
#endif

int n, k, i, a[3000002];

static inline int Pivot(int st, int dr) {
    int piv = st + (dr - st) / 2;

    if(a[st] > a[piv]) swap(a[st], a[piv]);
    if(a[piv] > a[dr]) swap(a[piv], a[dr]);
    if(a[st] > a[piv]) swap(a[st], a[piv]);

    int pivot = a[piv];
    swap(a[piv], a[dr]);

    int i = st;
    for(int j = st; j <= dr; j++) {
        if(a[j] < pivot) {
            swap(a[i++], a[j]);
        }
    }

    swap(a[i], a[dr]);
    return i;
}

static inline void QuickSort(int st, int dr) {
    if(st >= dr) return;

    int piv = Pivot(st, dr);
    QuickSort(st, piv - 1);
    QuickSort(piv + 1, dr);
}

int main() {
    if(USE_STD_IO) ios_base::sync_with_stdio(false);
    fin.tie(NULL);
    fout.tie(NULL);

    fin >> n >> k;
    for(i = 1; i <= n; i++) fin >> a[i];
    QuickSort(1, n);

    fout << a[k] << " ";

    return 0;
}