Cod sursa(job #633400)

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

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;
	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,j=dr,pivot=v[st];
	while(i<j){
		while(v[i]<=pivot && i<=dr && j>i)
			++i;
		while(v[j]>pivot && j>=st && j>=i)
			--j;
		if(j>i)
			schimba(i,j);
	}
    schimba(j,st);
	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);
	nth_element(v+1,v+k,v+n+1);
	out<<v[k];
	return 0;
}