Pagini recente » Cod sursa (job #1257568) | Cod sursa (job #1255267) | Cod sursa (job #2622477)
#include <fstream>
#include <vector>
int binary(std::vector<int>& v, int sought) {
int i, step = 1;
for (; step < v.size(); step <<= 1);
for (i = 0; step; step >>=1) {
if (i + step < v.size() && v[i+step] <= sought) {
i+=step;
}
}
if (v[i] == sought) {
return i;
}
return -1;
}
void push(std::vector<int>& v, int val) {
int i, step = 1;
for (; step < v.size(); step <<= 1);
for (i = 0; step; step >>=1) {
if (i + step < v.size() && v[i+step] < val) {
i+=step;
}
}
v.insert(v.begin() + i, val);
}
void remove(std::vector<int>& v, int val) {
int found = binary(v, val);
if (found != -1)
v.erase(v.begin() + found);
}
int main() {
std::ifstream f("heapuri.in");
std::ofstream g("heapuri.out");
std::vector<int> v, ord;
int command, n, tmp;
f >> n;
for (int i = 0; i < n; i++) {
f >> command;
if (command == 1) {
f >> tmp;
push(v, tmp);
ord.push_back(tmp);
}
if (command == 2) {
f >> tmp;
remove(v, ord[tmp - 1]);
}
if (command == 3) {
g << v[0] << "\n";
}
}
}