結果

問題 No.506 限られたジャパリまん
ユーザー 👑 obakyanobakyan
提出日時 2021-07-20 21:46:29
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 2,506 bytes
コンパイル時間 110 ms
コンパイル使用メモリ 5,332 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-10 12:57:30
合計ジャッジ時間 1,537 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 72 ms
4,376 KB
testcase_09 AC 29 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 74 ms
4,376 KB
testcase_12 AC 44 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 57 ms
4,376 KB
testcase_17 AC 5 ms
4,376 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 10 ms
4,376 KB
testcase_20 AC 20 ms
4,376 KB
testcase_21 AC 7 ms
4,376 KB
testcase_22 AC 24 ms
4,376 KB
testcase_23 AC 16 ms
4,380 KB
testcase_24 AC 4 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local bls, brs = bit.lshift, bit.rshift
local bxor = bit.bxor
local function badd(a, b) return (a + b) % mod end
local w, h, k, p = io.read("*n", "*n", "*n", "*n", "*l")
h, w = h + 1, w + 1
local row, col, name = {}, {}, {}
for i = 1, k do
  local a, b, c = io.read():match("(%d+) (%d+) (%w+)")
  col[i] = tonumber(a) + 1
  row[i] = tonumber(b) + 1
  name[i] = c
end
local map = {}
for i = 1, h do
  map[i] = {}
  for j = 1, w do
    map[i][j] = true
  end
end
for i = 1, k do
  map[row[i]][col[i]] = false
end

local function grayCode(x)
  return bxor(x, brs(x, 1))
end

local function grayWalk(size, add_func, rm_func, work_func)
  local prv = 0
  local total = bls(1, size) - 1
  local bpos = {}
  for i = 1, size do
    bpos[bls(1, i - 1)] = i
  end
  work_func()
  for i = 1, total do
    local v = grayCode(i)
    if prv < v then
      prv, v = v, v - prv
      add_func(bpos[v])
    else
      prv, v = v, prv - v
      rm_func(bpos[v])
    end
    work_func()
  end
end
local box = 0
local bcnt = 0
local t = {}
for i = 1, h do
  t[i] = {}
end
local retmax, retbox = 0LL, 0

local function add_func(idx)
  map[row[idx]][col[idx]] = true
  bcnt = bcnt + 1
  box = box + bls(1, idx - 1)
end
local function rm_func(idx)
  map[row[idx]][col[idx]] = false
  bcnt = bcnt - 1
  box = box - bls(1, idx - 1)
end
local function work_func()
  if bcnt == p then
    t[1][1] = 1LL
    for j = 2, w do
      if map[1][j] then
        if t[1][j - 1] < 0LL then
          t[1][j] = -1LL
        else
          t[1][j] = 1LL
        end
      else
        t[1][j] = -1LL
      end
    end
    for i = 2, h do
      if map[i][1] then
        t[i][1] = t[i - 1][1]
      else
        t[i][1] = -1LL
      end
      for j = 2, w do
        if map[i][j] then
          if t[i][j - 1] < 0LL and t[i - 1][j] < 0LL then
            t[i][j] = -1LL
          elseif t[i][j - 1] < 0LL then
            t[i][j] = t[i - 1][j]
          elseif t[i - 1][j] < 0LL then
            t[i][j] = t[i][j - 1]
          else
            t[i][j] = t[i - 1][j] + t[i][j - 1]
          end
        else
          t[i][j] = -1LL
        end
      end
    end
    if retmax < t[h][w] then
      retmax, retbox = t[h][w], box
    end
  end
end
grayWalk(k, add_func, rm_func, work_func)
if retmax == 0LL then
  print(0)
else
  retmax = tostring(retmax % 1000000007LL):gsub("LL", "")
  print(retmax)
  local i = 0
  while 0 < retbox do
    i = i + 1
    if retbox % 2 == 1 then
      print(name[i])
    end
    retbox = brs(retbox, 1)
  end
end
0