結果
問題 | No.90 品物の並び替え |
ユーザー |
|
提出日時 | 2022-01-30 18:28:46 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 436 ms / 5,000 ms |
コード長 | 1,860 bytes |
コンパイル時間 | 11,367 ms |
コンパイル使用メモリ | 378,728 KB |
実行使用メモリ | 38,784 KB |
最終ジャッジ日時 | 2024-06-11 08:23:12 |
合計ジャッジ時間 | 12,650 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 9 |
ソースコード
#[macro_export]macro_rules! setup {{ mut $input:ident: SplitWhitespace $(,)? } => {use std::io::Read;let mut buf = String::new();std::io::stdin().read_to_string(&mut buf).ok();let mut $input = buf.split_whitespace();};}#[macro_export]macro_rules! parse_next {($str_iter:expr) => {$str_iter.next().unwrap().parse().ok().unwrap()};}use std::collections::HashMap;fn main() {setup! { mut input: SplitWhitespace };let n: usize = parse_next!(input);let m: usize = parse_next!(input);let mut score = HashMap::new();(0..m).for_each(|_| {let i: usize = parse_next!(input);let j: usize = parse_next!(input);let s: usize = parse_next!(input);score.insert((i, j), s);});let src: Vec<usize> = (0..n).collect();let ans = (|| {let mut max = 0;for v in permutations(&src).into_iter() {let mut s = 0;for i in 0..n {for j in i..n {if let Some(value) = score.get(&(v[i], v[j])) {s += value;}}}if s >= max {max = s;}}max})();println!("{}", ans);}fn permutations<T>(v: &Vec<T>) -> Vec<Vec<T>>whereT: Clone,{let mut v = (*v).clone();let n = v.len();let mut ret = vec![];ret.push(v.clone());let mut c = vec![0; n];let mut i = 0;while i < n {if c[i] < i {if i % 2 == 0 {v.swap(0, i);} else {v.swap(c[i], i);}ret.push(v.clone());c[i] += 1;i = 0;} else {c[i] = 0;i += 1;}}ret}