Advertisement
Guest User

Untitled

a guest
May 25th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.45 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. #define MAX_VALUE 4294967295
  4.  
  5. struct Number {
  6.     unsigned int value;
  7.     int seqIndex;
  8.     int minHeapIndex;
  9.     int maxHeapIndex;
  10.     bool isTooBig;
  11. };
  12. Number mainSequence[60000];
  13. Number minSequence[60000];
  14. Number maxSequence[60000];
  15.  
  16. void minHeapify(Number* sequence, int n, int heap_length) {
  17.     while (true) {
  18.         int parent = n; //parent index
  19.         int left = 2 * n + 1; //left child index
  20.         int right = left + 1; //right child index
  21.  
  22.         if (left < heap_length && sequence[left].value < sequence[n].value) {
  23.             parent = left;
  24.         }
  25.         if (right < heap_length && sequence[right].value < sequence[parent].value) {
  26.             parent = right;
  27.         }
  28.         if (parent == n) {
  29.             return;
  30.         }
  31.  
  32.         swap(sequence[n], sequence[parent]);
  33.         sequence[n].minHeapIndex = n;
  34.         sequence[parent].minHeapIndex = parent;
  35.  
  36.         mainSequence[sequence[n].seqIndex].minHeapIndex = n;
  37.         mainSequence[sequence[parent].seqIndex].minHeapIndex = parent;
  38.  
  39.         n = parent;
  40.     }
  41. }
  42.  
  43. void maxHeapify(Number* sequence, int n, int heap_length) {
  44.     while (true) {
  45.         int parent = n; //parent index
  46.         int left = 2 * n + 1; //left child index
  47.         int right = left + 1; //right child index
  48.  
  49.         if (left < heap_length && sequence[left].value > sequence[n].value) {
  50.             parent = left;
  51.         }
  52.         if (right < heap_length && sequence[right].value > sequence[parent].value) {
  53.             parent = right;
  54.         }
  55.         if (parent == n) {
  56.             return;
  57.         }
  58.  
  59.         swap(sequence[n], sequence[parent]);
  60.         sequence[n].maxHeapIndex = n;
  61.         sequence[parent].maxHeapIndex = parent;
  62.  
  63.         mainSequence[sequence[n].seqIndex].maxHeapIndex = n;
  64.         mainSequence[sequence[parent].seqIndex].maxHeapIndex = parent;
  65.  
  66.         n = parent;
  67.     }
  68. }
  69.  
  70. void swap(Number& a, Number& b) {
  71.     Number tmp = a;
  72.     a = b;
  73.     b = tmp;
  74. }
  75.  
  76. void buildMinHeap(Number* sequence, int heap_length) {
  77.     for (int i = heap_length / 2 - 1; i >= 0; --i) {
  78.         minHeapify(sequence, i, heap_length);
  79.     }
  80.  
  81.     //heap is sorted by values - swap equal smallest numbers so that the one with smaller index is first
  82.     for (int i = 1; i < heap_length; i++) {
  83.         if (sequence[0].value == sequence[i].value) {
  84.             if (sequence[0].seqIndex > sequence[i].seqIndex) {
  85.                 swap(sequence[0], sequence[i]);
  86.             }
  87.         }
  88.     }
  89. }
  90.  
  91. void buildMaxHeap(Number* sequence, int heap_length) {
  92.     for (int i = heap_length / 2 - 1; i >= 0; --i) {
  93.         maxHeapify(sequence, i, heap_length);
  94.     }
  95.  
  96.     //heap is sorted by values - swap equal biggest numbers so that the one with smaller index is first
  97.     for (int i = 1; i < heap_length; i++) {
  98.         if (sequence[0].value == sequence[i].value) {
  99.             if (sequence[0].seqIndex > sequence[i].seqIndex) {
  100.                 swap(sequence[0], sequence[i]);
  101.             }
  102.         }      
  103.     }
  104. }
  105.  
  106. void removeMax(Number* sequence, int &heap_length) {
  107.     swap(sequence[0], sequence[heap_length - 1]);
  108.     heap_length--;
  109.     maxHeapify(sequence, 0, heap_length);
  110. }
  111.  
  112. void removeMin(Number* sequence, int &heap_length) {
  113.     swap(sequence[0], sequence[heap_length - 1]);
  114.     heap_length--;
  115.     minHeapify(sequence, 0, heap_length);
  116. }
  117.  
  118. void write(Number* sequence, int seq_length) {
  119.     for (int i = 0; i < seq_length; i++) {
  120.         if (sequence[i].isTooBig) {
  121.             cout << "m ";
  122.         }
  123.         else
  124.             cout << sequence[i].value << ' ';
  125.     }
  126.     cout << endl;
  127. }
  128.  
  129. void collatz(Number* sequence, int &heap_length) {
  130.     unsigned long long result = 0;
  131.  
  132.     if (sequence[0].value % 2 == 0) {
  133.         result = sequence[0].value / 2;
  134.     }
  135.     else {
  136.         result = 3ULL * sequence[0].value + 1;
  137.     }
  138.  
  139.     if (result > MAX_VALUE) { // result is too big - remove from heaps
  140.         mainSequence[sequence[0].seqIndex].isTooBig = true;
  141.         removeMax(sequence, heap_length);
  142.         heap_length--;
  143.     }
  144.     else if (result == 1) { // final value - remove from heaps
  145.         mainSequence[sequence[0].seqIndex].value = 1;
  146.         sequence[0].value = 1;
  147.         removeMin(sequence, heap_length);
  148.         heap_length--;
  149.     }
  150.     else { // result is correct
  151.         mainSequence[sequence[0].seqIndex].value = (unsigned int)result;
  152.         sequence[0].value = (unsigned int)result;
  153.     }
  154. }
  155.  
  156. int main() {
  157.     int seq_length; // number of elements of the sequence < 60000
  158.     int minHeap_length;
  159.     int maxHeap_length;
  160.  
  161.     cin >> seq_length;
  162.    
  163.     minHeap_length = seq_length;
  164.     maxHeap_length = seq_length;
  165.  
  166.     // second row - the sequence
  167.     for (int i = 0; i < seq_length; i++) {
  168.         cin >> mainSequence[i].value;
  169.         mainSequence[i].seqIndex = i;
  170.     }
  171.     int heap_i = 0;
  172.     for (int i = 0; i < seq_length; i++) {
  173.         if (mainSequence[i].value != 1) { // ommiting the '1's
  174.             minSequence[heap_i] = mainSequence[i];
  175.             minSequence[heap_i].minHeapIndex = heap_i;
  176.             maxSequence[heap_i] = mainSequence[i];
  177.             maxSequence[heap_i].maxHeapIndex = heap_i;
  178.             heap_i++;
  179.         }
  180.         else {
  181.             minHeap_length--;
  182.             maxHeap_length--;
  183.         }
  184.     }
  185.  
  186.     // third + fourth row - number of commands + commands
  187.     int q; // <= 2000
  188.     cin >> q;
  189.  
  190.     buildMinHeap(minSequence, minHeap_length);
  191.     buildMaxHeap(maxSequence, maxHeap_length);
  192.    
  193.     for (int i = 0; i < q; i++) {
  194.         int k; // how many times will the Collatz function be applied? <= 2000
  195.         char size; // s or l - smallest or largest
  196.         cin >> k >> size;
  197.  
  198.         if (size == 's') {
  199.             for (int i = 0; i < k; i++) {
  200.                 //buildMinHeap(minSequence, heap_length);
  201.                 collatz(minSequence, minHeap_length);
  202.                 minHeapify(minSequence, 0, minHeap_length);
  203.             }
  204.         }
  205.         if (size == 'l') {
  206.             for (int i = 0; i < k; i++) {
  207.                 //buildMaxHeap(maxSequence, heap_length);
  208.                 collatz(maxSequence, maxHeap_length);
  209.                 maxHeapify(maxSequence, 0, maxHeap_length);
  210.             }
  211.         }
  212.     }
  213.  
  214.     // output
  215.     write(mainSequence, seq_length);
  216.  
  217.     system("pause");
  218.     return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement