Pagini recente » Cod sursa (job #614056) | Cod sursa (job #383721) | Monitorul de evaluare | Profil Vanesa | Cod sursa (job #2026069)
#include <iostream>
#include <fstream>
#define ARRAY_SIZE 3000001
#define BUFF_SIZE 1000001
using namespace std;
ifstream in("sdo.in");
ofstream out("sdo.out");
char buffer[BUFF_SIZE];
int pos = 0;
void Read(int &a) {
while (!isdigit(buffer[pos]))
if (++pos == BUFF_SIZE)
in.read(buffer, BUFF_SIZE), pos = 0;
a = 0;
while (isdigit(buffer[pos])) {
a = a * 10 + buffer[pos] - '0';
if (++pos == BUFF_SIZE)
in.read(buffer, BUFF_SIZE), pos = 0;
}
}
int _array[ARRAY_SIZE];
void quicksort(int x, int y, int k) {
if (x < y) {
int i = x, j = y, pivot = _array[x + (y - x) / 2];
while (i <= j) {
while (_array[i] < pivot)
i++;
while (_array[j] > pivot)
j--;
if (i <= j) {
swap(_array[i], _array[j]);
i++;
j--;
}
}
if (i <= k && k <= y)
quicksort(i, y, k);
else
if (j >= k && k >= x)
quicksort(x, j, k);
}
}
int main() {
int n, k;
Read(n), Read(k);
for (int i = 1; i <= n; i++)
Read(_array[i]);
in.close();
quicksort(1, n, k);
out << _array[k] << "\n";
out.close();
return 0;
}