Cod sursa(job #633361)

Utilizator vlad.doruIon Vlad-Doru vlad.doru Data 13 noiembrie 2011 17:51:35
Problema Statistici de ordine Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 0.78 kb
#include <cstdio>

using namespace std;

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

const int N=3000010;

int n,k,v[N];

void citire(){
	freopen("sdo.in","r",stdin);
	freopen("sdo.out","w",stdout);
	int i;
	scanf("%d%d",&n,&k);
	for(i=1;i<=n;i++){
		scanf("%d",&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){
		printf("%d",v[j]);
		return;
	}
	if(k<j)
		Partition(st,j-1);
	else{
		Partition(j+1,dr);
	}
}

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