結果
| 問題 |
No.133 カードゲーム
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-03-22 17:57:35 |
| 言語 | Java (openjdk 23) |
| 結果 |
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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 19 |
ソースコード
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;
}
}
}
}