結果

問題 No.1515 Making Many Multiples
ユーザー koba-e964koba-e964
提出日時 2021-11-19 00:26:43
言語 Rust
(1.77.0)
結果
AC  
実行時間 190 ms / 2,000 ms
コード長 2,417 bytes
コンパイル時間 1,044 ms
コンパイル使用メモリ 155,036 KB
実行使用メモリ 17,876 KB
最終ジャッジ日時 2023-08-27 19:23:54
合計ジャッジ時間 5,838 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 189 ms
17,788 KB
testcase_03 AC 183 ms
17,872 KB
testcase_04 AC 188 ms
17,628 KB
testcase_05 AC 190 ms
17,812 KB
testcase_06 AC 159 ms
17,876 KB
testcase_07 AC 165 ms
17,792 KB
testcase_08 AC 54 ms
5,296 KB
testcase_09 AC 94 ms
9,544 KB
testcase_10 AC 100 ms
10,012 KB
testcase_11 AC 135 ms
12,816 KB
testcase_12 AC 76 ms
8,448 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 142 ms
14,112 KB
testcase_15 AC 6 ms
4,376 KB
testcase_16 AC 169 ms
16,456 KB
testcase_17 AC 30 ms
9,892 KB
testcase_18 AC 21 ms
12,428 KB
testcase_19 AC 5 ms
6,656 KB
testcase_20 AC 31 ms
6,436 KB
testcase_21 AC 111 ms
16,948 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 125 ms
17,220 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 165 ms
17,376 KB
testcase_28 AC 91 ms
17,444 KB
testcase_29 AC 6 ms
4,376 KB
testcase_30 AC 16 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::*;
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

// https://yukicoder.me/problems/no/1515 (3.5)
// カード c をめくったときの遷移は dp[a][b] -max-> dp[c][a], dp[a][b] -max-> dp[c][b] という形に限られる。ほとんどの遷移は +0 なので、各行各列の max を記憶しておけば良い。 -> dp[a][b] -> dp[a][b] (めくったカードを捨てる) という遷移もあった。ほとんど無視できるが k | a + b + c の場合には += 1 が必要。
// Tags: dp, inline-dp, overwriting-dp
fn main() {
    input! {
        n: usize, k: usize, x: usize, y: usize,
        a: [usize; n],
    }
    const INF: i32 = 1 << 28;
    let mut dp = vec![vec![-INF; k]; k];
    dp[x % k][y % k] = 0;
    let mut row = vec![-INF; k];
    let mut col = vec![-INF; k];
    row[x % k] = 0;
    col[y % k] = 0;
    for &a in &a {
        let a = a % k;
        let mut delay = vec![];
        for b in 0..k {
            let rest = (2 * k - a - b) % k;
            delay.push((b, rest, dp[b][rest] + 1));
        }
        for b in 0..k {
            let mut ma = max(row[b], col[b]);
            let rest = (2 * k - a - b) % k;
            ma = max(ma, max(dp[rest][b], dp[b][rest]) + 1);
            delay.push((a, b, ma));
        }
        for (i, j, val) in delay {
            dp[i][j] = max(dp[i][j], val);
            row[i] = max(row[i], dp[i][j]);
            col[j] = max(col[j], dp[i][j]);
        }
    }
    let ans = row.iter().max().unwrap();
    println!("{}", ans);
}
0