結果

問題 No.1715 Dinner 2
ユーザー fukafukatanifukafukatani
提出日時 2021-10-23 11:04:29
言語 Rust
(1.77.0 + proconio)
結果
RE  
実行時間 -
コード長 2,671 bytes
コンパイル時間 12,738 ms
コンパイル使用メモリ 396,340 KB
実行使用メモリ 9,780 KB
最終ジャッジ日時 2024-09-25 02:11:46
合計ジャッジ時間 16,262 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 RE -
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 1 ms
6,816 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 WA -
testcase_06 AC 1 ms
6,944 KB
testcase_07 RE -
testcase_08 WA -
testcase_09 AC 1 ms
6,944 KB
testcase_10 WA -
testcase_11 AC 271 ms
7,936 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 AC 204 ms
6,944 KB
testcase_17 AC 125 ms
6,940 KB
testcase_18 AC 5 ms
6,944 KB
testcase_19 AC 7 ms
6,940 KB
testcase_20 AC 5 ms
6,940 KB
testcase_21 AC 4 ms
6,940 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 19 ms
6,940 KB
testcase_26 AC 14 ms
6,944 KB
testcase_27 AC 13 ms
6,940 KB
testcase_28 WA -
testcase_29 AC 18 ms
6,940 KB
testcase_30 AC 18 ms
6,940 KB
testcase_31 AC 18 ms
6,944 KB
testcase_32 AC 352 ms
9,652 KB
testcase_33 AC 349 ms
9,776 KB
testcase_34 AC 356 ms
9,780 KB
testcase_35 AC 361 ms
9,776 KB
testcase_36 AC 1 ms
6,944 KB
testcase_37 WA -
testcase_38 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused variable: `i`
  --> src/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`
  --> src/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`

ソースコード

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[n - 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