Cod sursa(job #479520)

Utilizator razvi9Jurca Razvan razvi9 Data 24 august 2010 12:41:26
Problema Statistici de ordine Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.81 kb
#include <iostream>
#include <fstream>
#include <sstream>
#include <ctime>
#include <cstdlib>
using namespace std;

void order(int *a, int n, int k)
{
	int j, st, dr;
	bool dir;
	while(1)
	{
		int j = rand() % n;
		swap(a[0], a[j]);
		
		st = 0, dr = n-1, dir = false;
		while (st < dr)
		{
			if (a[st] > a[dr])
				swap(a[st], a[dr]), dir = !dir;
			if(dir) 
				++ st;
			else
				-- dr;
		}

		j = st;

		if(j == k)
			return;

		if(j < k)
			a += j, n -= j, k -= j;
		else
			n = j;
	}
}

int a[3000000];
char buf[1000000];

int main()
{
	srand(time(NULL));
	ifstream cin("sdo.in");
	cin.rdbuf()->pubsetbuf(buf, 1000000);
	ofstream fout("sdo.out");

	int k, n;
	cin >> n >> k;
	-- k;
	for(int i=0;i<n;++i)
		cin >> a[i];

	order(a, n, k);
	fout << a[k];

	return 0;
}