Pagini recente » Cod sursa (job #712295) | Istoria paginii runda/simulare_oji_2023_clasa_9_14_martie/clasament | Cod sursa (job #209627) | Cod sursa (job #1552228) | Cod sursa (job #2830265)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("sdo.in");
ofstream fout("sdo.out");
int N, K;
vector<int> v;
/// gasim pivotul
int pivot(int st, int dr)
{
int pivot=v[st+(rand()%(dr-st+1))];
while(st<=dr){
while(v[st]<pivot)
st++;
while(v[dr]>pivot)
dr--;
if(st<=dr){
swap(v[st],v[dr]);
st++;
dr--;
}
}
return st;
}
int quickSelect(int st, int dr, int nr)
{
int poz=pivot(st,dr);
if(poz-st+1==nr)
return v[poz];
if(poz-st+1>nr)
return quickSelect(st,poz-1,nr);
return quickSelect(poz+1,dr,nr-(poz-st+1));
}
int main()
{
fin>>N>>K;
int x;
for(int i=0;i<N;i++){
fin>>x;
v.push_back(x);
}
fout<<quickSelect(0,N-1,K);
fin.close();
fout.close();
return 0;
}