Pagini recente » Cod sursa (job #902318) | Cod sursa (job #2761851) | Cod sursa (job #1741280) | Cod sursa (job #14153) | Cod sursa (job #1560200)
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#include <cstring>
#include <map>
#include <iomanip>
#include <time.h>
#include <stdio.h>
#define MAX 500000000000
//#include <iostream>
using namespace std;
ifstream cin("sdo.in");
ofstream cout("sdo.out");
int heap[3000004];
void add(int heap[], int val, int poz)
{
heap[poz] = val;
while(poz / 2 > 0 && heap[poz] <= heap[poz / 2]){
swap(heap[poz], heap[poz / 2]);
poz /= 2;
}
}
void del(int x[], int &n)
{
swap(x[n], x[1]);
n--;
x[n + 1] = 0;
int poz = 1;
while(poz * 2 <= n && (x[poz] >= x[poz * 2] || x[poz] >= x[poz * 2 + 1])){
if(x[poz * 2] <= x[poz * 2 + 1])
{
swap(x[poz * 2], x[poz]);
poz *= 2;
}
else
{
swap(x[poz * 2 + 1], x[poz]);
poz *= 2;
poz++;
}
}
}
void elim(int &n, int x[], int poz)
{
swap(x[poz], x[n]);
n--;
while(poz * 2 <= n && (x[poz] >= x[poz * 2] || x[poz] >= x[poz * 2 + 1]))
{
if(x[poz * 2] <= x[poz * 2 + 1])
{
swap(x[poz], x[poz * 2]);
poz *= 2;
}
else
{
swap(x[poz], x[poz * 2 + 1]);
poz *= 2;
poz++;
}
}
}
int main()
{
int n, k, a;
cin >> n >> k;
for(int i = 0; i < n; i++){
cin >> a;
add(heap, a, i + 1);
}
while(k != 1){
elim(n, heap, 1);
k--;
}
cout << heap[1];
return 0;
}