結果

問題 No.130 XOR Minimax
ユーザー te-shte-sh
提出日時 2017-01-30 22:42:20
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 942 ms / 5,000 ms
コード長 881 bytes
コンパイル時間 2,243 ms
コンパイル使用メモリ 93,612 KB
実行使用メモリ 20,596 KB
最終ジャッジ日時 2023-09-03 01:05:44
合計ジャッジ時間 9,406 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 390 ms
5,976 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 12 ms
5,424 KB
testcase_05 AC 123 ms
20,596 KB
testcase_06 AC 141 ms
8,048 KB
testcase_07 AC 192 ms
14,192 KB
testcase_08 AC 942 ms
8,312 KB
testcase_09 AC 40 ms
4,380 KB
testcase_10 AC 57 ms
6,424 KB
testcase_11 AC 147 ms
7,284 KB
testcase_12 AC 23 ms
4,376 KB
testcase_13 AC 121 ms
6,684 KB
testcase_14 AC 793 ms
7,604 KB
testcase_15 AC 11 ms
4,380 KB
testcase_16 AC 549 ms
6,496 KB
testcase_17 AC 568 ms
6,444 KB
testcase_18 AC 609 ms
6,756 KB
testcase_19 AC 745 ms
7,496 KB
testcase_20 AC 355 ms
6,604 KB
testcase_21 AC 784 ms
7,468 KB
testcase_22 AC 164 ms
4,568 KB
testcase_23 AC 52 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void main()
{
  auto n = readln.chomp.to!size_t;
  auto ai = readln.split.to!(int[]);

  int calc(size_t i, int[] ai) {
    if (ai.empty) return int.max;
    auto bi = ai.filter!(a => a.bitTest(i)).map!(a => a.bitReset(i)).array;
    auto ci = ai.filter!(a => !a.bitTest(i)).array;
    if (i == 0) return bi.empty || ci.empty ? 0 : 1;
    auto b = calc(i - 1, bi);
    if (!ci.empty) b = b.bitSet(i);
    auto c = calc(i - 1, ci);
    if (!bi.empty) c = c.bitSet(i);
    return min(b, c);
  }

  writeln(calc(ai.fold!max.bsr, ai));
}

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