Cod sursa(job #3243224)

Utilizator YuzukyIstrate Andreea Ruxandra Yuzuky Data 16 septembrie 2024 16:46:53
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int MAX = 200000;
int h[MAX+1];
int cnt;

void sift (int x)  //pune nodul k la locul portivit, interschimband nodul cu cel mai mare fiu al sau pana cand nu mai are fii
{
    //cnt este left_fiu
    //cnt+1 este right_fiu
    //cnt/2 este tata
    ++cnt;
    h[cnt]=x;
    int node = cnt;
    while( (2*node<=cnt && h[2*node]<h[node]) || (2*node+1<=cnt) && h[2*node+1]<h[node] )
    {
        if( h[2*node] < h[node] && (2*node+1>cnt || h[2*node]<h[2*node+1]) )
            swap(h[node], h[2*node]);
        else
            swap(h[node], h[2*node+1]);
        node++;
    }
    /*while( (node<=MAX && h[node]> h[node/2]) || (node<=MAX && h[node+1]<h[node]) )
    {
        if( h[node]<h[node/2] && (node+1>MAX || h[node]<h[node+1]))
        {
            swap(h[node/2], h[node]);
        }
        else
        {
            swap(h[node/2], h[node+1]);
        }
        node = ?
    }*/
}



int main()
{
    int n;
    cin>>n;
    for(int i=0; i<n; ++i)
    {
        int c,x;
        cin>>c;
        if(c==1 || c==2)
            cin>>x;
        if(c==1)
        {
            sift(x);
            cout<<"heap : ";
            for(int j=1; j<=cnt; ++j)
                cout<<h[j]<<" ";
            cout<<'\n';
        }
    }
    return 0;
}