結果

問題 No.1345 Beautiful BINGO
ユーザー BrightLightBrightLight
提出日時 2021-01-16 19:17:48
言語 Rust
(1.77.0)
結果
AC  
実行時間 536 ms / 2,000 ms
コード長 2,199 bytes
コンパイル時間 15,311 ms
コンパイル使用メモリ 378,404 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-06 07:19:09
合計ジャッジ時間 23,456 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 0 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 1 ms
5,376 KB
testcase_20 AC 0 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 20 ms
5,376 KB
testcase_23 AC 532 ms
5,376 KB
testcase_24 AC 183 ms
5,376 KB
testcase_25 AC 9 ms
5,376 KB
testcase_26 AC 92 ms
5,376 KB
testcase_27 AC 520 ms
5,376 KB
testcase_28 AC 10 ms
5,376 KB
testcase_29 AC 92 ms
5,376 KB
testcase_30 AC 43 ms
5,376 KB
testcase_31 AC 21 ms
5,376 KB
testcase_32 AC 3 ms
5,376 KB
testcase_33 AC 522 ms
5,376 KB
testcase_34 AC 5 ms
5,376 KB
testcase_35 AC 4 ms
5,376 KB
testcase_36 AC 398 ms
5,376 KB
testcase_37 AC 3 ms
5,376 KB
testcase_38 AC 90 ms
5,376 KB
testcase_39 AC 530 ms
5,376 KB
testcase_40 AC 181 ms
5,376 KB
testcase_41 AC 158 ms
5,376 KB
testcase_42 AC 0 ms
5,376 KB
testcase_43 AC 1 ms
5,376 KB
testcase_44 AC 1 ms
5,376 KB
testcase_45 AC 0 ms
5,376 KB
testcase_46 AC 1 ms
5,376 KB
testcase_47 AC 1 ms
5,376 KB
testcase_48 AC 1 ms
5,376 KB
testcase_49 AC 1 ms
5,376 KB
testcase_50 AC 1 ms
5,376 KB
testcase_51 AC 1 ms
5,376 KB
testcase_52 AC 349 ms
5,376 KB
testcase_53 AC 530 ms
5,376 KB
testcase_54 AC 533 ms
5,376 KB
testcase_55 AC 526 ms
5,376 KB
testcase_56 AC 339 ms
5,376 KB
testcase_57 AC 513 ms
5,376 KB
testcase_58 AC 521 ms
5,376 KB
testcase_59 AC 522 ms
5,376 KB
testcase_60 AC 416 ms
5,376 KB
testcase_61 AC 536 ms
5,376 KB
testcase_62 AC 345 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp;

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>().split_whitespace()
        .map(|e| e.parse().ok().unwrap()).collect()
}

fn read_vec2<T: std::str::FromStr>(n: usize) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}


fn main() {
    let nm: Vec<usize> = read_vec();
    let n = nm[0];
    let m = nm[1];
    let a: Vec<Vec<usize>> = read_vec2(n);

    let mut ans: usize = 9999999;

    let mut total: usize = 0;
    for i in 0..n {
        for j in 0..n {
            total += a[i][j];
        }
    }

    for i in 0..1 << (n + 2) {
        let mut a0 = a.clone();
        let cost;
        let mut count = 0;
        for j in 0..(n + 2) {
            if (1 << j) & i == 0 && j < n {
                a0[j] = vec![0; n];
                count += 1;
            } else 
            if (1 << j) & i == 0 && j == n {
                for k in 0..n {
                    a0[k][k] = 0;
                }
                count += 1;
            } else 
            if (1 << j) & i == 0 && j > n {
                for k in 0..n {
                    a0[k][n - k - 1] = 0;
                }
                count += 1;
            }
        }
        //println!("count={}, a = {:?}", count, a0);

        if count > m {
            //println!("count > m");
            continue;
        } else {
            let mut line_sum: Vec<usize> = Vec::new();
            for j in 0..n {
                let mut sum = 0;
                for k in 0..n {
                    sum += a0[k][j];
                }
                line_sum.push(sum);
            }
            line_sum.sort_unstable();

            for j in 0..cmp::min(m - count, n) {
                line_sum[j] = 0
            }

            let mut remain = 0;
            for j in 0..n {
                remain += line_sum[j];
            }

            cost = total - remain;
            if ans > cost {
                ans = cost;
            }
            //println!("cost = {}, ans = {}", cost, ans);
        }
    }

    println!("{}", ans);

}
0