Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- void heapify (vector <int> &A, int i) {
- int best = i;
- int temp;
- int l = 2*i + 1;
- int r = 2*i + 2;
- if (r < A.size() && A[r] < A[best])
- best = r;
- if (l < A.size() && A[l] <= A[best])
- best = l;
- if (best != i) {
- swap(A[i], A[best]);
- heapify(A, best);
- }
- }
- void buildHeap(vector<int> &A) {
- for (int i=A.size()/2; i>=0; i--) {
- heapify(A, i);
- }
- }
- void extract(vector<int> &A) {
- int m = A[0];
- cout<<A[0]<<endl;
- swap(A[0], A[A.size()-1]);
- A.pop_back();
- buildHeap(A);
- }
- void print_heap(vector<int> &A) {
- for (int i=0; i<A.size(); i++) {
- cout << A[i] << " ";
- }
- cout << endl;
- }
- int main() {
- int n_test;
- int N;
- vector <int> A; // tablica dla kopca
- cin >> n_test;
- for (int i=0; i<n_test; i++) {
- cin >> N;
- A.clear();
- if (N>0) {
- int i;
- int value;
- for (i=0; i<N; i++) {
- cin >> value;
- A.push_back(value);
- }
- buildHeap(A);
- }
- int option_number;
- char option;
- cin >> option_number;
- for (int i=0; i<option_number; i++) {
- cin >> option;
- switch (option) {
- case 'E':
- extract(A);
- break;
- case 'P':
- print_heap(A);
- break;
- default:
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement