Pagini recente » Istoria paginii runda/testasd | Istoria paginii runda/proges | Istoria paginii runda/rares_pre | Istoria paginii runda/das/clasament | Cod sursa (job #1780545)
#include <iostream>
#include <fstream>
#define NMAX 3000001
#define BUFF_SIZE 100000
using namespace std;
ifstream in("sdo.in");
ofstream out("sdo.out");
int a[NMAX], n, k;
char BUFF[BUFF_SIZE];
int pos = 0;
void Read(int &a)
{
while(!isdigit(BUFF[pos]))
if(++pos == BUFF_SIZE)
in.read(BUFF,BUFF_SIZE), pos = 0;
a = 0;
while(isdigit(BUFF[pos]))
{
a = a * 10 + (BUFF[pos] - '0');
if(++pos == BUFF_SIZE)
in.read(BUFF,BUFF_SIZE), pos = 0;
}
}
void quicksort(int x, int y)
{
if (x < y)
{
int i = x, j = y, pivot = a[(x+y)/2];
while (i < j)
{
while (i < y && a[i] < pivot)
i++;
while (j > x && a[j] > pivot)
j--;
if (i <= j)
{
int h = a[i];
a[i] = a[j];
a[j] = h;
i++;
j--;
}
}
if (i <= k && k <= y)
quicksort(i,y);
if (j >= k && k >= x)
quicksort(x,j);
}
}
int main()
{
Read(n);
Read(k);
for (int i = 1; i <= n; i++)
Read(a[i]);
in.close();
quicksort(1,n);
out << a[k] << "\n";
out.close();
return 0;
}