complete 2805.rs 4366.rs 6235.rs 31614.rs 34412.rs
This commit is contained in:
53
storage/zeta/rs/completed/2805.rs
Normal file
53
storage/zeta/rs/completed/2805.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user