Pagini recente » Istoria paginii utilizator/viplairnotyet | Rating Stingaciu Petru Sebastian (StingaciuSebastian) | Istoria paginii utilizator/juniorutu | Cod sursa (job #2015743) | Cod sursa (job #1981814)
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("sdo.in");
ofstream out("sdo.out");
#define ll long long
#define pb push_back
const int NMax = 3e6 + 5;
int N,K,nrHeap;
int arr[NMax];
struct elem {
int idx,val;
elem (int _idx = 0,int _val = 0) {
val = _val;
idx = _idx;
}
}heap[NMax];
bool operator <(elem a,elem b) {
return a.val < b.val;
}
template <class T>
void sift(T*,int,int);
template <class T>
void percolate(T*,int);
int main() {
in>>N>>K;
for (int i=1;i <= N;++i) {
in>>arr[i];
}
for (int i=N/2;i > 0;--i) {
sift<int> (arr,i,N);
}
/*
for (int j=1;j <= N;++j) {
cout<<arr[j]<<' ';
}
cout<<"\n\n";
//*/
heap[++nrHeap] = elem(1,arr[1]);
for (int c=1;c < K;++c) {
elem node = heap[1];
swap(heap[1],heap[nrHeap]);
--nrHeap;
sift<elem> (heap,1,nrHeap);
int fs = node.idx*2,ss = fs+1;
if (fs <= N) {
heap[++nrHeap] = elem(fs,arr[fs]);
percolate<elem> (heap,nrHeap);
}
if (ss <= N) {
heap[++nrHeap] = elem(ss,arr[ss]);
percolate<elem> (heap,nrHeap);
}
}
out<<heap[1].val<<'\n';
in.close();out.close();
return 0;
}
template <class T>
void sift(T *v,int node,int dim) {
int son = 0;
do {
son = 0;
int fs = node*2,
ss = node*2 + 1;
if (fs <= dim) {
son = fs;
if (ss <= dim && v[ss] < v[son]) {
son = ss;
}
}
if (son && v[son] < v[node]) {
swap(v[node],v[son]);
node = son;
}
else {
son = 0;
}
}
while (son);
}
template <typename T>
void percolate(T* v,int node) {
int dad = node/2;
while (node != 1 && v[node] < v[dad]) {
swap(v[node],v[dad]);
node = dad;
dad = node/2;
}
}