結果

問題 No.1818 6 Operations
ユーザー koba-e964koba-e964
提出日時 2023-06-16 09:51:32
言語 Rust
(1.77.0)
結果
AC  
実行時間 141 ms / 2,500 ms
コード長 2,578 bytes
コンパイル時間 1,639 ms
コンパイル使用メモリ 152,012 KB
実行使用メモリ 58,716 KB
最終ジャッジ日時 2023-09-06 09:39:07
合計ジャッジ時間 6,079 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 43 ms
18,840 KB
testcase_09 AC 81 ms
34,420 KB
testcase_10 AC 44 ms
19,108 KB
testcase_11 AC 39 ms
17,772 KB
testcase_12 AC 67 ms
27,476 KB
testcase_13 AC 54 ms
23,012 KB
testcase_14 AC 61 ms
26,160 KB
testcase_15 AC 71 ms
30,192 KB
testcase_16 AC 59 ms
25,096 KB
testcase_17 AC 87 ms
36,796 KB
testcase_18 AC 122 ms
50,728 KB
testcase_19 AC 115 ms
48,936 KB
testcase_20 AC 124 ms
51,544 KB
testcase_21 AC 123 ms
52,360 KB
testcase_22 AC 107 ms
45,244 KB
testcase_23 AC 133 ms
55,540 KB
testcase_24 AC 111 ms
46,560 KB
testcase_25 AC 141 ms
58,716 KB
testcase_26 AC 113 ms
47,572 KB
testcase_27 AC 117 ms
49,724 KB
testcase_28 AC 42 ms
18,836 KB
testcase_29 AC 41 ms
17,752 KB
testcase_30 AC 121 ms
50,484 KB
testcase_31 AC 130 ms
53,956 KB
testcase_32 AC 136 ms
56,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// 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"));
}

trait Change { fn chmax(&mut self, x: Self); fn chmin(&mut self, x: Self); }
impl<T: PartialOrd> Change for T {
    fn chmax(&mut self, x: T) { if *self < x { *self = x; } }
    fn chmin(&mut self, x: T) { if *self > x { *self = x; } }
}

fn encode(a: Vec<usize>) -> Vec<bool> {
    let mut res = vec![];
    for i in 0..a.len() {
        if i > 0 {
            res.push(true);
        }
        res.extend_from_slice(&vec![false; a[i]]);
    }
    res
}

// https://yukicoder.me/problems/no/1818 (3.5)
// 整数列 A を、0 を A[1] 個、 1 を 1 個、0 を A[2] 個、...、0 を A[N] 個ならべたものとして 0 と 1 の列にエンコードする。
// この時、操作は 0 や 1 を挿入・削除、 0 と 1 の書き換えに対応する。
// A も B も長さが 6 * 10^3 程度であるため、編集距離を求めれば良い。
fn main() {
    input! {
        n: usize, m: usize,
        a: [usize; n],
        b: [usize; m],
    }
    let a = encode(a);
    let b = encode(b);
    let n = a.len();
    let m = b.len();
    const INF: i32 = 1 << 28;
    let mut dp = vec![vec![INF; m + 1]; n + 1];
    dp[0][0] = 0;
    for i in 0..n + 1 {
        for j in 0..m + 1 {
            if i + j == 0 { continue; }
            let mut me = INF;
            if i > 0 {
                me.chmin(dp[i - 1][j] + 1);
            }
            if j > 0 {
                me.chmin(dp[i][j - 1] + 1);
            }
            if i > 0 && j > 0 {
                me.chmin(dp[i - 1][j - 1] + if a[i - 1] == b[j - 1] { 0 } else { 1 });
            }
            dp[i][j] = me;
        }
    }
    println!("{}", dp[n][m]);
}
0