結果

問題 No.910 素数部分列
ユーザー akakimidori
提出日時 2019-10-18 23:58:34
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 4 ms / 1,000 ms
コード長 980 bytes
コンパイル時間 16,888 ms
コンパイル使用メモリ 390,556 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-25 22:03:45
合計ジャッジ時間 18,649 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

fn run() {
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let _n: usize = it.next().unwrap().parse().unwrap();
    let s = it.next().unwrap();
    let mut a = vec![];
    let mut ans = 0;
    for c in s.chars() {
        match c {
            '1' => {
                a.push(c);
            },
            '9' => {
                if let Some(&'1') = a.last() {
                    a.pop();
                    ans += 1;
                } else {
                    a.push(c);
                }
            },
            _ => ans += 1,
        }
    }
    let mut c9 = a.iter().filter(|c| **c == '9').count();
    let mut c1 = a.iter().filter(|c| **c == '1').count();
    while c9 >= 2 && c1 >= 1 {
        ans += 1;
        c9 -= 2;
        c1 -= 1;
    }
    while c1 >= 2 {
        ans += 1;
        c1 -= 2;
    }
    println!("{}", ans);
}

fn main() {
    run();
}
0