Cod sursa(job #460326)

Utilizator toni2007Pripoae Teodor Anton toni2007 Data 1 iunie 2010 22:00:23
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <stack>
#include <fstream>

#define maxN 	3000100

using namespace std;

int V[maxN];

int partition (int Left, int Right){
	int piv, aux, i = Left - 1, j = Right + 1;

	piv = V[Left + rand () % (Right - Left + 1)];
//	piv = V[(Left + Right) >> 1];

	while (1){
		do {++i;} while (V [i] < piv);
		do {--j;} while (V [j] > piv);

		if (i < j){
			aux  = V[i];
			V[i] = V[j];
			V[j] = aux;
		}

		else
			return j;
	}
}

void quick_sort(int Left, int Right, int K){
	if (Left < Right){
		int Middle = partition (Left, Right);
		if (K <= Middle)
			quick_sort (Left, Middle, K);
		else
			quick_sort (Middle + 1, Right, K);
	}
}

int main(){
    int N, K, i;

	ifstream fin("sdo.in");
    freopen("sdo.out", "w", stdout);

	fin >> N >> K;

    srand(time(NULL));

    for (i = 0; i < N; ++i)
		fin >> V[i];

    quick_sort(0, N - 1, K - 1);

	printf("%d\n", V[K - 1]);
}