Cod sursa(job #1332950)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 2 februarie 2015 16:42:24
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>

#define Nmax 3000100

using namespace std;

int N, K, V[Nmax];

int nthElement(int A, int B, int & K) {

    int left, right, middleValue;

    left = A;
    right = B;
    middleValue = V[left + (right - left) / 2];

    while(left < right) {

        while(V[left] < middleValue)
            ++left;

        while(middleValue < V[right])
            --right;

        if(left <= right) {
            swap(V[left], V[right]);
            ++left;
            --right;
        }
    }

    if(K <= right && A < right)
        nthElement(A, right, K);
    else
    if(K >= left && left < B)
        nthElement(left, B, K);

}
void Read() {

    ifstream in("sdo.in");
    in >> N >> K;

    for(int i = 1; i <= N; i++)
        in >> V[i];

    in.close();
}
void Write() {

    ofstream out("sdo.out");
    out << V[K] << '\n';
    out.close();
}
int main() {

    Read();
    nthElement(1, N, K);
    Write();

    return 0;
}