Cod sursa(job #3333442)

Utilizator 17.emi._Tabara Emilian 17.emi._ Data 13 ianuarie 2026 16:24:02
Problema Statistici de ordine Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <fstream>
using namespace std;

ifstream fin("sdo.in");
ofstream fout("sdo.out");

static int a[3000005];

int main() {
    ios::sync_with_stdio(false);
    fin.tie(nullptr);

    int n, k;
    fin >> n >> k;
    k--;

    for (int i = 0; i < n; i++) fin >> a[i];

    int L = 0, R = n - 1;

    while (true) {
        int pivot = a[L + rand() % (R - L + 1)];

        int i = L, j = L, t = R;
        while (j <= t) {
            if (a[j] < pivot) {
                swap(a[i], a[j]);
                i++;
                j++;
            } else if (a[j] > pivot) {
                swap(a[j], a[t]);
                t--;
            } else j++;
        }
        if (k < i) R = i - 1;
        else if (k > t) L = t + 1;
        else {
            fout << pivot << "\n";
            return 0;
        }
    }
}