forked from rudi8848/data_structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.2.parallel_processing.cpp
More file actions
86 lines (73 loc) · 1.61 KB
/
2.2.parallel_processing.cpp
File metadata and controls
86 lines (73 loc) · 1.61 KB
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
#include <iostream>
#include <vector>
template <class T>
class MinHeap
{
public:
MinHeap(int processors) {
int tasks;
std::cin >> tasks;
int i = 0;
while (i < processors) {
_heap.push_back(std::make_pair(0, i));
++i;
}
_size = processors;
BuildHeap();
i = 0;
while (i < tasks)
{
int task;
std::cin >> task;
ChangePriority(task);
++i;
}
}
~MinHeap() {}
void BuildHeap() {
for (int i = _size / 2 + 1 ; i > -1 ; --i)
SiftDown(i);
}
int Parent(int i) {
if (!i)
return 0;
return (i - 1) / 2;
}
int LeftChild(int i) { return 2 * i + 1; }
int RightChild(int i) { return 2 * i + 2; }
int size() { return _size; }
void SiftDown(int i) {
int minIndex = i;
int l = LeftChild(i);
if (l < _size && _heap[l] < _heap[minIndex])
minIndex = l;
int r = RightChild(i);
if (r < _size && _heap[r] < _heap[minIndex])
minIndex = r;
if (i != minIndex)
{
std::swap(_heap[i], _heap[minIndex]);
SiftDown(minIndex);
}
}
void ChangePriority(unsigned long time) {
_log.push_back(std::make_pair(_heap[0].second, _heap[0].first));
_heap[0].first += time;
SiftDown(0);
}
std::vector<T> const & heap() { return _heap; }
std::vector<std::pair<int, unsigned long>> const & log() { return _log;}
private:
std::vector<T> _heap;
std::vector<std::pair<int, unsigned long>> _log;
int _size;
};
int main(void)
{
int processors;
std::cin >> processors ;
MinHeap<std::pair<unsigned long, int>> heap(processors);
for (int i = 0; i < heap.log().size(); ++i)
std::cout << heap.log()[i].first << " " << heap.log()[i].second << std::endl;
return 0;
}