AtCoder Beginner Contest 025

Submission #1587505

Source codeソースコード

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

void main()
{
  auto b = new int[][](2);
  foreach (i; 0..2) b[i] = readln.split.to!(int[]);
  auto c = new int[][](3);
  foreach (i; 0..3) c[i] = readln.split.to!(int[]);

  struct P { int p1, p2; }

  P[int] memo;

  P calc(int x, int y)
  {
    auto mi = ((x << 9) | y);
    if (mi in memo) return memo[mi];

    if (y == (1 << 9) - 1) {
      auto p = P(0, 0);
      foreach (i; 0..2)
        foreach (j; 0..3) {
          if (x.bitTest(i*3+j) == x.bitTest((i+1)*3+j)) p.p1 += b[i][j];
          else                                          p.p2 += b[i][j];
        }
      foreach (i; 0..3)
        foreach (j; 0..2) {
          if (x.bitTest(i*3+j) == x.bitTest(i*3+(j+1))) p.p1 += c[i][j];
          else                                          p.p2 += c[i][j];
        }

      return memo[mi] = p;
    }

    auto turn = y.popcnt % 2 == 0;
    auto maxP = P(-1, -1);

    foreach (i; 0..9) {
      if (y.bitTest(i)) continue;
      auto p = calc(turn ? x.bitSet(i) : x, y.bitSet(i));
      if (maxP.p1 == -1 || turn && (p.p1 - p.p2) > (maxP.p1 - maxP.p2) || !turn && (p.p2 - p.p1) > (maxP.p2 - maxP.p1)) maxP = p;
    }

    return memo[mi] = maxP;
  }

  auto p = calc(0, 0);
  writeln(p.p1);
  writeln(p.p2);
}

pragma(inline) {
  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 T bitReset(T)(T n, size_t i) { return n & ~(T(1) << i); }
  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)); }
}

Submission

Task問題 C - 双子と○×ゲーム
User nameユーザ名 tesh
Created time投稿日時
Language言語 D (DMD64 v2.070.1)
Status状態 AC
Score得点 100
Source lengthソースコード長 1844 Byte
File nameファイル名
Exec time実行時間 3 ms
Memory usageメモリ使用量 2556 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - sample-01.txt,sample-02.txt
All 100 / 100 sample-01.txt,sample-02.txt,test-01.txt,test-02.txt,test-03.txt,test-04.txt,test-05.txt,test-06.txt,test-07.txt,test-08.txt,test-09.txt,test-10.txt,test-11.txt,test-12.txt,test-13.txt,test-14.txt,test-15.txt,test-16.txt,test-17.txt,test-18.txt,test-19.txt,test-20.txt,sample-01.txt,sample-02.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
sample-01.txt AC 3 ms 508 KB
sample-02.txt AC 3 ms 508 KB
test-01.txt AC 3 ms 508 KB
test-02.txt AC 3 ms 508 KB
test-03.txt AC 3 ms 508 KB
test-04.txt AC 3 ms 508 KB
test-05.txt AC 3 ms 508 KB
test-06.txt AC 3 ms 508 KB
test-07.txt AC 3 ms 508 KB
test-08.txt AC 3 ms 508 KB
test-09.txt AC 3 ms 508 KB
test-10.txt AC 3 ms 508 KB
test-11.txt AC 3 ms 508 KB
test-12.txt AC 3 ms 508 KB
test-13.txt AC 3 ms 2556 KB
test-14.txt AC 3 ms 508 KB
test-15.txt AC 3 ms 508 KB
test-16.txt AC 3 ms 508 KB
test-17.txt AC 3 ms 508 KB
test-18.txt AC 3 ms 508 KB
test-19.txt AC 3 ms 508 KB
test-20.txt AC 3 ms 508 KB