Skip to content

Commit

Permalink
tessoku-book/c07
Browse files Browse the repository at this point in the history
  • Loading branch information
emanon001 committed Jan 9, 2023
1 parent 0f1daee commit bc9d12d
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions tessoku-book/src/bin/c07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,53 @@ use proconio::marker::*;
#[allow(unused_imports)]
use std::collections::*;

pub fn bsearch<F>(ok: i64, ng: i64, pred: F) -> Option<i64>
where
F: Fn(i64) -> bool,
{
let orig_ok = ok;
let mut ok = ok;
let mut ng = ng;
while (ok - ng).abs() > 1 {
let mid = (ok + ng) / 2;
if pred(mid) {
ok = mid;
} else {
ng = mid;
}
}
if ok == orig_ok {
None
} else {
Some(ok)
}
}

fn solve() {
input! {
n: usize,
mut cv: [i64; n],
q: usize,
xv: [i64; q]
};

cv.sort();
let cusum = cv
.into_iter()
.scan(0_i64, |state, c| {
*state += c;
Some(*state)
})
.collect::<Vec<_>>();
for x in xv {
let res = bsearch(-1, n as i64, |i| {
let i = i as usize;
cusum[i] <= x
})
.map(|i| i + 1)
.unwrap_or(0);
println!("{}", res);
}
}

fn main() {
Expand Down

0 comments on commit bc9d12d

Please sign in to comment.