Pagini recente » Cod sursa (job #2662927) | Cod sursa (job #2830788) | Cod sursa (job #1310537) | Cod sursa (job #1583647) | Cod sursa (job #3210959)
#include <fstream>
#include <cstdlib>
using namespace std;
const int N = 3e6;
int v[N];
void part3(int v[], int st, int dr, int &p1, int &p2)
{
int i = st;
p1 = st;
p2 = dr - 1;
while (i <= p2)
{
if (v[i] < v[dr])
{
swap(v[i++], v[p1++]);
}
else if (v[i] > v[dr])
{
swap(v[i], v[p2--]);
}
else///v[i]==v[dr]
{
i++;
}
}
swap(v[++p2], v[dr]);
}
void qs(int v[], int st, int dr, int k)
{
if (st >= dr)
{
return;
}
int p1, p2;
part3(v, st, dr, p1, p2);
if (k <= p1)
{
qs(v, st, p1 - 1, k);
}
if (k >= p2 + 1)
{
qs(v, p2 + 1, dr, k);
}
}
void shuffle(int v[], int n)
{
for (int i = n - 1; i > 0; i--)
{
int p = rand() % (i + 1);///poz. oarecare intre 0 si i inclusiv
swap(v[i], v[p]);
}
}
int main()
{
ifstream in("sdo.in");
ofstream out("sdo.out");
int n, k;
in >> n >> k;
k--;
for (int i = 0; i < n; i++)
{
in >> v[i];
}
shuffle(v, n);
qs(v, 0, n - 1, k);
out << v[k];
in.close();
out.close();
return 0;
}