Cod sursa(job #806540)

Utilizator alex_unixPetenchea Alexandru alex_unix Data 2 noiembrie 2012 23:28:37
Problema Statistici de ordine Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb

#include <cstdio>
#include <cstdlib>
#include <ctime>

const int MAX_SIZE(3000000);

int v [MAX_SIZE];
int n, k;

inline void read (void)
{
	std::freopen("sdo.in","r",stdin);
	std::scanf("%d%d",&n,&k);
	for (int *iterator(v), *end(v + n) ; iterator < end ; ++iterator)
		std::scanf("%d",iterator);
	std::fclose(stdin);
}

inline void print (void)
{
	std::freopen("sdo.out","w",stdout);
	std::printf("%d\n",v[k - 1]);
	std::fclose(stdout);
}

void order_statistic (int left, int right, int k)
{
	int pivot(v[left + std::rand() % (right - left + 1)]), i(left), j(right), temp;
	while (i < j)
	{
		while (v[i] < pivot)
			++i;
		while (v[j] > pivot)
			--j;
		if (i < j)
		{
			temp = v[i];
			v[i] = v[j];
			v[j] = temp;
		}
	}
	if (k == j)
		return;
	if (k < j)
		order_statistic(left,j - 1,k);
	else if (k > j)
		order_statistic(j + 1,right,k);
}

int main (void)
{
	std::srand(std::time(0));
	read();
	order_statistic(0,n - 1,k - 1);
	print();
	return 0;
}