結果

問題 No.133 カードゲーム
コンテスト
ユーザー kinodjnz
提出日時 2024-05-10 01:27:50
言語 Rust
(1.94.0 + proconio + num + itertools)
コンパイル:
/usr/bin/rustc_custom
実行:
./target/release/main
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,258 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,372 ms
コンパイル使用メモリ 192,100 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-05-26 23:47:06
合計ジャッジ時間 2,787 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

use itertools::Itertools;
use std::io::Read;
use std::cmp::Ordering;

fn factorial(x: usize) -> usize {
    if x == 0 {
        1
    } else {
        x * factorial(x - 1)
    }
}

fn solve(n: usize, a: &[i64], b: &[i64]) -> f64 {
    let mut a_wins = 0;
    for pa in a.iter().permutations(n) {
        for pb in b.iter().permutations(n) {
            let m = pa
                .iter()
                .zip(pb.iter())
                .map(|(x, y)| match x.cmp(y) {
                    Ordering::Greater => 1,
                    Ordering::Less => -1,
                    Ordering::Equal => 0,
                })
                .sum::<i64>();
            if m > 0 {
                a_wins += 1;
            }
        }
    }
    (a_wins as f64) / (factorial(n).pow(2) as f64)
}

fn main() {
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut iter = buf.split_whitespace();
    let n: usize = iter.next().unwrap().parse().unwrap();
    let a: Vec<i64> = (0..n)
        .map(|_| iter.next().unwrap().parse().unwrap())
        .collect();
    let b: Vec<i64> = (0..n)
        .map(|_| iter.next().unwrap().parse().unwrap())
        .collect();
    let result = solve(n, &a, &b);
    println!("{}", result);
}
0