結果

問題 No.1715 Dinner 2
ユーザー fukafukatanifukafukatani
提出日時 2021-10-23 11:16:49
言語 Rust
(1.72.1)
結果
WA  
実行時間 -
コード長 2,671 bytes
コンパイル時間 3,484 ms
コンパイル使用メモリ 171,260 KB
実行使用メモリ 9,760 KB
最終ジャッジ日時 2023-10-25 07:05:24
合計ジャッジ時間 10,384 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 WA -
testcase_04 AC 1 ms
4,348 KB
testcase_05 WA -
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,348 KB
testcase_08 WA -
testcase_09 AC 1 ms
4,348 KB
testcase_10 WA -
testcase_11 AC 425 ms
7,912 KB
testcase_12 AC 334 ms
6,856 KB
testcase_13 AC 273 ms
6,064 KB
testcase_14 AC 329 ms
6,856 KB
testcase_15 AC 338 ms
6,856 KB
testcase_16 AC 317 ms
6,592 KB
testcase_17 AC 192 ms
4,744 KB
testcase_18 AC 4 ms
4,348 KB
testcase_19 AC 8 ms
4,348 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 31 ms
4,348 KB
testcase_26 AC 21 ms
4,348 KB
testcase_27 AC 21 ms
4,348 KB
testcase_28 WA -
testcase_29 AC 30 ms
4,348 KB
testcase_30 AC 29 ms
4,348 KB
testcase_31 AC 31 ms
4,348 KB
testcase_32 AC 535 ms
9,760 KB
testcase_33 AC 529 ms
9,760 KB
testcase_34 AC 544 ms
9,760 KB
testcase_35 AC 554 ms
9,760 KB
testcase_36 AC 0 ms
4,348 KB
testcase_37 WA -
testcase_38 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> Main.rs:23:9
   |
23 |     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`
  --> Main.rs:63:14
   |
63 |     let (ok, ng) = binary_search(-(d as i64) * 100000000 - 1, 0, criterion);
   |              ^^ help: if this is intentional, prefix it with an underscore: `_ng`

warning: 2 warnings emitted

ソースコード

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 negs = vec![];

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

    let criterion = |mid: i64| -> bool {
        let mut dp = vec![vec![-std::i64::MAX / 4; n]; d];
        for (i, &(p, q)) in negs.iter().enumerate() {
            if -p >= mid {
                dp[0][i] = q - p;
            }
        }
        for di in 1..d {
            let mut from_left = vec![0; n];
            from_left[0] = dp[di - 1][0];
            for i in 1..n {
                from_left[i] = max(from_left[i - 1], dp[di - 1][i]);
            }
            let mut from_right = vec![0; n];
            from_right[n - 1] = dp[di - 1][n - 1];
            for i in (0..n - 1).rev() {
                from_right[i] = max(from_right[i + 1], dp[di - 1][i]);
            }
            for (i, &(p, q)) in negs.iter().enumerate() {
                let mut cand_from = -std::i64::MAX / 4;
                if i > 0 {
                    cand_from = from_left[i - 1];
                }
                if i < n - 1 {
                    cand_from = max(from_right[i + 1], cand_from);
                }
                if cand_from != -std::i64::MAX / 4 && cand_from - p >= mid {
                    dp[di][i] = cand_from + p - q;
                }
            }
        }
        *dp[d - 1].iter().max().unwrap() >= mid
    };

    let (ok, ng) = binary_search(-(d as i64) * 100000000 - 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