結果

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

テストケース

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

ソースコード

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
      t[i][1] = map[i][1] and 1LL or -1LL
      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