-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
307 lines (247 loc) · 6.7 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <iostream>
#include <assert.h>
template <class T>
class LilArray {
public:
LilArray(): buf(0), currentSize(0), maxSize(0) { }
LilArray(T* array, int size);
//~LilArray() { delete [] buf; }
// T& operator=(const T& val);
// T& operator=(T&& val);
int count() const { return currentSize; }
void swipe(const int &index1, const int &index2);
const T& at(const int &index1);
void print();
bool is_empty() { return currentSize == 0; }
void push_back(T val);
void pop();
const T& first();
const T& last();
private:
T* buf;
int currentSize;
int maxSize;
int defaultSize = 1;
bool is_full() { return currentSize == maxSize; }
void grow();
};
template <typename T>
LilArray<T>::LilArray(T* array, int size) {
currentSize = 0;
maxSize = 4;
while(maxSize < size) {
maxSize *= 2;
}
buf = new T[maxSize];
for (int i = 0; i < size; i++) {
buf[currentSize++] = array[i];
}
}
template <typename T>
void LilArray<T>::swipe(const int &index1, const int &index2) {
assert(index1 >= 0);
assert(index2 >= 0);
assert(index1 <= currentSize);
assert(index2 <= currentSize);
T tmp = buf[index1];
buf[index1] = buf[index2];
buf[index2] = tmp;
}
template <typename T>
const T& LilArray<T>::at(const int &index1) {
assert(index1 >= 0);
assert(index1 <= currentSize);
return buf[index1];
}
template <typename T>
const T& LilArray<T>::first() {
assert(!is_empty());
return buf[0];
}
template <typename T>
const T& LilArray<T>::last() {
assert(!is_empty());
return buf[currentSize - 1];
}
template <typename T>
void LilArray<T>::pop() {
if (currentSize > 0) {
currentSize--;
}
}
template <typename T>
void LilArray<T>::print() {
if (!is_empty()) {
std::cout << "LilArray" << std::endl;
std::cout << "bufferSize: " << maxSize << std::endl;
std::cout << "currentSize: " << currentSize << std::endl;
std::cout << "first: " << first() << std::endl;
std::cout << "last: " << last() << std::endl;
for (int i = 0; i < currentSize; i++) {
std::cout << buf[i] << " ";
if (i == currentSize - 1) {
std::cout << std::endl << std::endl;
}
}
} else {
std::cout << "LilArray" << std::endl;
std::cout << "Is Empty: " << std::endl << std::endl;
}
}
template <typename T>
void LilArray<T>::push_back(T val) {
if (is_full()) {
grow();
}
buf[currentSize++] = val;
}
template <typename T>
void LilArray<T>::grow() {
int newBufferSize = std::max(maxSize * 2, defaultSize);
T* tmp_arr = new T[newBufferSize];
for (int i = 0; i < currentSize; i++) {
tmp_arr[i] = buf[i];
}
delete [] buf;
maxSize = newBufferSize;
buf = tmp_arr;
}
template <class T>
struct DefaultComparator {
bool operator() (const T& l, const T& r) const {
return l < r;
}
};
template <class T, class Comparator = DefaultComparator<T>>
class Heap {
public:
Heap(Comparator comp = Comparator());
Heap(T* arr, int arr_size, Comparator comp = Comparator());
~Heap() { delete buf; }
const T& top() const { return buf->first(); }
void extract_top();
void pop();
void push(const T&);
void push(T&&);
bool is_empty() const { return buf->is_empty(); }
int count() const { return buf->count(); }
private:
LilArray<T>* buf;
Comparator comp;
void sift_up(int i);
void sift_down(int i);
void heapify();
};
template <typename T, typename Comparator>
Heap<T, Comparator>::Heap(T* arr, int arr_size, Comparator comp) {
this->comp = comp;
buf = new LilArray<T>(arr, arr_size);
heapify();
}
template <typename T, typename Comparator>
Heap<T, Comparator>::Heap(Comparator comp) {
this->comp = comp;
buf = new LilArray<T>();
}
template <typename T, typename Comparator>
void Heap<T, Comparator>::sift_down(int i) {
int left = 2 * i + 1;
int right = 2 * i + 2;
int curSize = count();
int largest = i;
if (left < curSize && buf->at(left) > buf->at(i)) {
largest = left;
}
if (right < curSize && buf->at(right) > buf->at(largest)) {
largest = right;
}
if (i != largest) {
buf->swipe(i, largest);
sift_down(largest);
}
}
template <typename T, typename Comparator>
void Heap<T, Comparator>::sift_up(int i) {
while(i > 0) {
int parent = (i - 1) / 2;
if (buf->at(i) <= buf->at(parent))
return;
buf->swipe(i, parent);
i = parent;
}
}
template <typename T, typename Comparator>
void Heap<T, Comparator>::heapify() {
int sizee = count();
for (int i = sizee / 2 - 1; i >= 0; i--) {
sift_down(i);
}
}
template <typename T, typename Comparator>
void Heap<T, Comparator>::push(const T& val) {
buf->push_back(val);
int size = count();
sift_up(size - 1);
}
template <typename T, typename Comparator>
void Heap<T, Comparator>::push(T&& val) {
buf->push_back(val);
int size = count();
sift_up(size - 1);
}
template <typename T, typename Comparator>
void Heap<T, Comparator>::extract_top() {
assert(!is_empty());
int last_index = count()-1;
buf->swipe(0, last_index);
buf->pop();
sift_down(0);
}
template <typename T, typename Comparator>
void Heap<T, Comparator>::pop() {
assert(!is_empty());
buf->pop();
}
int cormorant(Heap<int> * heap, int weight) {
int *tmp_arr = new int[heap->count()];
int current_size = 0;
assert(heap->top() <= weight);
int iterations = 0;
while (!heap->is_empty()) {
int tmp_weight = weight;
current_size = 0;
while(!heap->is_empty() && heap->top() <= tmp_weight) {
int popped = heap->top();
heap->extract_top();
tmp_arr[current_size++] = popped;
tmp_weight -= popped;
}
for (int i = 0; i < current_size; i++) {
if (tmp_arr[i] != 1) {
heap->push(tmp_arr[i] / 2);
}
}
iterations++;
}
free(tmp_arr);
delete heap;
return iterations;
}
int run(std::istream& input, std::ostream& output) {
int size;
input >> size;
assert(size > 0);
Heap<int> * heap = new Heap<int>();
for (int i = 0; i < size; i++) {
int num;
input >> num;
heap->push(num);
}
int weight;
input >> weight;
output << cormorant(heap, weight) << "\n";
return 0;
}
int main(int argc, const char * argv[]) {
return run(std::cin, std::cout);
}