Cod sursa(job #2340543)

Utilizator axelteoTeodor Dutu axelteo Data 10 februarie 2019 17:03:17
Problema Statistici de ordine Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <fstream>
#include <cstdio>
#include <random>

#define NMAX (1 << 20)

using namespace std;

FILE *pInFile, *pOutFile;
int v[3000000], pos = NMAX - 1, n, k;
char buff[NMAX];

void readInt(int &x) {
    while (buff[pos] < '0' || buff[pos] > '9') {
        if (++pos == NMAX) {
            fread(buff, 1, NMAX, pInFile);
            pos = 0;
        }
    }

    while (buff[pos] >= '0' && buff[pos] <= '9') {
        x = x * 10 + buff[pos] - '0';
        if (++pos == NMAX) {
            fread(buff, 1, NMAX, pInFile);
            pos = 0;
        }
    }
}

void partition(int left, int right, int currK) {
    if (left >= right) {
        return;
    }

    int pos = random() % (right - left + 1) + left;
    int pivot = v[pos], j = left;

    for (int i = left; i <= right; ++i) {
        if (v[i] <= pivot) {
            swap(v[i], v[j++]);
        }
    }

    if (currK <= j - left) {
        partition(left, j - 1, currK);
    } else {
        partition(j, right, currK - j + left);
    }
}

int main() {
    pInFile = fopen("sdo.in", "r");
    pOutFile = fopen("sdo.out", "w");

    readInt(n);
    readInt(k);

    for (int i = 0; i < n; ++i) {
        readInt(v[i]);
    }

    srand(time(NULL));
    partition(0, n - 1, k - 1);
    fprintf(pOutFile, "%d\n", v[k - 1]);

    fclose(pInFile);
    fclose(pOutFile);
    return 0;
}