結果

問題 No.43 野球の試合
ユーザー te-shte-sh
提出日時 2017-04-14 13:47:23
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 20 ms / 5,000 ms
コード長 1,152 bytes
コンパイル時間 1,682 ms
コンパイル使用メモリ 103,620 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 12:50:26
合計ジャッジ時間 1,738 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 20 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void main()
{
  auto n = readln.chomp.to!size_t;
  auto gi = n.iota.map!(_ => readln.chomp.until("#")).join;

  auto ai = gi.enumerate.map!(g => g[1] == '-' ? g[0] : size_t.max).filter!(i => i < size_t.max).array;
  auto r = n;
  foreach (i; 0..(1 << ai.length)) {
    auto bi = gi.dup;
    foreach (j, a; ai)
      bi[a] = bitTest(i, j) ? 'o' : 'x';
    r = min(r, calc(n, bi));
  }

  writeln(r);
}

auto calc(size_t n, dchar[] bi)
{
  auto wi = new int[](n);
  foreach (i; 0..n) {
    auto w = 0;
    foreach (j; 0..n)
      if (result(bi, i, j)) w = w.bitSet(j);
    wi[i] = w.popcnt;
  }
  auto w0 = wi[0];
  auto ri = wi.sort!"a > b".uniq;
  return ri.countUntil(w0) + 1;
}

bool result(dchar[] bi, size_t p1, size_t p2)
{
  if (p1 == p2)
    return false;
  else if (p1 > p2)
    return !result(bi, p2, p1);
  else
    return bi[(p2 - 1) * p2 / 2 + p1] == 'x';
}

pure bool bitTest(T)(T n, size_t i) { return (n & (T(1) << i)) != 0; }
pure T bitSet(T)(T n, size_t i) { return n | (T(1) << i); }
pure int popcnt(T)(T n) { return core.bitop.popcnt(ulong(n)); }
0