Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // HomeworkOrder.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- struct Heap {
- int* T;
- int gorup;
- //= new int[2000];
- int nElements;
- bool initialized;
- Heap() {
- nElements = 0;
- initialized = false;
- }
- ~Heap() {
- //if(initialized)
- //delete[] T;
- }
- void initializeArray() {
- T = new int[1];
- }
- void clearMemory() {
- delete[] T;
- }
- void printHeap()
- {
- int* pom = new int[nElements];
- pom = T;
- for (int i = 0; i<nElements; i++)
- for (int j = 1; j<nElements - i; j++)
- if (pom[j - 1]>pom[j]){
- int a = pom[j - 1];
- int b = pom[j];
- pom[j] = a;
- pom[j - 1] = b;
- }
- for (int i = nElements-1; i >= 0; --i)
- cout << pom[i]<<" ";
- cout << endl;
- //delete[] pom;
- }
- void heap_push(int v)
- {
- int i, j;
- i = nElements;
- nElements++;
- if (nElements > 1) {
- int* tmp = new int[nElements + 1];
- tmp = T;
- tmp[nElements] = 0;
- T = tmp;
- }
- j = (i - 1) / 2;
- while (i > 0 && T[j] < v)
- {
- T[i] = T[j];
- i = j;
- j = (i - 1) / 2;
- }
- T[i] = v;
- }
- void heap_pop(bool printValue)
- {
- int i, j, v;
- if(printValue &&T[0]!=0)
- cout << T[0] << endl;
- if (T[0] == 0)
- cout << "na" << endl;
- //cout << "LICZBA ELEMENTOW: " << nElements << endl;
- if (nElements--)
- {
- int* tmp = new int[nElements];
- tmp = T;
- T = tmp;
- v = T[nElements];
- T[nElements] = 0;
- i = 0;
- j = 1;
- while (j < nElements)
- {
- if (j + 1 < nElements && T[j + 1]> T[j]) j++;
- if (v >= T[j]) break;
- T[i] = T[j];
- i = j;
- j = 2 * j + 1;
- }
- T[i] = v;
- }
- if(nElements<=0) { //usun element z listy
- T[0] = 0;
- nElements = 0;
- }
- }
- int heap_returnTopValue() {
- return T[0];
- }
- void heap_change_element( int v, int p)
- {
- //cout << "CHANGING IN BIG HEAP " << v << " with " << p << " index " << index << endl;
- int npom = nElements;
- for (int i = 0; i < npom; i++)
- {
- if (T[i] == v) {
- T[i] = p;
- break;
- }
- }
- }
- void heap_build() {
- int npom = nElements;
- int* pom = new int[nElements];
- for (int i = 0; i < npom; i++) {
- pom[i] = T[i];
- }
- for (int i = 0; i < npom; i++) {
- heap_pop(false);
- }
- for (int i = 0; i < npom; ++i) {
- heap_push(pom[i]);
- }
- //delete[] pom;
- }
- };
- int main()
- {
- Heap heaps[1000];
- int n;
- cin >> n;
- for (int i = 0; i < n; ++i){
- char character;
- cin >> character;
- if (character == 'a') {
- int group;
- int value;
- cin >> group >> value;
- if (heaps[group].initialized == false) {
- heaps[group].initialized = true;
- heaps[group].initializeArray();
- }
- heaps[group].heap_push(value);
- }
- else if(character == 'e') {
- int group;
- cin >> group;
- heaps[group].heap_pop(true);
- }
- else if (character == 'p') {
- int group;
- cin >> group;
- heaps[group].printHeap();
- }
- else if (character == 'm') {
- int group1, group2;
- cin >> group1 >> group2;
- int el=heaps[group2].nElements;
- int pom = 0;
- for (int j = 0; j < el; ++j)
- {
- pom= heaps[group2].heap_returnTopValue();
- heaps[group2].heap_pop(false);
- heaps[group1].heap_push(pom);
- }
- }
- else if (character == 'i') {
- int group, value1, value2;
- cin >> group >> value1 >> value2;
- heaps[group].heap_change_element(value1, value2);
- heaps[group].heap_build();
- }
- }
- for (int i = 0; i < 1000; ++i) {
- if (heaps[i].initialized == true)
- heaps[i].clearMemory();
- }
- //return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement