Pagini recente » Cod sursa (job #922432) | Cod sursa (job #2670463) | Cod sursa (job #1401382) | Cod sursa (job #125684) | Cod sursa (job #1324182)
#include <fstream>
#define Bmax (1 << 14)
#define Nmax 500100
#define digit(x) ('0' <= x && x <= '9')
using namespace std;
template <typename T>
class priorityQueue {
#define root 1
#define father(x) (x >> 1)
#define leftSon(x) (x << 1)
#define rightSon(x) ((x << 1) | 1)
#define compare(a, b) (H[a - 1] < H[b - 1])
#define change(a, b) swap(H[a - 1], H[b - 1])
private:
vector <T> H;
public:
void insert(T Value) {
H.push_back(Value);
up(H.size());
}
void pop() {
H[0] = H[H.size() - 1];
H.pop_back();
down(root);
}
T front() {
return H[0];
}
bool empty() {
return (H.size() == 0);
}
private:
void up(int Node) {
while(Node != root && compare(Node, father(Node))) {
change(Node, father(Node));
Node = father(Node);
}
}
void down(int Node) {
T son;
do {
son = 0;
if(leftSon(Node) <= H.size()) {
son = leftSon(Node);
if(rightSon(Node) <= H.size() && compare(rightSon(Node), son))
son = rightSon(Node);
}
if(compare(Node, son))
son = 0;
if(son != 0) {
change(Node, son);
Node = son;
}
} while(son);
}
};
priorityQueue <int> Heap;
int N;
void Read() {
int i, x;
ifstream in("algsort.in");
in >> N;
for(i = 1; i <= N; i++) {
in >> x
Heap.insert(x);
}
in.close();
}
void Write() {
ofstream out("algsort.out");
for(int i = 1; i <= N; i++) {
out << Heap.front() << ' ';
Heap.pop();
}
out.close();
}
int main() {
Read();
Write();
return 0;
}