結果

問題 No.174 カードゲーム(Hard)
ユーザー te-shte-sh
提出日時 2017-05-01 15:40:46
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 428 ms / 2,000 ms
コード長 1,432 bytes
コンパイル時間 2,126 ms
コンパイル使用メモリ 158,428 KB
実行使用メモリ 38,948 KB
最終ジャッジ日時 2023-09-03 13:07:35
合計ジャッジ時間 6,127 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 427 ms
36,104 KB
testcase_03 AC 424 ms
38,452 KB
testcase_04 AC 424 ms
36,568 KB
testcase_05 AC 428 ms
38,948 KB
testcase_06 AC 421 ms
36,076 KB
testcase_07 AC 422 ms
36,036 KB
testcase_08 AC 421 ms
38,732 KB
testcase_09 AC 420 ms
36,040 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto rd = readln.split, n = rd[0].to!size_t, pa = rd[1].to!real, pb = rd[2].to!real;
  auto ai = readln.split.to!(int[]); ai.sort();
  auto bi = readln.split.to!(int[]); bi.sort();

  auto paij = calc(n, ai, pa);
  auto pbij = calc(n, bi, pb);

  auto r = real(0);
  foreach (i; 0..n)
    foreach (ja; 0..n)
      foreach (jb; 0..n)
        if (ai[ja] > bi[jb])
          r += paij[ja][i] * pbij[jb][i] * (ai[ja] + bi[jb]);

  writefln("%.10f", r);
}

auto calc(size_t n, int[] ai, real p)
{
  auto dp = new real[](1 << n), pij = new real[][](n, n);
  dp[$-1] = 1; dp[0..$-1] = 0;
  foreach (ref pi; pij) pi[] = 0;

  foreach_reverse (uint i; 1..(1 << n)) {
    auto f = i.bsf, r = i.bsr, c = i.popcnt;

    auto pf = dp[i] * (c == 1 ? 1 : p);
    dp[i.bitComp(f)] += pf;
    pij[f][n - c] += pf;

    foreach (j; f+1..r+1)
      if (i.bitTest(j)) {
        auto pj = dp[i] * (1 - p) / (c - 1);
        dp[i.bitComp(j)] += pj;
        pij[j][n - c] += pj;
      }
  }

  return pij;
}

pragma(inline) {
  pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
  pure T bitComp(T)(T n, size_t i) { return n ^ (T(1) << i); }

  import core.bitop;
  pure int bsf(T)(T n) { return core.bitop.bsf(ulong(n)); }
  pure int bsr(T)(T n) { return core.bitop.bsr(ulong(n)); }
  pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
}
0