Pagini recente » Cod sursa (job #165845) | Monitorul de evaluare | Istoria paginii utilizator/vacduzz | Cod sursa (job #2830966) | Cod sursa (job #1241325)
#include <fstream>
#include <iterator>
#include <vector>
#include <utility>
#include <random>
#include <algorithm>
using namespace std;
struct Generator {
mt19937 gen;
Generator() : gen() {}
int operator()(const int& a, const int& b) {
return uniform_int_distribution<int>(a, b)(gen);
}
} uniform;
inline pair<int, int> partition(
const int& left,
const int& right,
std::vector<int>& V) {
// random pivot;
swap(V[left], V[ uniform(left, right) ]);
int pivot_l = left, pivot_r = left;
for (int i = left + 1; i <= right; ++i)
if (V[i] == V[pivot_l]) {
swap(V[i], V[++pivot_r]);
}
else if (V[i] < V[pivot_l]) {
int t = V[pivot_l];
V[pivot_l++] = V[i];
V[i] = V[++pivot_r];
V[pivot_r] = t;
}
return make_pair(pivot_l, pivot_r);
}
int order_statistic(int k, vector<int>& V) {
pair<int, int> q;
int l = 0, r = V.size()-1;
while (true) {
q = partition(l, r, V);
if (k < q.first) r = q.first - 1;
else if (q.second < k) l = q.second + 1;
else return V[q.first];
}
}
int main() {
ifstream fin("sdo.in");
ofstream fout("sdo.out");
int n, k; fin >> n >> k;
vector<int> V(n);
copy_n(istream_iterator<int>(fin), n, V.begin());
fout << order_statistic(k-1, V) << endl;
return 0;
}