complete 13334.rs 20207.rs

This commit is contained in:
2026-01-17 00:17:04 -08:00
parent 8f4d80c08f
commit ffc4d85822
2 changed files with 115 additions and 0 deletions

View File

@@ -0,0 +1,69 @@
use std::{
cmp::{max, Reverse},
collections::BinaryHeap,
i32,
io::{stdin, Read},
};
fn main() {
let mut buffer = Vec::new();
stdin().read_to_end(&mut buffer).unwrap();
let mut tokens = buffer.split(|&b| b <= b' ');
let mut next_i32 = || {
tokens.find(|s| !s.is_empty()).map(|s| {
let mut res = 0i32;
let mut neg = false;
let mut start = 0;
if s[0] == b'-' {
neg = true;
start = 1;
}
for i in start..s.len() {
res = res * 10 + (s[i] - b'0') as i32;
}
if neg {
-res
} else {
res
}
})
};
let n = next_i32().unwrap();
let mut commute_infos = (0..n)
.map(|_| {
let (a, b) = (next_i32().unwrap(), next_i32().unwrap());
if b > a {
(a, b)
} else {
(b, a)
}
})
.collect::<Vec<(i32, i32)>>();
let d = next_i32().unwrap();
commute_infos.sort_unstable_by_key(|&(_, b)| b);
let mut max_benefit = 0;
let mut deq: BinaryHeap<Reverse<i32>> = BinaryHeap::new();
for &(h, o) in commute_infos.iter() {
if o - h > d {
continue;
}
deq.push(Reverse(h));
while let Some(ch) = deq.peek() {
if ch.0 < o - d {
deq.pop();
} else {
break;
}
}
max_benefit = max(max_benefit, deq.len());
}
println!("{}", max_benefit);
}

View File

@@ -0,0 +1,46 @@
use std::cmp::max;
use std::io::{read_to_string, stdin};
fn main() {
let temp = read_to_string(stdin()).unwrap();
let mut iter = temp
.split_ascii_whitespace()
.map(|x| x.parse::<usize>().unwrap());
let n = iter.next().unwrap();
let mut schedules = (0..n)
.map(|_| (iter.next().unwrap(), iter.next().unwrap()))
.collect::<Vec<(usize, usize)>>();
schedules.sort_by_key(|x| x.0);
let mut calendar = vec![0usize; 366];
for &(s, e) in schedules.iter() {
for i in s..=e {
calendar[i] += 1;
}
}
let mut local_max_height = 0;
let mut starting = 0;
let mut sheet_area = 0;
for (i, &c) in calendar.iter().enumerate() {
if c == 0 {
sheet_area += (i - starting) * local_max_height;
local_max_height = 0;
} else {
if local_max_height == 0 {
starting = i;
}
local_max_height = max(c, local_max_height);
}
}
if local_max_height > 0 {
sheet_area += (366 - starting) * local_max_height;
}
println!("{}", sheet_area);
}