-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek2parallel.cpp
101 lines (89 loc) · 2.27 KB
/
week2parallel.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
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using std::vector;
using std::cin;
using std::cout;
using std::priority_queue;
class Worker {
public:
int id;
long long nextFreeTime;
Worker (int id) {
this->id = id;
nextFreeTime = 0;
}
};
struct WorkerCompare {
bool operator()(const Worker &w1, const Worker &w2) const {
if(w1.nextFreeTime == w2.nextFreeTime)
return w1.id > w2.id;
else
return w1.nextFreeTime > w2.nextFreeTime;
}
};
class JobQueue {
private:
int num_workers_;
vector<int> jobs_;
vector<int> assigned_workers_;
vector<long long> start_times_;
void WriteResponse() const {
for (int i = 0; i < jobs_.size(); ++i) {
cout << assigned_workers_[i] << " " << start_times_[i] << "\n";
}
}
void ReadData() {
int m;
cin >> num_workers_ >> m;
jobs_.resize(m);
for(int i = 0; i < m; ++i)
cin >> jobs_[i];
}
void AssignJobs() {
assigned_workers_.resize(jobs_.size());
start_times_.resize(jobs_.size());
vector<long long> next_free_time(num_workers_, 0);
for (int i = 0; i < jobs_.size(); ++i) {
int duration = jobs_[i];
int next_worker = 0;
for (int j = 0; j < num_workers_; ++j) {
if (next_free_time[j] < next_free_time[next_worker])
next_worker = j;
}
assigned_workers_[i] = next_worker;
start_times_[i] = next_free_time[next_worker];
next_free_time[next_worker] += duration;
}
}
void FastAssignJobs() {
assigned_workers_.resize(jobs_.size());
start_times_.resize(jobs_.size());
priority_queue<Worker, vector<Worker>, WorkerCompare> pq;
for(int i = 0; i < num_workers_; i++) {
pq.push(Worker(i));
}
for (int i = 0; i < jobs_.size(); i++) {
Worker freeThread = pq.top();
pq.pop();
assigned_workers_[i] = freeThread.id;
start_times_[i] = freeThread.nextFreeTime;
freeThread.nextFreeTime += jobs_[i];
pq.push(freeThread);
}
}
public:
void Solve() {
ReadData();
// AssignJobs();
FastAssignJobs();
WriteResponse();
}
};
int main() {
std::ios_base::sync_with_stdio(false);
JobQueue job_queue;
job_queue.Solve();
return 0;
}