Cod sursa(job #785366)

Utilizator danalex97Dan H Alexandru danalex97 Data 8 septembrie 2012 18:07:24
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>
#include <ctime>
#include <cstdlib>

using namespace std;

const int Maxn = 3000005;

ifstream F("sdo.in");
ofstream G("sdo.out");

int A[Maxn], N, K;

int Part(int Left,int Right)
{
	int i = Left-1, j = Right+1;
	int	p = A[Left + (rand()%(Right-Left+1))];
	
	while ( true ) 
	{
		do{ ++i; } while( A[i]<p );
		do{ --j; } while( A[j]>p );
		if ( i>=j ) return j;
		swap(A[i],A[j]);
	}
}

void Stats(int Left,int Right,int Poz)
{
	if ( Left == Right ) return;
	
	int Q = Part( Left , Right );
	int T = Q - Left + 1;
	
	if ( T >= Poz ) Stats( Left , Q , Poz );
			   else Stats( Q+1 , Right , Poz-T );
}

int main()
{
	srand(time(NULL));
	
	F >> N >> K;
	for (int i = 1; i <= N; ++i)
		F >> A[i];

	Stats( 1, N, K );
	
	G << A[K] << "\n";
}