結果

問題 No.2157 崖
ユーザー 👑 obakyanobakyan
提出日時 2022-12-29 11:25:18
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 3,552 ms / 6,000 ms
コード長 1,286 bytes
コンパイル時間 140 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 29,568 KB
最終ジャッジ日時 2024-05-03 13:19:43
合計ジャッジ時間 33,879 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2,914 ms
21,120 KB
testcase_14 AC 2,740 ms
29,568 KB
testcase_15 AC 2,924 ms
23,168 KB
testcase_16 AC 3,015 ms
23,168 KB
testcase_17 AC 2,445 ms
27,008 KB
testcase_18 AC 2,446 ms
26,624 KB
testcase_19 AC 3,434 ms
23,936 KB
testcase_20 AC 3,552 ms
28,928 KB
testcase_21 AC 3,522 ms
29,440 KB
testcase_22 AC 2,331 ms
25,984 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

local n, m = io.read("*n", "*n")
local d = {}
for i = 1, n do
  d[i] = {}
  for j = 1, m do
    d[i][j] = io.read("*n")
  end
  table.sort(d[i])
end
local dp1, dp2 = {}, {}
local function solve(x)
  for i = 1, m + 1 do
    dp1[i] = true
  end
  for i = 2, n do
    local src = i % 2 == 0 and dp1 or dp2
    local dst = i % 2 == 0 and dp2 or dp1
    for j = 1, m + 1 do
      dst[j] = 0
    end
    local dsrc = d[i - 1]
    local ddst = d[i]
    local bleft, bright = 1, 1
    for apos = 1, m do
      while bleft <= m and ddst[bleft] < dsrc[apos] do
        bleft = bleft + 1
      end
      if m < bleft then break end
      while bright <= m and ddst[bright] <= dsrc[apos] + x do
        bright = bright + 1
      end
      if src[apos] then
        dst[bleft] = dst[bleft] + 1
        dst[bright] = dst[bright] - 1
      end
    end
    for j = 2, m do
      dst[j] = dst[j] + dst[j - 1]
    end
    local f = false
    for j = 1, m do
      dst[j] = 0 < dst[j]
      if dst[j] then f = true end
    end
    if not f then return false end
  end
  return true
end
local min, max = -1, 1000000000
while 1 < max - min do
  local mid = math.floor((min + max) / 2)
  if solve(mid) then
    max = mid
  else
    min = mid
  end
end
if max == 1000000000 then print(-1)
else print(max)
end
0