結果

問題 No.1715 Dinner 2
ユーザー fukafukatanifukafukatani
提出日時 2021-10-23 11:16:49
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 2,671 bytes
コンパイル時間 13,178 ms
コンパイル使用メモリ 388,108 KB
実行使用メモリ 9,904 KB
最終ジャッジ日時 2024-09-25 02:26:33
合計ジャッジ時間 17,688 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 WA -
testcase_04 AC 1 ms
6,944 KB
testcase_05 WA -
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 WA -
testcase_09 AC 1 ms
6,940 KB
testcase_10 WA -
testcase_11 AC 308 ms
8,064 KB
testcase_12 AC 241 ms
6,944 KB
testcase_13 AC 206 ms
6,944 KB
testcase_14 AC 249 ms
6,940 KB
testcase_15 AC 256 ms
7,040 KB
testcase_16 AC 236 ms
6,944 KB
testcase_17 AC 144 ms
6,944 KB
testcase_18 AC 4 ms
6,940 KB
testcase_19 AC 6 ms
6,940 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 23 ms
6,940 KB
testcase_26 AC 15 ms
6,940 KB
testcase_27 AC 15 ms
6,940 KB
testcase_28 WA -
testcase_29 AC 23 ms
6,944 KB
testcase_30 AC 21 ms
6,944 KB
testcase_31 AC 22 ms
6,944 KB
testcase_32 AC 397 ms
9,772 KB
testcase_33 AC 401 ms
9,904 KB
testcase_34 AC 421 ms
9,772 KB
testcase_35 AC 415 ms
9,904 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[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