結果

問題 No.2542 Yokan for Two
ユーザー nautnaut
提出日時 2023-11-24 22:03:00
言語 Rust
(1.77.0)
結果
AC  
実行時間 1,248 ms / 2,000 ms
コード長 2,233 bytes
コンパイル時間 898 ms
コンパイル使用メモリ 188,416 KB
実行使用メモリ 23,716 KB
最終ジャッジ日時 2023-11-24 22:03:37
合計ジャッジ時間 36,192 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 24 ms
12,820 KB
testcase_01 AC 30 ms
12,820 KB
testcase_02 AC 16 ms
12,800 KB
testcase_03 AC 386 ms
23,716 KB
testcase_04 AC 577 ms
15,408 KB
testcase_05 AC 667 ms
22,092 KB
testcase_06 AC 36 ms
18,580 KB
testcase_07 AC 1,071 ms
22,920 KB
testcase_08 AC 321 ms
21,640 KB
testcase_09 AC 1,087 ms
22,844 KB
testcase_10 AC 324 ms
15,416 KB
testcase_11 AC 423 ms
15,248 KB
testcase_12 AC 503 ms
21,872 KB
testcase_13 AC 316 ms
14,892 KB
testcase_14 AC 1,171 ms
22,984 KB
testcase_15 AC 1,195 ms
23,496 KB
testcase_16 AC 1,182 ms
23,156 KB
testcase_17 AC 1,121 ms
22,840 KB
testcase_18 AC 1,239 ms
23,664 KB
testcase_19 AC 906 ms
18,700 KB
testcase_20 AC 855 ms
18,204 KB
testcase_21 AC 854 ms
18,232 KB
testcase_22 AC 877 ms
18,360 KB
testcase_23 AC 919 ms
18,416 KB
testcase_24 AC 98 ms
23,712 KB
testcase_25 AC 144 ms
23,712 KB
testcase_26 AC 145 ms
23,712 KB
testcase_27 AC 156 ms
23,712 KB
testcase_28 AC 171 ms
23,716 KB
testcase_29 AC 326 ms
23,716 KB
testcase_30 AC 1,248 ms
23,716 KB
testcase_31 AC 1,205 ms
23,716 KB
testcase_32 AC 1,189 ms
23,716 KB
testcase_33 AC 1,214 ms
23,716 KB
testcase_34 AC 1,200 ms
23,716 KB
testcase_35 AC 1,197 ms
23,716 KB
testcase_36 AC 1,202 ms
23,716 KB
testcase_37 AC 1,203 ms
23,716 KB
testcase_38 AC 1,196 ms
23,716 KB
testcase_39 AC 1,206 ms
23,716 KB
testcase_40 AC 1,206 ms
23,716 KB
testcase_41 AC 1,205 ms
23,716 KB
testcase_42 AC 1,208 ms
23,716 KB
testcase_43 AC 1,227 ms
23,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::io::{self, prelude::*};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let L = input!(usize);
    let N = input!(usize);
    let mut X = input!(usize, N);
    X.insert(0, 0);
    X.push(L);

    const MARGIN: usize = 100_010;

    let mut dp = vec![vec![false; 2]; MARGIN + L + 1];
    dp[MARGIN + X[1] - X[0]][0] = true;

    for i in 1..=N {
        let mut nxt_dp = vec![vec![false; 2]; MARGIN + L + 1];

        for j in 0..MARGIN + L + 1 {
            let d = X[i + 1] - X[i];

            if dp[j][0] {
                nxt_dp[j + d][0] = true;
                nxt_dp[j - d][1] = true;
            }

            if dp[j][1] {
                nxt_dp[j + d][0] = true;
                nxt_dp[j - d][1] = true;
            }
        }

        dp = nxt_dp;
    }

    let mut ans = 1 << 30;

    for j in 0..MARGIN + L + 1 {
        if dp[j][1] {
            ans = std::cmp::min(ans, (j as isize - MARGIN as isize).abs());
        }
    }

    writeln!(out, "{}", ans);
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}
0