Pagini recente » Cod sursa (job #1989650) | Cod sursa (job #1445051) | Cod sursa (job #2930206) | Cod sursa (job #2269311) | Cod sursa (job #244726)
Cod sursa(job #244726)
#include <iostream>
#define NMAX 200005
using namespace std;
FILE *f = fopen("heapuri.in", "r"), *g = fopen("heapuri.out", "w");
int nrElemente = -1, a[NMAX], v[NMAX], p[NMAX];
int addHeap(int x)
{
a[++nrElemente] = x;
int i = nrElemente, tata = (i - 1) / 2;
while (a[i] < a[tata])
{
int aux = a[i];
a[i] = a[tata];
a[tata] = aux;
aux = v[p[i]];
v[p[i]] = v[p[tata]];
v[p[tata]] = aux;
aux = p[i];
p[i] = p[tata];
p[tata] = aux;
i = tata;
tata = (i - 1) / 2;
}
return 0;
}
int removeHeap(int k)
{
//trebuie sa caut pozitia pe care se afla k in heap
int i = k;
a[i] = a[nrElemente--];
int s = 2 * i + 1, d = 2 * i + 2;
while ((d <= nrElemente) && ((a[s] < a[i]) || (a[d] < a[i])))
{
if ((a[i] > a[s]) && (a[i] > a[d]))
{
if (a[s] > a[d])
{
int aux = a[i];
a[i] = a[d];
a[d] = aux;
aux = v[p[i]];
v[p[i]] = v[p[d]];
v[p[d]] = aux;
aux = p[i];
p[i] = p[d];
p[d] = aux;
i = d;
}
else
{
int aux = a[i];
a[i] = a[s];
a[s] = aux;
aux = v[p[i]];
v[p[i]] = v[p[s]];
v[p[s]] = aux;
aux = p[i];
p[i] = p[s];
p[s] = aux;
i = s;
}
}
else
{
if (a[i] > a[s])
{
int aux = a[i];
a[i] = a[s];
a[s] = aux;
aux = v[p[i]];
v[p[i]] = v[p[s]];
v[p[s]] = aux;
aux = p[i];
p[i] = p[s];
p[s] = aux;
i = s;
}
else
{
int aux = a[i];
a[i] = a[d];
a[d] = aux;
aux = v[p[i]];
v[p[i]] = v[p[d]];
v[p[d]] = aux;
aux = p[i];
p[i] = p[d];
p[d] = aux;
i = d;
}
}
s = 2 * i + 1;
d = 2 * i + 2;
}
if ((2 * i + 1 <= nrElemente) && (a[i] > a[2 * i + 1]))
{
int aux = a[2 * i + 1];
a[2 * i + 1] = a[i];
a[i] = aux;
aux = v[p[i]];
v[p[i]] = v[p[2 * i + 1]];
v[p[2 * i + 1]] = aux;
aux = p[i];
p[i] = p[2 * i + 1];
p[2 * i + 1] = aux;
i = 2 * i + 1;
}
return 0;
}
int main()
{
int N;
fscanf(f, "%d", &N);
int q = 0;
for (int i = 0; i < N; ++i)
{
int n, x;
fscanf(f, "%d", &n);
if (n != 3)
{
fscanf(f, "%d", &x);
if (n == 1)
{
v[q] = q;
p[q] = q;
q++;
addHeap(x);
}
else
{
removeHeap(v[x - 1]);
}
}
else
{
fprintf(g, "%d\n", a[0]);
}
}
fclose(f);
fclose(g);
return 0;
}