Cod sursa(job #2026889)

Utilizator ARobertAntohi Robert ARobert Data 25 septembrie 2017 12:01:54
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.06 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int v[200004],total,curent;
pair<int,int> heap[200004];

void fixpoz(int x)
{
    int ok=1;
    while (x/2!=0&&heap[x].first<heap[x/2].first)
    {
        v[heap[x].second]=x/2;
        v[heap[x/2].second]=x;
        swap(heap[x],heap[x/2]);
        ok=0;
    }
    while (2*x<=total&&ok==1)
    {
          if (2*x==total)
          {
              ok=0;
              if (heap[x].first>heap[2*x].first)
              {
                    v[heap[x].second]=2*x;
                    v[heap[2*x].second]=x;
                    swap(heap[x],heap[2*x]);
              }
          }
          else if (2*x+1==total)
          {
              ok=0;
              if (heap[2*x+1].first<heap[2*x].first)
              {
                  if (heap[x].first>heap[2*x+1].first)
                  {
                        v[heap[x].second]=2*x+1;
                        v[heap[2*x+1].second]=x;
                        swap(heap[x],heap[2*x+1]);
                  }
              }
              else if (heap[x].first>heap[2*x].first)
              {
                    v[heap[x].second]=2*x;
                    v[heap[2*x].second]=x;
                    swap(heap[x],heap[2*x]);
              }
          }
          else
          {
              ok=0;
              if (heap[2*x+1].first<heap[2*x].first)
              {
                  if (heap[x].first>heap[2*x+1].first)
                  {
                        ok=1;
                        v[heap[x].second]=2*x+1;
                        v[heap[2*x+1].second]=x;
                        swap(heap[x],heap[2*x+1]);
                  }
              }
              else if (heap[x].first>heap[2*x].first)
              {
                    ok=1;
                    v[heap[x].second]=2*x;
                    v[heap[2*x].second]=x;
                    swap(heap[x],heap[2*x]);
              }
          }
    }
}

void ins(int x)
{
    total++;
    curent++;
    v[total]=curent;
    heap[curent].first=x;
    heap[curent].second=total;
    fixpoz(curent);
}

void del(int x)
{
    int t=v[x];
    v[heap[x].second]=t;
    v[heap[t].second]=x;
    swap(heap[x],heap[t]);
    curent--;
    v[x]=0;
    fixpoz(t);
}

int main()
{
    int tip,n,i,t;
    fin>>n;
    for (i=1;i<=n;i++)
    {
        fin>>tip;
        if (tip==1)
        {
            fin>>t;
            ins(t);
            for (i=1;i<=curent;i++)
                cout<<heap[i].first<<" ";
            cout<<endl;
            tip=0;
        }
        else if (tip==2)
        {
            fin>>t;
            del(t);
            for (i=1;i<=curent;i++)
                cout<<heap[i].first<<" ";
            cout<<endl;
            tip=0;
        }
        else if (tip==3)
            {fout<<heap[1].first<<'\n';
            for (i=1;i<=curent;i++)
                cout<<heap[i].first<<" ";
            cout<<endl;
            tip=0;
            }
    }
    return 0;
}