complete 2805.rs 4366.rs 6235.rs 31614.rs 34412.rs

This commit is contained in:
2026-03-30 03:10:37 +09:00
parent 5c6ad569c4
commit c85e661c6c
5 changed files with 148 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
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 m = iter.next().unwrap();
let mut arr = (0..n).map(|_| iter.next().unwrap()).collect::<Vec<_>>();
arr.sort();
let mut lo = 0;
let mut hi = *arr.last().unwrap();
let mut cum = vec![0];
for &e in arr.iter() {
cum.push(cum.last().unwrap() + e);
}
while hi >= lo && hi - lo > 1 {
let mid = (hi + lo) / 2;
let key = arr.binary_search(&mid);
let s = match key {
Ok(idx) => cum[n] - cum[idx] - (n - idx) * mid,
Err(idx) => cum[n] - cum[idx] - (n - idx) * mid,
};
if s < m {
hi = mid - 1;
} else {
if lo == mid {
hi = lo;
}
lo = mid;
}
}
{
let key = arr.binary_search(&hi);
let s = match key {
Ok(idx) => cum[n] - cum[idx] - (n - idx) * hi,
Err(idx) => cum[n] - cum[idx] - (n - idx) * hi,
};
if s >= m {
println!("{}", hi);
} else {
println!("{}", lo);
}
}
}