Cod sursa(job #1201577)

Utilizator azkabancont-vechi azkaban Data 25 iunie 2014 14:47:12
Problema Interclasari Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream cin("interclasari.in");
ofstream cout("interclasari.out");

long H[200013],i,j,n,nod,l(0),aux,sol(0);

void heap_up(long nod)
{
 if (H[nod]<H[nod/2] && nod>1) {
                                swap(H[nod],H[nod/2]);
                                heap_up(nod/2); 
                               }
}

void heap_down(long nod)
{
  long fiu;
  while (fiu){
              fiu=0;
              if (nod*2<=l){
                            fiu=nod*2;
                            if (nod*2+1<=l && H[nod*2+1]<H[fiu]) fiu=nod*2+1;
                            if (H[fiu]>=H[nod]) fiu=0;
                            } 
                 if (fiu){
                          swap(H[fiu],H[nod]);
                          nod=fiu; 
                          }
             }  
}

void add(long nod)
{
 H[++l]=nod;
 heap_up(l);    
}

void del()
{ 
 cout<<H[1]<<" ";
 H[1]=H[l];
 --l;
 heap_down(1);
}

int main()
{
 cin>>n;
 for (i=1;i<=n;++i) {
                    cin>>aux;
                    sol+=aux;
                    for (j=1;j<=aux;j++) {
                                          cin>>nod;
                                          add(nod);
                                         }
                    } 
 cout<<sol<<"\n";   
 while (l) del();
 return 0;
}