Pagini recente » Cod sursa (job #1155769) | Cod sursa (job #554979) | Cod sursa (job #2131514) | Cod sursa (job #3204337) | Cod sursa (job #1332950)
#include <fstream>
#define Nmax 3000100
using namespace std;
int N, K, V[Nmax];
int nthElement(int A, int B, int & K) {
int left, right, middleValue;
left = A;
right = B;
middleValue = V[left + (right - left) / 2];
while(left < right) {
while(V[left] < middleValue)
++left;
while(middleValue < V[right])
--right;
if(left <= right) {
swap(V[left], V[right]);
++left;
--right;
}
}
if(K <= right && A < right)
nthElement(A, right, K);
else
if(K >= left && left < B)
nthElement(left, B, K);
}
void Read() {
ifstream in("sdo.in");
in >> N >> K;
for(int i = 1; i <= N; i++)
in >> V[i];
in.close();
}
void Write() {
ofstream out("sdo.out");
out << V[K] << '\n';
out.close();
}
int main() {
Read();
nthElement(1, N, K);
Write();
return 0;
}