結果
問題 | No.133 カードゲーム |
ユーザー | takumikid |
提出日時 | 2019-03-22 17:57:35 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 145 ms / 5,000 ms |
コード長 | 3,505 bytes |
コンパイル時間 | 2,618 ms |
コンパイル使用メモリ | 86,712 KB |
実行使用メモリ | 41,736 KB |
最終ジャッジ日時 | 2024-09-19 02:29:37 |
合計ジャッジ時間 | 5,856 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 109 ms
41,088 KB |
testcase_01 | AC | 117 ms
41,252 KB |
testcase_02 | AC | 113 ms
41,196 KB |
testcase_03 | AC | 109 ms
41,196 KB |
testcase_04 | AC | 118 ms
41,160 KB |
testcase_05 | AC | 129 ms
41,476 KB |
testcase_06 | AC | 125 ms
41,324 KB |
testcase_07 | AC | 116 ms
41,428 KB |
testcase_08 | AC | 125 ms
41,292 KB |
testcase_09 | AC | 133 ms
41,236 KB |
testcase_10 | AC | 145 ms
41,496 KB |
testcase_11 | AC | 131 ms
41,736 KB |
testcase_12 | AC | 131 ms
40,476 KB |
testcase_13 | AC | 127 ms
41,184 KB |
testcase_14 | AC | 131 ms
41,460 KB |
testcase_15 | AC | 127 ms
41,416 KB |
testcase_16 | AC | 120 ms
41,476 KB |
testcase_17 | AC | 143 ms
41,376 KB |
testcase_18 | AC | 138 ms
41,308 KB |
testcase_19 | AC | 132 ms
41,316 KB |
testcase_20 | AC | 130 ms
41,392 KB |
testcase_21 | AC | 136 ms
41,336 KB |
testcase_22 | AC | 133 ms
41,576 KB |
ソースコード
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; } } } }