Cod sursa(job #2623357)

Utilizator danutbodbodnariuc danut danutbod Data 2 iunie 2020 23:52:59
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.55 kb
////arbore binar de cautare implementa cu pointeri
////creare ,adaugare, afisare ,cautare,stergere
////insereaza elemente distincte
//#include<fstream>
//using namespace std;
//ifstream fi("arb.in");
//ofstream fo("arb.out");
//struct Nod{
//int info;
//Nod *st,*dr;
//}*r;
//int i,n,x;
//void inserare_distincte(Nod*&r,int x){//inserare el. distincte
//Nod*q,*p=new Nod;
//p->info=x;
//p->st=0;
//p->dr=0;
//if(r==0){r=p;return;}
//q=r;
//while(1){
//    if(q->info==x){delete p;return;}
//    if(x<q->info)
//        if(q->st)q=q->st;
//        else {q->st=p; return;}
//    else
//        if(q->dr)q=q->dr;
//        else {q->dr=p; return;}
//    }
//}
//void inserare(Nod*&r,int x){//inserare el. oarecare(se pot repeta)
//Nod*q,*p=new Nod;
//p->info=x;
//p->st=0;
//p->dr=0;
//if(r==0){r=p;return;}
//q=r;
//while(1){
//    if(x<=q->info)
//        if(q->st)q=q->st;
//        else {q->st=p; return;}
//    else
//        if(q->dr)q=q->dr;
//        else {q->dr=p; return;}
//    }
//}
//void afisare(Nod*r){
//if(r){
//    afisare(r->st);
//    fo<<r->info<< " ";
//    afisare(r->dr);
//}
//}
//int cautare(Nod*r,int x){
//if(r){
//        if(x==r->info)return 1;
//        else if(x<r->info)cautare(r->st,x);
//             else cautare(r->dr,x);
//    }
//    else return 0;
//}
//void sterge(Nod*&r,int x){   //stergere nod de valoare x
//Nod *p,*t=0,*tmax,*max,*fiu;
//p=r;
//while(p and p->info!=x){
//        t=p;
//        if(x<p->info)p=p->st;
//        else p=p->dr;
//    }
//if(p==0)return; //nu este nodul x in arbore
//if(p->st and p->dr)  //nodul de sters are 2 fii
//{   //se determina maximul din subarborele stang
//    tmax=p;
//    max=p->st;
//    while(max->dr){
//        tmax=max;
//        max=max->dr;
//    }
//    p->info=max->info;//copiez informatia
//    t=tmax;//sterg nodul max
//    p=max;
//}
////nodul care va fi sters are cel mult un nod
//if(p->st)fiu=p->st;
//  else fiu=p->dr;
//if(t)
//   if(t->st==p)t->st=fiu;//p este fiu stang al tatalui
//   else t->dr=fiu;       //p este fiu drept al tatalui
// else r=fiu;  //sterg radacina
// delete p;
//}
////echilibrare
//int main(){
//    r=0;
//    fi>>n;
//    for(i=1;i<=n;i++)
//        {fi>>x;
//         inserare(r,x);
//        }
//    afisare(r);
////    sterge(r,1);sterge(r,34);
////     fo<<endl;afisare(r);
//   //fo<<endl<<cautare(r,41);
//return 0;
//}

//5
////----------------------------------------------------
////infoarena heapuri
//https://www.infoarena.ro/problema/heapuri
//Fie o multime de numere naturale initial vida.
//Se cere sa se efectueze N operatii asupra acestei multimi, unde operatiile
// sunt de tipul celor de mai jos:
//
//operatia de tipul 1: se insereaza elementul x in multime
//operatia de tipul 2: se sterge elementul intrat al x-lea in multime,
//                     in ordine cronologica
//operatia de tipul 3: se afiseaza elementul minim din multime
#include <fstream>
#define M 200003
using namespace std;
ifstream fi("heapuri.in");
ofstream fo("heapuri.out");
struct Nod{
int info;
Nod *st,*dr;
}*r;
int i,n,x,m,op,j,a[M];

void inserare(Nod*&r,int x){//inserare el. oarecare(se pot repeta)
Nod*q,*p=new Nod;
p->info=x;
p->st=0;
p->dr=0;
if(r==0){r=p;return;}
q=r;
while(1){
    if(x<=q->info)
        if(q->st)q=q->st;
        else {q->st=p; return;}
    else
        if(q->dr)q=q->dr;
        else {q->dr=p; return;}
    }
}
int minim(Nod*r){  //minimul este cel mai din stanga nod
while(r->st)r=r->st;
return r->info;
}
void sterge(Nod*&r,int x){   //stergere nod de valoare x
Nod *p,*t=0,*tmax,*max,*fiu;
p=r;
while(p and p->info!=x){
        t=p;
        if(x<p->info)p=p->st;
        else p=p->dr;
    }
if(p==0)return; //nu este nodul x in arbore
if(p->st and p->dr)  //nodul de sters are 2 fii
{   //se determina maximul din subarborele stang
    tmax=p;
    max=p->st;
    while(max->dr){
        tmax=max;
        max=max->dr;
    }
    p->info=max->info;//copiez informatia
    t=tmax;//sterg nodul max
    p=max;
}
//nodul care va fi sters are cel mult un nod
if(p->st)fiu=p->st;
  else fiu=p->dr;
if(t)
   if(t->st==p)t->st=fiu;//p este fiu stang al tatalui
   else t->dr=fiu;       //p este fiu drept al tatalui
 else r=fiu;  //sterg radacina
 delete p;
}
int main()
{
    fi >> m;
    for(i=1;i<=m;i++){
       fi >> op;
       if(op==1){ n++; fi>>a[n]; inserare(r,a[n]); }
       if(op==2){ fi>>j; sterge(r,a[j]); }
       if(op==3){ fo<<minim(r)<<'\n'; }
    }
    fi.close(); fo.close();
    return 0;
}