結果

問題 No.133 カードゲーム
ユーザー takumikidtakumikid
提出日時 2019-03-22 17:57:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 150 ms / 5,000 ms
コード長 3,505 bytes
コンパイル時間 3,664 ms
コンパイル使用メモリ 80,200 KB
実行使用メモリ 57,696 KB
最終ジャッジ日時 2023-10-19 06:11:08
合計ジャッジ時間 7,270 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
57,464 KB
testcase_01 AC 136 ms
57,516 KB
testcase_02 AC 137 ms
57,388 KB
testcase_03 AC 136 ms
57,420 KB
testcase_04 AC 136 ms
57,688 KB
testcase_05 AC 144 ms
57,696 KB
testcase_06 AC 146 ms
57,664 KB
testcase_07 AC 147 ms
57,660 KB
testcase_08 AC 137 ms
57,644 KB
testcase_09 AC 137 ms
57,492 KB
testcase_10 AC 148 ms
57,556 KB
testcase_11 AC 149 ms
57,564 KB
testcase_12 AC 148 ms
57,652 KB
testcase_13 AC 135 ms
57,452 KB
testcase_14 AC 137 ms
57,664 KB
testcase_15 AC 139 ms
57,392 KB
testcase_16 AC 150 ms
57,664 KB
testcase_17 AC 143 ms
57,480 KB
testcase_18 AC 145 ms
57,580 KB
testcase_19 AC 144 ms
57,636 KB
testcase_20 AC 146 ms
57,624 KB
testcase_21 AC 146 ms
57,648 KB
testcase_22 AC 146 ms
57,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {

    public static void main(String[] args) {
        try {
            new Main().execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void execute() throws IOException {
        Scanner sc = new Scanner(System.in);
        final int N = sc.nextInt();
        Integer[] handA = new Integer[N];
        Integer[] handB = new Integer[N];

        for (int i = 0; i < N; i++) {
            handA[i] = sc.nextInt();
        }

        for (int i = 0; i < N; i++) {
            handB[i] = sc.nextInt();
        }

        Permutation<Integer> possibleHandsA = new Permutation<>(Arrays.asList(handA), true);
        int patterns = 0;
        int wins = 0;
        for (List<Integer> ptnA : possibleHandsA) {
            Permutation<Integer> possibleHandsB = new Permutation<>(Arrays.asList(handB), true);
            for (List<Integer> ptnB : possibleHandsB) {
                if (aWins(ptnA, ptnB)) {
                    wins++;
                }
                patterns++;
            }
        }
        System.out.println((double) wins / (double) patterns);
        sc.close();
    }

    private boolean aWins(List<Integer> a, List<Integer> b) {
        int n = a.size();
        int na = 0;
        for (int i = 0; i < n; i++) {
            if (a.get(i) > b.get(i)) {
                na++;
            }
        }
        return (na > (n - na));
    }

    class Permutation<T extends Comparable<? super T>> implements Iterable<List<T>>, Iterator<List<T>> {

        private final ArrayList<T> permutation;
        private final int N;
        private boolean hasNext = true;

        public Permutation(List<T> l) {
            this(l, false);
        }


        public Permutation(List<T> l, boolean sort) {
            permutation = new ArrayList<>(l);
            N = permutation.size();

            if (sort) {
                Collections.sort(permutation);
            }
        }

        private boolean _nextPermutation() {

            // Reference: https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order

            // Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation
            int k = N - 2;
            while (k >= 0) {
                if (permutation.get(k).compareTo(permutation.get(k + 1)) < 0) {
                    break;
                }
                k--;
            }
            if (k == -1) {
                return false;
            }

            // Find the largest index l greater than k such that a[k] < a[l].
            int l = N - 1;
            while (l > k) {
                if (permutation.get(k).compareTo(permutation.get(l)) < 0) {
                    break;
                }
                l--;
            }
            Collections.swap(permutation, k, l);
            Collections.reverse(permutation.subList(k + 1, N));
            return true;
        }

        @Override
        public Iterator<List<T>> iterator() {
            return this;
        }

        @Override
        public boolean hasNext() {
            return this.hasNext;
        }

        @Override
        public List<T> next() {
            if (this.hasNext) {
                List<T> next = new ArrayList<>(permutation);
                hasNext = _nextPermutation();
                return next;
            } else {
                return null;
            }
        }
    }
}
0