Pagini recente » Cod sursa (job #1625951) | Cod sursa (job #1781038) | Cod sursa (job #75826) | Cod sursa (job #658262) | Cod sursa (job #2489476)
//
// main.cpp
// c++joc
//
// Created by Andu Andu on 07/11/2019.
// Copyright © 2019 Andu Andu. All rights reserved.
//
#include <fstream>
#define N 500001
using namespace std;
ifstream cin ("algsort.in");
ofstream cout ("algsort.out");
int n,m;
int h[N];
void maxx() {
cout<<h[1]<<" ";
}
void add(int newElem) {
n++;
int poz = n;
h[n] = newElem;
while (poz>1 && h[poz/2] > h[poz]) {
swap(h[poz/2], h[poz]);
poz /= 2;
}
}
void popFirst() {
int poz = 1;
h[1] = h[n];
h[n] = 0;
n--;
while (2*poz <= n) {
poz *= 2;
if (poz<n && h[poz+1] < h[poz]) {
poz++; //trec la celalalt copil
}
if (h[poz] < h[poz/2]) {
swap(h[poz], h[poz/2]);
} else poz = n; // ca sa iasa
}
}
int main() {
cin>>m;
int v;
for (int i=1; i<=m; i++) {
cin>>v;
add(v);
}
for (int i=1; i<=m; i++) {
maxx();
popFirst();
}
return 0;
}