結果

問題 No.3266 岩井星人は見ずにはいられない
コンテスト
ユーザー elphe
提出日時 2025-11-23 12:29:51
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,122 bytes
コンパイル時間 13,031 ms
コンパイル使用メモリ 401,408 KB
実行使用メモリ 7,852 KB
最終ジャッジ日時 2025-11-23 12:30:07
合計ジャッジ時間 14,982 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

use proconio::{fastout, input, marker::Bytes};

#[fastout]
fn main() {
    input! {
        n: usize,
        a: u64,
        s: Bytes,
    }

    println!("{}", solve(n, a, s));
}

fn solve(_n: usize, mut a: u64, s: Vec<u8>) -> u64 {
    let mut margin: u64 = 0;
    let mut do_cycle = |a: &mut _| -> Option<u64> {
        for (i, s) in s.iter().enumerate() {
            match *s {
                b'0' => margin += 1,
                b'1' => {
                    if margin > 0 {
                        margin -= 1;
                        *a -= 1;
                        if *a == 0 {
                            return Some((i + 1) as u64);
                        }
                    }
                }
                _ => (),
            }
        }
        None
    };

    if let Some(result) = do_cycle(&mut a) {
        return result;
    }
    let a_origin = a;
    if let Some(result) = do_cycle(&mut a) {
        return s.len() as u64 + result;
    }

    let ans_about = s.len() as u64 * (2 + (a - 1) / (a_origin - a));
    a = (a - 1) % (a_origin - a) + 1;
    ans_about + do_cycle(&mut a).unwrap()
}
0