Cod sursa(job #479515)

Utilizator razvi9Jurca Razvan razvi9 Data 24 august 2010 12:35:30
Problema Statistici de ordine Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 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[10000000];

int main()
{
	srand(time(NULL));
	ifstream fin("sdo.in");
	ofstream fout("sdo.out");
	fin.read(buf, sizeof(buf) - 1);
	istringstream cin(buf);

	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;
}