Skip to content

Commit 7521d95

Browse files
committed
parallel processing
1 parent 1c8f32f commit 7521d95

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

2/proc.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
template <class T>
5+
class MinHeap
6+
{
7+
public:
8+
MinHeap(int processors) {
9+
int tasks;
10+
std::cin >> tasks;
11+
int i = 0;
12+
13+
while (i < processors) {
14+
_heap.push_back(std::make_pair(0, i));
15+
++i;
16+
}
17+
_size = processors;
18+
_maxSize = _heap.capacity();
19+
BuildHeap();
20+
21+
i = 0;
22+
while (i < tasks)
23+
{
24+
int task;
25+
std::cin >> task;
26+
ChangePriority(0, task);
27+
++i;
28+
}
29+
}
30+
~MinHeap() {}
31+
32+
void BuildHeap() {
33+
for (int i = _size / 2 + 1 ; i > -1 ; --i)
34+
SiftDown(i);
35+
}
36+
37+
int Parent(int i) {
38+
if (!i)
39+
return 0;
40+
return (i - 1) / 2;
41+
}
42+
int LeftChild(int i) { return 2 * i + 1; }
43+
int RightChild(int i) { return 2 * i + 2; }
44+
int size() { return _size; }
45+
int maxSize() { return _maxSize; }
46+
47+
void SiftDown(int i) {
48+
int minIndex = i;
49+
int l = LeftChild(i);
50+
51+
if (l < _size && _heap[l] < _heap[minIndex])
52+
minIndex = l;
53+
54+
int r = RightChild(i);
55+
if (r < _size && _heap[r] < _heap[minIndex])
56+
minIndex = r;
57+
58+
if (i != minIndex)
59+
{
60+
std::swap(_heap[i], _heap[minIndex]);
61+
SiftDown(minIndex);
62+
}
63+
}
64+
65+
void ChangePriority(int i, unsigned long time) {
66+
_log.push_back(std::make_pair(_heap[i].second, _heap[i].first));
67+
_heap[i].first += time;
68+
SiftDown(i);
69+
}
70+
std::vector<std::pair<long, int>> const & heap() { return _heap; }
71+
std::vector<std::pair<int, unsigned long>> const & log() { return _log;}
72+
73+
private:
74+
std::vector<T> _heap;
75+
std::vector<std::pair<int, unsigned long>> _log;
76+
int _size;
77+
int _maxSize;
78+
};
79+
80+
int main(void)
81+
{
82+
int processors;
83+
84+
std::cin >> processors ;
85+
MinHeap<std::pair<unsigned long, int>> heap(processors);
86+
for (int i = 0; i < heap.log().size(); ++i)
87+
std::cout << heap.log()[i].first << " " << heap.log()[i].second << std::endl;
88+
return 0;
89+
}

0 commit comments

Comments
 (0)