結果

問題 No.636 硬貨の枚数2
ユーザー koba-e964
提出日時 2021-09-28 23:02:31
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,550 bytes
コンパイル時間 14,962 ms
コンパイル使用メモリ 378,448 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-08 08:33:15
合計ジャッジ時間 15,472 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 65
権限があれば一括ダウンロードができます

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::*;
#[allow(unused_imports)]
use std::collections::*;
use std::io::Read;

#[allow(dead_code)]
fn getline() -> String {
    let mut ret = String::new();
    std::io::stdin().read_line(&mut ret).ok().unwrap();
    ret
}

fn get_word() -> String {
    let stdin = std::io::stdin();
    let mut stdin=stdin.lock();
    let mut u8b: [u8; 1] = [0];
    loop {
        let mut buf: Vec<u8> = Vec::with_capacity(16);
        loop {
            let res = stdin.read(&mut u8b);
            if res.unwrap_or(0) == 0 || u8b[0] <= b' ' {
                break;
            } else {
                buf.push(u8b[0]);
            }
        }
        if buf.len() >= 1 {
            let ret = String::from_utf8(buf).unwrap();
            return ret;
        }
    }
}

#[allow(dead_code)]
fn get<T: std::str::FromStr>() -> T { get_word().parse().ok().unwrap() }

const INF: i32 = 1 << 28;

fn f(c: i32) -> i32 {
    if c < 0 {
        INF
    } else {
        c / 10 + (c % 10) / 5 + c % 5
    }
}

// Tags: digital-dp
fn main() {
    let s: Vec<_> = get_word().bytes().collect();
    let n = s.len();
    let mut dp = vec![[INF; 3]; n + 1];
    dp[n][0] = 0;
    for i in (0..n).rev() {
        let c = (s[i] - b'0') as i32;
        for b in 0..2 {
            let c = c + b as i32;
            for j in 0..11 {
                dp[i][0] = min(dp[i][0], dp[i + 1][b] + f(j - c) + f(j));
                dp[i][1] = min(dp[i][1], dp[i + 1][b] + f(j) + f(j - c + 10));
            }
        }
    }
    println!("{}", dp[0][0]);
}
0