Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- #define MAX_VALUE 4294967295
- struct Number {
- unsigned int value;
- int seqIndex;
- int minHeapIndex;
- int maxHeapIndex;
- bool isTooBig;
- };
- Number mainSequence[60000];
- Number minSequence[60000];
- Number maxSequence[60000];
- void minHeapify(Number* sequence, int n, int heap_length) {
- while (true) {
- int parent = n; //parent index
- int left = 2 * n + 1; //left child index
- int right = left + 1; //right child index
- if (left < heap_length && sequence[left].value < sequence[n].value) {
- parent = left;
- }
- if (right < heap_length && sequence[right].value < sequence[parent].value) {
- parent = right;
- }
- if (parent == n) {
- return;
- }
- swap(sequence[n], sequence[parent]);
- sequence[n].minHeapIndex = n;
- sequence[parent].minHeapIndex = parent;
- mainSequence[sequence[n].seqIndex].minHeapIndex = n;
- mainSequence[sequence[parent].seqIndex].minHeapIndex = parent;
- n = parent;
- }
- }
- void maxHeapify(Number* sequence, int n, int heap_length) {
- while (true) {
- int parent = n; //parent index
- int left = 2 * n + 1; //left child index
- int right = left + 1; //right child index
- if (left < heap_length && sequence[left].value > sequence[n].value) {
- parent = left;
- }
- if (right < heap_length && sequence[right].value > sequence[parent].value) {
- parent = right;
- }
- if (parent == n) {
- return;
- }
- swap(sequence[n], sequence[parent]);
- sequence[n].maxHeapIndex = n;
- sequence[parent].maxHeapIndex = parent;
- mainSequence[sequence[n].seqIndex].maxHeapIndex = n;
- mainSequence[sequence[parent].seqIndex].maxHeapIndex = parent;
- n = parent;
- }
- }
- void swap(Number& a, Number& b) {
- Number tmp = a;
- a = b;
- b = tmp;
- }
- void buildMinHeap(Number* sequence, int heap_length) {
- for (int i = heap_length / 2 - 1; i >= 0; --i) {
- minHeapify(sequence, i, heap_length);
- }
- //heap is sorted by values - swap equal smallest numbers so that the one with smaller index is first
- for (int i = 1; i < heap_length; i++) {
- if (sequence[0].value == sequence[i].value) {
- if (sequence[0].seqIndex > sequence[i].seqIndex) {
- swap(sequence[0], sequence[i]);
- }
- }
- }
- }
- void buildMaxHeap(Number* sequence, int heap_length) {
- for (int i = heap_length / 2 - 1; i >= 0; --i) {
- maxHeapify(sequence, i, heap_length);
- }
- //heap is sorted by values - swap equal biggest numbers so that the one with smaller index is first
- for (int i = 1; i < heap_length; i++) {
- if (sequence[0].value == sequence[i].value) {
- if (sequence[0].seqIndex > sequence[i].seqIndex) {
- swap(sequence[0], sequence[i]);
- }
- }
- }
- }
- void removeMax(Number* sequence, int &heap_length) {
- swap(sequence[0], sequence[heap_length - 1]);
- heap_length--;
- maxHeapify(sequence, 0, heap_length);
- }
- void removeMin(Number* sequence, int &heap_length) {
- swap(sequence[0], sequence[heap_length - 1]);
- heap_length--;
- minHeapify(sequence, 0, heap_length);
- }
- void write(Number* sequence, int seq_length) {
- for (int i = 0; i < seq_length; i++) {
- if (sequence[i].isTooBig) {
- cout << "m ";
- }
- else
- cout << sequence[i].value << ' ';
- }
- cout << endl;
- }
- void collatz(Number* sequence, int &heap_length) {
- unsigned long long result = 0;
- if (sequence[0].value % 2 == 0) {
- result = sequence[0].value / 2;
- }
- else {
- result = 3ULL * sequence[0].value + 1;
- }
- if (result > MAX_VALUE) { // result is too big - remove from heaps
- mainSequence[sequence[0].seqIndex].isTooBig = true;
- removeMax(sequence, heap_length);
- heap_length--;
- }
- else if (result == 1) { // final value - remove from heaps
- mainSequence[sequence[0].seqIndex].value = 1;
- sequence[0].value = 1;
- removeMin(sequence, heap_length);
- heap_length--;
- }
- else { // result is correct
- mainSequence[sequence[0].seqIndex].value = (unsigned int)result;
- sequence[0].value = (unsigned int)result;
- }
- }
- int main() {
- int seq_length; // number of elements of the sequence < 60000
- int minHeap_length;
- int maxHeap_length;
- cin >> seq_length;
- minHeap_length = seq_length;
- maxHeap_length = seq_length;
- // second row - the sequence
- for (int i = 0; i < seq_length; i++) {
- cin >> mainSequence[i].value;
- mainSequence[i].seqIndex = i;
- }
- int heap_i = 0;
- for (int i = 0; i < seq_length; i++) {
- if (mainSequence[i].value != 1) { // ommiting the '1's
- minSequence[heap_i] = mainSequence[i];
- minSequence[heap_i].minHeapIndex = heap_i;
- maxSequence[heap_i] = mainSequence[i];
- maxSequence[heap_i].maxHeapIndex = heap_i;
- heap_i++;
- }
- else {
- minHeap_length--;
- maxHeap_length--;
- }
- }
- // third + fourth row - number of commands + commands
- int q; // <= 2000
- cin >> q;
- buildMinHeap(minSequence, minHeap_length);
- buildMaxHeap(maxSequence, maxHeap_length);
- for (int i = 0; i < q; i++) {
- int k; // how many times will the Collatz function be applied? <= 2000
- char size; // s or l - smallest or largest
- cin >> k >> size;
- if (size == 's') {
- for (int i = 0; i < k; i++) {
- //buildMinHeap(minSequence, heap_length);
- collatz(minSequence, minHeap_length);
- minHeapify(minSequence, 0, minHeap_length);
- }
- }
- if (size == 'l') {
- for (int i = 0; i < k; i++) {
- //buildMaxHeap(maxSequence, heap_length);
- collatz(maxSequence, maxHeap_length);
- maxHeapify(maxSequence, 0, maxHeap_length);
- }
- }
- }
- // output
- write(mainSequence, seq_length);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement