https://atcoder.jp/contests/abc423/tasks/abc423_d
レストラン側で実際に店を出る時間でPriorityQueueに団体客を入れればよいです。
// Long Waiting #![allow(non_snake_case)] use std::cmp::max; use std::collections::BinaryHeap; //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } //////////////////// Group //////////////////// #[derive(Debug, Eq, PartialEq)] struct Group { A: i64, // 来店時間 B: i64, // 滞在時間 C: usize, // 人数 D: i64 // 実際に出る時間 } impl Group { fn read() -> Group { let v: Vec<i64> = read_vec(); let (A, B, C) = (v[0], v[1], v[2] as usize); Group { A, B, C, D: 0 } } } impl Ord for Group { fn cmp(&self, other: &Self) -> std::cmp::Ordering { other.D.cmp(&self.D) } } impl PartialOrd for Group { fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { Some(self.cmp(other)) } } //////////////////// Restaurant //////////////////// struct Restaurant { Q: BinaryHeap<Group>, S: usize, // 今の空き T: i64 // 時刻 } impl Restaurant { fn new(K: usize) -> Restaurant { Restaurant { Q: BinaryHeap::new(), S: K, T: 0 } } // 客を追加 fn add(&mut self, mut group: Group) { group.D = max(self.T, group.A) + group.B; self.S -= group.C; self.Q.push(group) } // 客が出て行くまで時間を進める fn proceed(&mut self) { if let Some(group) = self.Q.pop() { self.T = group.D; self.S += group.C } } } //////////////////// process //////////////////// fn read_input() -> (usize, Vec<Group>) { let v: Vec<usize> = read_vec(); let (N, K) = (v[0], v[1]); let groups: Vec<Group> = (0..N).map(|_| Group::read()).collect(); (K, groups) } fn F(K: usize, groups: Vec<Group>) { let mut restaurant = Restaurant::new(K); for group in groups { while restaurant.S < group.C { restaurant.proceed() } println!("{}", max(group.A, restaurant.T)); restaurant.add(group) } } fn main() { let (K, groups) = read_input(); F(K, groups) }