結果

問題 No.1715 Dinner 2
ユーザー fukafukatani
提出日時 2021-10-22 23:30:05
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 2,599 bytes
コンパイル時間 13,106 ms
コンパイル使用メモリ 390,288 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-23 08:04:56
合計ジャッジ時間 13,500 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 34 WA * 4
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> src/main.rs:24:9
   |
24 |     for i in 0..n {
   |         ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: unused variable: `ng`
  --> src/main.rs:70:14
   |
70 |     let (ok, ng) = binary_search(-(d as i64) * 100000 - 1, 0, criterion);
   |              ^^ help: if this is intentional, prefix it with an underscore: `_ng`

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<usize>();
    let (n, d) = (v[0], v[1]);
    let mut poses = vec![];
    let mut negs = vec![];

    for i in 0..n {
        let v = read_vec::<i64>();
        let (p, q) = (v[0], v[1]);
        if q > p {
            poses.push((p, q));
        } else {
            negs.push((p, q));
        }
    }

    let criterion = |mid: i64| -> bool {
        let mut used = n;
        let mut cur = 0;
        'outer: for _ in 0..d {
            for (i, &(p, q)) in poses.iter().enumerate() {
                if i == used {
                    continue;
                }
                if cur - p >= mid {
                    cur += q - p;
                    used = i;
                    continue 'outer;
                }
            }
            let mut max_next = -std::i64::MAX;
            let mut max_i = n;
            for (i, &(p, q)) in negs.iter().enumerate() {
                if i + negs.len() == used {
                    continue;
                }
                if cur - p >= mid {
                    if max_next < cur + q - p {
                        max_next = cur + q - p;
                        max_i = i + negs.len();
                    }
                }
            }
            if max_next < mid {
                return false;
            }
            cur = max_next;
            used = max_i;
        }
        true
    };

    let (ok, ng) = binary_search(-(d as i64) * 100000 - 1, 0, criterion);
    println!("{}", ok);
}

type Input = i64;
fn binary_search<F>(lb: Input, ub: Input, criterion: F) -> (Input, Input)
where
    F: Fn(Input) -> bool,
{
    assert_eq!(criterion(lb), true);
    assert_eq!(criterion(ub), false);
    let mut ok = lb;
    let mut ng = ub;
    while ng - ok > 1 {
        let mid = (ng + ok) / 2;
        if criterion(mid) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    (ok, ng)
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
0