結果
| 問題 |
No.1345 Beautiful BINGO
|
| コンテスト | |
| ユーザー |
BrightLight
|
| 提出日時 | 2021-01-16 19:17:48 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
AC
|
| 実行時間 | 553 ms / 2,000 ms |
| コード長 | 2,199 bytes |
| コンパイル時間 | 15,144 ms |
| コンパイル使用メモリ | 389,744 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-11-28 12:26:25 |
| 合計ジャッジ時間 | 24,186 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 61 |
ソースコード
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);
}
BrightLight