結果

問題 No.1029 JJOOII 3
ユーザー 👑 obakyan
提出日時 2020-04-17 23:14:19
言語 Lua
(LuaJit 2.1.1734355927)
結果
TLE  
実行時間 -
コード長 2,872 bytes
コンパイル時間 241 ms
コンパイル使用メモリ 6,820 KB
実行使用メモリ 257,700 KB
最終ジャッジ日時 2024-10-03 15:14:38
合計ジャッジ時間 19,659 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 3 TLE * 6 -- * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local mfl = math.floor

local function lower_bound(ary, x)
  local num = #ary
  if num == 0 then return 1 end
  if x <= ary[1] then return 1 end
  if ary[num] < x then return num + 1 end
  local min, max = 1, num
  while 1 < max - min do
    local mid = brs(min + max, 1)
    if ary[mid] < x then
      min = mid
    else
      max = mid
    end
  end
  return max
end

local n, k = io.read("*n", "*n", "*l")
local w, len, c = {}, {}, {}
for i = 1, n do
  local s = io.read()
  local wi, ci = s:match("(%w+) (%d+)")
  w[i] = wi
  len[i] = #wi
  c[i] = tonumber(ci)
end
local wj, wo, wi = {}, {}, {}
for i = 1, n do
  wj[i] = {}
  for j = 1, #w[i] do
    if w[i]:sub(j, j) == "J" then
      table.insert(wj[i], j)
    end
  end
  wo[i] = {}
  for j = 1, #w[i] do
    if w[i]:sub(j, j) == "O" then
      table.insert(wo[i], j)
    end
  end
  wi[i] = {}
  for j = 1, #w[i] do
    if w[i]:sub(j, j) == "I" then
      table.insert(wi[i], j)
    end
  end
end

local t = {}
local edge = {}
for i = 1, k * 3 + 1 do
  edge[i] = {}
  t[i] = -1
end
for i_n = 1, n do
  local jlim = #wj[i_n]
  local olim = #wo[i_n]
  local ilim = #wi[i_n]
  for j = 1, k * 3 do
    local step = 0
    local need_j = mma(0, k + 1 - j)
    local need_o = mma(0, mmi(k, 2 * k + 1 - j))
    local need_i = mmi(k, 3 * k + 1 - j)
    if jlim < need_j then
      step = jlim
    elseif 0 == need_j and olim < need_o then
      step = olim
    elseif 0 == need_o then
      step = mmi(ilim, need_i)
    else
      local offset = 0
      if 0 < need_j then
        if #wj[i_n] < need_j then
          step = #wj[i_n]
          offset = len[i_n] + 1
        else
          step = need_j
          offset = wj[i_n][need_j]
        end
      end
      if offset <= len[i_n] then
        if 0 < need_o then
          local ofst_o = lower_bound(wo[i_n], offset) - 1
          if #wo[i_n] < need_o + ofst_o then
            step = step + #wo[i_n] - ofst_o
            offset = len[i_n] + 1
          else
            step = step + need_o
            offset = wo[i_n][need_o + ofst_o]
          end
        end
      end
      if offset <= len[i_n] then
        if 0 < need_i then
          local ofst_i = lower_bound(wi[i_n], offset) - 1
          step = step + mmi(#wi[i_n] - ofst_i, need_i)
        end
      end
    end
    if 0 < step then
      if edge[j][j + step] then
        edge[j][j + step] = mmi(edge[j][j + step], c[i_n])
      else
        edge[j][j + step] = c[i_n]
      end
    end
  end--j
end--i_n
-- print(os.clock())

t[1] = 0
for i = 1, 3 * k do
  local srccost = t[i]
  if 0 <= srccost then
    for dst, cost in pairs(edge[i]) do
      if t[dst] == -1 then
        t[dst] = srccost + cost
      else
        t[dst] = mmi(t[dst], srccost + cost)
      end
    end
  end
end
print(t[3 * k + 1])
-- print(os.clock())
0