結果

問題 No.174 カードゲーム(Hard)
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-10 03:41:04
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 382 ms / 2,000 ms
コード長 2,810 bytes
コンパイル時間 3,354 ms
コンパイル使用メモリ 162,876 KB
実行使用メモリ 39,496 KB
最終ジャッジ日時 2023-09-03 21:50:19
合計ジャッジ時間 6,082 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 381 ms
37,428 KB
testcase_03 AC 378 ms
37,652 KB
testcase_04 AC 382 ms
39,468 KB
testcase_05 AC 379 ms
37,084 KB
testcase_06 AC 379 ms
38,896 KB
testcase_07 AC 380 ms
36,564 KB
testcase_08 AC 379 ms
36,064 KB
testcase_09 AC 382 ms
39,496 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) throw new EOFException; tokens = readln.split; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return readToken().to!int; }
long readLong() { return readToken().to!long; }
real readReal() { return readToken().to!real; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


int N;
real[] P;
int[][] A;

void main() {
  try {
    for (; ; ) {
      N = readInt();
      P = new real[2];
      foreach (h; 0 .. 2) {
        P[h] = readReal();
      }
      A = new int[][](2, N);
      foreach (h; 0 .. 2) {
        foreach (i; 0 .. N) {
          A[h][i] = readInt();
        }
        A[h].sort;
      }
      
      auto prob = new real[][][](2, N, N);
      auto dp = new real[][](2, 1 << N);
      foreach (h; 0 .. 2) {
        foreach (k; 0 .. N) {
          prob[h][k][] = 0.0;
        }
        dp[h][] = 0.0;
        dp[h][(1 << N) - 1] = 1.0;
        foreach_reverse (s; 1 .. 1 << N) {
          const pop = popcnt(s);
          const k = N - pop;
          const iMin = bsf(s);
          if (pop == 1) {
            prob[h][k][iMin] += dp[h][s] * 1.0;
            dp[h][s ^ 1 << iMin] += dp[h][s] * 1.0;
          } else {
            prob[h][k][iMin] += dp[h][s] * P[h];
            dp[h][s ^ 1 << iMin] += dp[h][s] * P[h];
            foreach (i; iMin + 1 .. N) {
              if ((s >> i) & 1) {
                prob[h][k][i] += dp[h][s] * ((1.0 - P[h]) / (pop - 1));
                dp[h][s ^ 1 << i] += dp[h][s] * ((1.0 - P[h]) / (pop - 1));
              }
            }
          }
        }
      }
      debug {
        foreach (h; 0 .. 2) {
          writefln("h = %s", h);
          foreach (k; 0 .. N) {
            writefln("  prob[%s][%s] = %s", k, h, prob[h][k]);
          }
        }
      }
      
      real ans = 0.0;
      foreach (k; 0 .. N) {
        foreach (i0; 0 .. N) foreach (i1; 0 .. N) {
          if (A[0][i0] > A[1][i1]) {
            ans += prob[0][k][i0] * prob[1][k][i1] * (A[0][i0] + A[1][i1]);
          }
        }
      }
      writefln("%.10f", ans);
    }
  } catch (EOFException e) {
  }
}
0