結果

問題 No.1606 Stuffed Animals Keeper
ユーザー phsplsphspls
提出日時 2022-10-26 01:13:48
言語 Rust
(1.77.0)
結果
AC  
実行時間 64 ms / 3,000 ms
コード長 1,235 bytes
コンパイル時間 1,291 ms
コンパイル使用メモリ 153,552 KB
実行使用メモリ 67,360 KB
最終ジャッジ日時 2023-09-16 21:30:01
合計ジャッジ時間 4,517 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 28 ms
28,548 KB
testcase_04 AC 36 ms
35,636 KB
testcase_05 AC 7 ms
7,484 KB
testcase_06 AC 12 ms
13,020 KB
testcase_07 AC 24 ms
24,640 KB
testcase_08 AC 22 ms
22,472 KB
testcase_09 AC 9 ms
10,648 KB
testcase_10 AC 5 ms
6,400 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 12 ms
13,012 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 43 ms
44,848 KB
testcase_17 AC 45 ms
45,644 KB
testcase_18 AC 45 ms
45,960 KB
testcase_19 AC 44 ms
45,384 KB
testcase_20 AC 43 ms
45,344 KB
testcase_21 AC 8 ms
8,716 KB
testcase_22 AC 7 ms
7,916 KB
testcase_23 AC 7 ms
7,892 KB
testcase_24 AC 7 ms
8,632 KB
testcase_25 AC 7 ms
8,152 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 3 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 1 ms
4,380 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 64 ms
67,336 KB
testcase_44 AC 62 ms
67,348 KB
testcase_45 AC 63 ms
67,360 KB
testcase_46 AC 63 ms
67,344 KB
testcase_47 AC 62 ms
67,264 KB
testcase_48 AC 0 ms
4,376 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const INF: usize = 1usize << 60;

fn main() {
    let mut n = String::new();
    std::io::stdin().read_line(&mut n).ok();
    let n: usize = n.trim().parse().unwrap();
    let mut a = String::new();
    std::io::stdin().read_line(&mut a).ok();
    let a: Vec<_> = a.trim().split_whitespace().collect::<Vec<_>>().join("").split('2').map(|s| s.chars().collect::<Vec<_>>()).collect();

    let zeros = a.iter().filter(|&v| v.len() > 0).map(|v| v.iter().filter(|&&c| c == '1').count()).sum::<usize>();
    let one_cnt = a.iter().filter(|&v| v.len() > 0).map(|v| v.iter().filter(|&&c| c == '1').count()).collect::<Vec<_>>();
    let size_cnt = a.iter().filter(|&v| v.len() > 0).map(|v| v.len()).collect::<Vec<_>>();
    let mut dp = vec![vec![INF; n+1]; size_cnt.len()+1];
    dp[0][0] = 0;
    for i in 0..size_cnt.len() {
        let to_zero_cnt = size_cnt[i] - one_cnt[i];
        for j in 0..=n {
            if dp[i][j] == INF { continue; }
            dp[i+1][j+size_cnt[i]] = dp[i+1][j+size_cnt[i]].min(dp[i][j] + to_zero_cnt);
            dp[i+1][j] = dp[i+1][j].min(dp[i][j]);
        }
    }
    if dp[size_cnt.len()][zeros] == INF {
        println!("-1");
    } else {
        println!("{}", dp[size_cnt.len()][zeros]);
    }
}
0