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
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
//#include <gtest/gtest.h>
using std::vector;
using std::cin;
using std::cout;
using std::pair;
using std::make_pair;
class JobQueue {
private:
vector<int> assigned_workers_;
vector<long long> start_times_;
void WriteResponse() const {
for (size_t 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() {
// Naive algorithm
assigned_workers_.resize(jobs.size());
start_times_.resize(jobs.size());
vector<long long> next_free_time(num_workers, 0);
for (size_t 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;
}
}
public:
int num_workers;
vector<int> jobs;
void Solve() {
ReadData();
AssignJobsImproved();
WriteResponse();
}
const vector<int>& assigned_workers() const {
return assigned_workers_;
}
const vector<long long>& start_times() const {
return start_times_;
}
void AssignJobsImproved() {
std::priority_queue<pair<long, int>> pq;
assigned_workers_.resize(jobs.size());
start_times_.resize(jobs.size());
for (size_t i = 0; i < num_workers; ++i) {
pq.push(make_pair(0, -i));
}
for (size_t i = 0; i < jobs.size(); ++i) {
pair<long, int> l = pq.top();
pq.pop();
start_times_[i] = -l.first;
assigned_workers_[i] = -l.second;
pq.push(make_pair(l.first - jobs[i], l.second));
}
}
};
int main() {
std::ios_base::sync_with_stdio(false);
JobQueue job_queue;
job_queue.Solve();
return 0;
}
//TEST(ParralelProcessing, t) {
// vector<int> jobs = { 1, 2, 3, 4, 5 };
// vector<int> expected_assigned_workers = { 0, 1, 0, 1, 0 };
// vector<long long> expected_start_times = { 0, 0, 1, 2, 4 };
// JobQueue jq;
// jq.num_workers = 2;
// jq.jobs = jobs;
// jq.AssignJobsImproved();
// ASSERT_EQ(expected_assigned_workers, jq.assigned_workers());
// ASSERT_EQ(expected_start_times, jq.start_times());
//}
//
//TEST(ParralelProcessing, t1) {
// vector<int> jobs = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
// 1, 1 };
// vector<int> expected_assigned_workers = { 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
// 3, 0, 1, 2, 3, 0, 1, 2, 3 };
// vector<long long> expected_start_times = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
// 2, 3, 3, 3, 3, 4, 4, 4, 4 };
// JobQueue jq;
// jq.num_workers = 4;
// jq.jobs = jobs;
// jq.AssignJobsImproved();
// ASSERT_EQ(expected_assigned_workers, jq.assigned_workers());
// ASSERT_EQ(expected_start_times, jq.start_times());
//}
|