-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatchscheduling.cpp
More file actions
54 lines (44 loc) · 1.6 KB
/
batchscheduling.cpp
File metadata and controls
54 lines (44 loc) · 1.6 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
#include "batch_instance.hpp"
#include "scheduler.hpp"
#include <iostream>
int main(int argc, char **argv) {
// --- READ INPUT ---
auto inst = Instance::parse(std::cin);
// NOTE: The jobs are already sorted by release time, earliest first
std::cerr << inst;
// --- SIMULATE ONLINE SCHEDULER ---
Scheduler scheduler(inst);
int next_job_idx = 0;
std::optional<Job> next_job_to_finish;
while (next_job_idx < inst.jobs.size()) {
Job next_job_to_release = inst.jobs[next_job_idx];
next_job_to_finish =
scheduler.next_job_to_finish(); // This is an optional value
// Check which event happens first:
// either a job finishes or the next job gets released
if (next_job_to_finish.has_value() &&
next_job_to_finish.value().actual_end() <=
next_job_to_release.release_time) {
// Some currently running job finishes before the next job gets released
scheduler.finish_job(next_job_to_finish.value());
} else {
// New job gets released before any currently running job finishes
scheduler.queue_job(next_job_to_release);
++next_job_idx;
}
}
// Keep running until all jobs are finished
while (scheduler.still_running()) {
next_job_to_finish = scheduler.next_job_to_finish();
scheduler.finish_job(next_job_to_finish.value());
}
// --- OUTPUT SOLUTION ---
// Print n
std::cout << inst.n;
// Print ID's and start times
for (u32 id = 1; id <= inst.n; id++) {
std::cout << '\n' << id << ' ' << scheduler.start_times[id - 1];
}
// Flush stdout (don't use endl in loop, it's slower)
std::cout << std::endl;
}