Cod sursa(job #1052253)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 10 decembrie 2013 23:11:05
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include <fstream>
#define Nmax 3000100
using namespace std;

int N, K, V[Nmax];

void Part(int A, int B) {

    int Left, Right, Pivot;

    Left = A;
    Right = B;
    Pivot = V[(Left + Right) >> 1];

    while(Left <= Right) {

        while(V[Left] < Pivot)
            ++Left;

        while(Pivot < V[Right])
            --Right;

        if(Left <= Right) {
            swap(V[Left], V[Right]);
            ++Left; --Right;
            }

        }

    if(K <= Right && A < Right)
        Part(A, Right);

    if(K >= Left && Left < B)
        Part(Left, B);

}
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();
    Part(1, N);
    Write();

    return 0;

}