結果

問題 No.2157 崖
ユーザー 👑 obakyanobakyan
提出日時 2022-12-29 11:25:18
言語 Lua
(LuaJit 2.1.1696795921)
結果
AC  
実行時間 3,609 ms / 6,000 ms
コード長 1,286 bytes
コンパイル時間 438 ms
コンパイル使用メモリ 7,020 KB
実行使用メモリ 28,544 KB
最終ジャッジ日時 2023-08-16 03:49:29
合計ジャッジ時間 37,478 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,388 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2,947 ms
20,092 KB
testcase_14 AC 2,970 ms
28,504 KB
testcase_15 AC 2,996 ms
22,804 KB
testcase_16 AC 2,982 ms
22,812 KB
testcase_17 AC 2,644 ms
26,440 KB
testcase_18 AC 2,639 ms
26,060 KB
testcase_19 AC 3,443 ms
22,488 KB
testcase_20 AC 3,609 ms
28,216 KB
testcase_21 AC 3,515 ms
28,544 KB
testcase_22 AC 2,454 ms
25,508 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 3 ms
4,384 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