Cod sursa(job #442867)

Utilizator liviu12345Stoica Liviu liviu12345 Data 15 aprilie 2010 16:17:58
Problema Statistici de ordine Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include<stdlib.h>
#include<time.h>
#include<iostream.h>
#include<fstream.h>
#define maxn  3000000
ifstream f ("sdo.in");
ofstream g ("sdo.out");
int n,Pozitie,Sir[maxn];
int Distribuire(int Left,int Right, int Pivot)
{
	int Contor=Left-1;
	for(int i=Left; i<=Right; i++)
	{
		if (Sir[i]<=Sir[Pivot])
			Contor++;
	}
	Sir[Pivot]=Sir[Pivot]+Sir[Contor]-(Sir[Contor]=Sir[Pivot]);
	for(int i=Left;i<Contor;i++)
	{
		if(Sir[i]>Sir[Contor])
		{
			int j=Contor+1;
			while(Sir[j]>Sir[Contor])
			{
				j++;
			}
			Sir[i]=Sir[i]+Sir[j]-(Sir[j]=Sir[i]);
		}
	}
	return Contor;
}
void StatOrd (int Left, int Right)
{
	if(Left!=Right)
	{
		int h = Left + rand() % ( Right - Left );;
		int Referinta=Distribuire(Left, Right, h);
		if (Pozitie<=Referinta)
			StatOrd(Left,Referinta);
		else
		{
			//Pozitie -= Referinta;
			StatOrd(Referinta+1,Right);
		}
	}
	
}
int main()
{
	srand( time( 0 ) );
	
	//random_poz = left + rand() % ( right - left );
	f>>n>>Pozitie;
	for(int i=1;i<=n;i++)
		f>>Sir[i];
	StatOrd(1,n);
	g<<Sir[Pozitie];
	return 0;
	
	
}