-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathe_p1_sum_wu.rs
97 lines (80 loc) · 2.09 KB
/
e_p1_sum_wu.rs
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
use std::{
collections::{BinaryHeap, HashSet},
fs::File,
io::{self, Write},
};
use crate::utils::Scanner;
struct Job {
deadline: i64,
weight: i64,
}
fn schedule_p1_sum_wu(jobs: &[Job]) -> (i64, Vec<i64>) {
let mut jobs: Vec<(usize, &Job)> = jobs.iter().enumerate().collect();
jobs.sort_by_key(|(_, j)| j.deadline);
let mut selected = HashSet::new();
let mut heap = BinaryHeap::new();
let mut time = 1;
jobs.iter().enumerate().for_each(|(i, (_, job))| {
selected.insert(i);
heap.push((-job.weight, i));
if job.deadline >= time {
time += 1;
} else {
selected.remove(&heap.pop().unwrap().1);
}
});
let mut result = vec![-1; jobs.len()];
let mut sum_wu = 0;
time = 0;
jobs.iter().enumerate().for_each(|(i, (j, job))| {
if selected.contains(&i) {
result[*j] = time;
time += 1;
} else {
result[*j] = (selected.len() + j) as i64;
sum_wu += job.weight;
}
});
(sum_wu, result)
}
#[allow(dead_code)]
fn main() -> Result<(), io::Error> {
let mut scanner = Scanner::from_file("p1sumwu.in")?;
let n: usize = scanner.read_next();
let jobs: Vec<Job> = (0..n)
.map(|_| Job {
deadline: scanner.read_next(),
weight: scanner.read_next(),
})
.collect();
let (size, schedule) = schedule_p1_sum_wu(&jobs);
let mut output = File::create("p1sumwu.out")?;
writeln!(output, "{}", size)?;
for i in schedule {
write!(output, "{} ", i)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::{schedule_p1_sum_wu, Job};
#[test]
fn sample() {
let jobs = vec![
Job {
deadline: 1,
weight: 2,
},
Job {
deadline: 1,
weight: 3,
},
Job {
deadline: 3,
weight: 1,
},
];
let actual = schedule_p1_sum_wu(&jobs);
assert_eq!(actual, (2, vec![2, 0, 1]));
}
}