Advertisement
Butol

Untitled

Mar 31st, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. #include <utility>
  4. #include <algorithm>
  5. #include <vector>
  6.  
  7. #define DEFAULT_INITIAL_SIZE 4
  8.  
  9. class Heap {
  10. public:
  11.     Heap();
  12.     ~Heap() { delete[] buffer; };
  13.     int PeekMax() const;
  14.     bool IsEmpty() const;
  15.     bool IsFull() const;
  16.     void Insert(int element);
  17.     int ExtractMax();
  18.     void printHeap();
  19.     int Size() {return length;}
  20.  
  21. private:
  22.     int* buffer;
  23.     int length;
  24.     int capacity;
  25.  
  26.     void siftDown( int index );
  27.     void siftUp( int i );
  28.     void resize();
  29. };
  30.  
  31. Heap::Heap() {
  32.     buffer = new int[DEFAULT_INITIAL_SIZE];
  33.     length = 0;
  34.     capacity = DEFAULT_INITIAL_SIZE;
  35. }
  36.  
  37. bool Heap::IsEmpty() const {
  38.     return length == 0;
  39. }
  40.  
  41. int Heap::PeekMax() const {
  42.     assert(!IsEmpty());
  43.     return buffer[0];
  44. }
  45.  
  46. void Heap::siftDown(int index) {
  47.     int left = 2 * index + 1;
  48.     int right = 2 * index + 2;
  49.     int largest = index;
  50.     if( left < length && buffer[left] > buffer[index])
  51.         largest = left;
  52.     if( right < length && buffer[right] > buffer[index])
  53.         largest = right;
  54.     if(largest != index ) {
  55.         std::swap(buffer[index], buffer[largest]);
  56.         siftDown( largest );
  57.     }
  58. }
  59.  
  60. void Heap::siftUp( int index ) {
  61.     while( index > 0 ) {
  62.         int parent = ( index - 1 ) / 2;
  63.         if(buffer[index] <= buffer[parent])
  64.             return;
  65.         std::swap(buffer[index], buffer[parent] );
  66.         index = parent;
  67.     }
  68. }
  69.  
  70. void Heap::Insert(int element) {
  71.     if (IsFull())
  72.         resize();
  73.  
  74.     length++;
  75.     buffer[length - 1] = element;
  76.     siftUp(length - 1);
  77. }
  78.  
  79. bool Heap::IsFull() const {
  80.     return length == capacity;
  81. }
  82.  
  83. void Heap::resize() {
  84.     int newBufferSize = std::max(capacity * 2, DEFAULT_INITIAL_SIZE);
  85.     int* newBuffer = new int[newBufferSize];
  86.     std::copy(buffer, buffer + length, newBuffer);
  87.     delete[] buffer;
  88.     buffer = newBuffer;
  89.     capacity = newBufferSize;
  90. }
  91.  
  92. int Heap::ExtractMax() {
  93.     assert( !IsEmpty() );
  94.     int result = buffer[0];
  95.     buffer[0] = buffer[--length];
  96.     if( !IsEmpty() ) {
  97.         siftDown(0);
  98.     }
  99.     return result;
  100. }
  101.  
  102. void Heap::printHeap() {
  103.     for(int i = 0; i < length; i++) {
  104.         std::cout << buffer[i] << " ";
  105.     }
  106.     std::cout << std::endl;
  107. }
  108.  
  109. int main() {
  110.  
  111.     Heap heap;
  112.     int countFruits = 0;
  113.     std::cin >> countFruits;
  114.  
  115.     for(int i = 0; i < countFruits; i++) {
  116.         int weightFruit = 0;
  117.         std::cin >> weightFruit;
  118.         heap.Insert(weightFruit);
  119.     }
  120.  
  121.     // Вес который может поднять мальчик
  122.     int loadCapacity = 0;
  123.     std::cin >> loadCapacity;
  124.  
  125.     int count = 0; // Количество шагов
  126.  
  127.     // Вес который сейчас держит Мальчик
  128.     int currentCapacity = 0;
  129.     // Вес текущего фрукта
  130.     int currentWeightFruit = 0;
  131.  
  132.     // Динамический массив для вставки элементов обратно в хип
  133.     std::vector<int> buffer;
  134.  
  135.     while(true) {
  136.  
  137.         if(!heap.IsEmpty())
  138.             currentWeightFruit = heap.PeekMax();
  139.         else
  140.             currentWeightFruit = 0;
  141.  
  142.         while (currentCapacity < loadCapacity) {
  143.             if(!heap.IsEmpty())
  144.                 currentWeightFruit = heap.ExtractMax();
  145.             currentCapacity += currentWeightFruit;
  146.             if(currentWeightFruit > 1)
  147.                 buffer.push_back(currentWeightFruit / 2);
  148.         }
  149.  
  150.         currentCapacity = 0;
  151.  
  152.         while(!buffer.empty()) {
  153.             heap.Insert(buffer.back());
  154.             buffer.pop_back();
  155.         }
  156.  
  157.         count++;
  158.  
  159.         if(heap.IsEmpty())
  160.             break;
  161.     }
  162.  
  163.     std::cout << count;
  164.  
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement