結果

問題 No.90 品物の並び替え
ユーザー cra77756176cra77756176
提出日時 2022-12-21 19:27:39
言語 Rust
(1.77.0)
結果
AC  
実行時間 44 ms / 5,000 ms
コード長 1,269 bytes
コンパイル時間 13,858 ms
コンパイル使用メモリ 379,688 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 03:24:12
合計ジャッジ時間 11,648 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 0 ms
5,248 KB
testcase_01 AC 5 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 5 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 44 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

trait NextPermutation {
    fn next_permutation(&mut self) -> bool;
}

impl<T: PartialOrd> NextPermutation for Vec<T> {
    fn next_permutation(&mut self) -> bool {
        self.windows(2)
            .rposition(|x| x[0] < x[1])
            .map_or(false, |i| {
                let j = self.iter().rposition(|x| x > &self[i]).unwrap();
                self.swap(i, j);
                self[i + 1..].reverse();
                true
            })
    }
}

fn main() {
    let mut xx = String::new();
    std::io::Read::read_to_string(&mut std::io::stdin(), &mut xx).ok();
    let xx: Vec<usize> = xx.split_whitespace().flat_map(str::parse).collect();

    let n = xx[0];

    let mut scores = vec![];
    for x in xx[2..].chunks(3) {
        scores.push((x[0], x[1], x[2]));
    }

    let mut items: Vec<usize> = (0..n).collect();

    let mut max = 0;
    loop {
        let mut idx = vec![0; n];
        for (i, &item) in items.iter().enumerate() {
            idx[item] = i;
        }

        let mut score = 0;
        for &(i, j, s) in &scores {
            if idx[i] < idx[j] {
                score += s;
            }
        }

        max = max.max(score);

        if !items.next_permutation() {
            break;
        }
    }

    println!("{max}");
}
0