Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- #include <utility>
- #include <algorithm>
- #include <vector>
- #define DEFAULT_INITIAL_SIZE 4
- class Heap {
- public:
- Heap();
- ~Heap() { delete[] buffer; };
- int PeekMax() const;
- bool IsEmpty() const;
- bool IsFull() const;
- void Insert(int element);
- int ExtractMax();
- void printHeap();
- int Size() {return length;}
- private:
- int* buffer;
- int length;
- int capacity;
- void siftDown( int index );
- void siftUp( int i );
- void resize();
- };
- Heap::Heap() {
- buffer = new int[DEFAULT_INITIAL_SIZE];
- length = 0;
- capacity = DEFAULT_INITIAL_SIZE;
- }
- bool Heap::IsEmpty() const {
- return length == 0;
- }
- int Heap::PeekMax() const {
- assert(!IsEmpty());
- return buffer[0];
- }
- void Heap::siftDown(int index) {
- int left = 2 * index + 1;
- int right = 2 * index + 2;
- int largest = index;
- if( left < length && buffer[left] > buffer[index])
- largest = left;
- if( right < length && buffer[right] > buffer[index])
- largest = right;
- if(largest != index ) {
- std::swap(buffer[index], buffer[largest]);
- siftDown( largest );
- }
- }
- void Heap::siftUp( int index ) {
- while( index > 0 ) {
- int parent = ( index - 1 ) / 2;
- if(buffer[index] <= buffer[parent])
- return;
- std::swap(buffer[index], buffer[parent] );
- index = parent;
- }
- }
- void Heap::Insert(int element) {
- if (IsFull())
- resize();
- length++;
- buffer[length - 1] = element;
- siftUp(length - 1);
- }
- bool Heap::IsFull() const {
- return length == capacity;
- }
- void Heap::resize() {
- int newBufferSize = std::max(capacity * 2, DEFAULT_INITIAL_SIZE);
- int* newBuffer = new int[newBufferSize];
- std::copy(buffer, buffer + length, newBuffer);
- delete[] buffer;
- buffer = newBuffer;
- capacity = newBufferSize;
- }
- int Heap::ExtractMax() {
- assert( !IsEmpty() );
- int result = buffer[0];
- buffer[0] = buffer[--length];
- if( !IsEmpty() ) {
- siftDown(0);
- }
- return result;
- }
- void Heap::printHeap() {
- for(int i = 0; i < length; i++) {
- std::cout << buffer[i] << " ";
- }
- std::cout << std::endl;
- }
- int main() {
- Heap heap;
- int countFruits = 0;
- std::cin >> countFruits;
- for(int i = 0; i < countFruits; i++) {
- int weightFruit = 0;
- std::cin >> weightFruit;
- heap.Insert(weightFruit);
- }
- // Вес который может поднять мальчик
- int loadCapacity = 0;
- std::cin >> loadCapacity;
- int count = 0; // Количество шагов
- // Вес который сейчас держит Мальчик
- int currentCapacity = 0;
- // Вес текущего фрукта
- int currentWeightFruit = 0;
- // Динамический массив для вставки элементов обратно в хип
- std::vector<int> buffer;
- while(true) {
- if(!heap.IsEmpty())
- currentWeightFruit = heap.PeekMax();
- else
- currentWeightFruit = 0;
- while (currentCapacity < loadCapacity) {
- if(!heap.IsEmpty())
- currentWeightFruit = heap.ExtractMax();
- currentCapacity += currentWeightFruit;
- if(currentWeightFruit > 1)
- buffer.push_back(currentWeightFruit / 2);
- }
- currentCapacity = 0;
- while(!buffer.empty()) {
- heap.Insert(buffer.back());
- buffer.pop_back();
- }
- count++;
- if(heap.IsEmpty())
- break;
- }
- std::cout << count;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement