Cod sursa(job #633391)

Utilizator vlad.doruIon Vlad-Doru vlad.doru Data 13 noiembrie 2011 18:23:42
Problema Statistici de ordine Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.7 kb
#include <fstream>

using namespace std;

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

const int N=3000010;

int n,k,v[N];

void citire(){
	int i;
	in>>n>>k;
	k--;
	for(i=1;i<=n;i++){
		in>>v[i];
	}
}

inline void schimba(int x,int y){
	int aux;
	aux=v[x];
	v[x]=v[y];
	v[y]=aux;
}

void Partition(int st,int dr){
	int i=st-1,j=dr+1,pivot=v[(st+dr)/2];
	while(1)
	{
		do
		{
			++i;
		} while(v[i] < pivot);
		do
		{
			--j;
		} while(pivot< v[j]);
		if(i < j)
			schimba(i,j);
		else 
			break;
	}
	if(j==k){
		out<<v[j];
		return;
	}
	if(k<j)
		Partition(st,j-1);
	else{
		Partition(j+1,dr);
	}
}

int main(){
	citire();
	Partition(1,n);
	return 0;
}